Toggle
在按下与未按下状态之间切换的按钮。
- 浏览器
- Chrome 123+, Firefox 121+, Safari 17.5+
- 层级
- 0
- 压缩后大小
- 295 B
- 强制布局预算
- 0 ms
- axe 预算
- 0
- 支持 SSR
- 是
- 公开契约
- 测试环境
交互示例
切换
默认
已禁用
- 1
- 2
- 3
- 4
- 5
import { Toggle } from "@gunkin/uiify";
export default function Example() {
return <Toggle className="" id="bold" label="Bold" />;
}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
/* Toggle + native-control toggle groups. The labels are styled by the Button skin
(`data-uiify-button`); this file only places the native controls behind them. */
@layer components {
[data-uiify-toggle] {
position: relative;
display: inline-flex;
}
[data-uiify-toggle] > input {
position: absolute;
inset: 0;
inline-size: 100%;
block-size: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
}
[data-uiify-toggle] > input:disabled {
cursor: not-allowed;
}
[data-uiify-toggle] > input:focus-visible + [data-uiify-button],
[data-uiify-toggle-group] [data-part="item"] > input:focus-visible + [data-uiify-button] {
box-shadow: 0 0 0 var(--ring-width) color-mix(in oklch, var(--ring) 16%, transparent);
}
[data-uiify-toggle-group] {
display: inline-flex;
gap: var(--space-xs);
}
[data-uiify-toggle-group][data-orientation="vertical"] {
flex-direction: column;
}
[data-uiify-toggle-group] [data-part="item"] {
position: relative;
}
[data-uiify-toggle-group] [data-part="item"] > input {
position: absolute;
inline-size: 1px;
block-size: 1px;
margin: -1px;
overflow: hidden;
/* stylelint-disable-next-line property-no-deprecated -- fallback for older browsers */
clip: rect(0 0 0 0);
clip-path: inset(50%);
white-space: nowrap;
}
/* Group items are tighter than a standalone button, so a one-glyph item stays square. */
[data-uiify-toggle-group] [data-part="item"] > label {
padding-inline: 0.75rem;
}
}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
import { Toggle } from "@gunkin/uiify";
export default function Example() {
return (
<Toggle
className={
"[[data-uiify-toggle]:has(>&)]:relative [[data-uiify-toggle]:has(>&)]:inline-flex [[data-uiify-toggle]:has(>" +
"&)>input]:absolute [[data-uiify-toggle]:has(>&)>input]:inset-0 [[data-uiify-toggle]:has(>&)>input]:[inline-" +
"size:100%] [[data-uiify-toggle]:has(>&)>input]:[block-size:100%] [[data-uiify-toggle]:has(>&)>input]:m-0 [[" +
"data-uiify-toggle]:has(>&)>input]:opacity-0 [[data-uiify-toggle]:has(>&)>input]:cursor-pointer [[data-uiify" +
"-toggle]:has(>&)>input:disabled]:cursor-not-allowed relative isolate inline-flex items-center justify-cente" +
"r gap-1.5 min-h-11 min-w-11 px-5 py-px border-[length:var(--border-width)] border-solid border-transparent " +
"rounded-full bg-clip-padding text-base font-medium leading-6 no-underline cursor-pointer [transition:var(--" +
"transition-colors)] focus-visible:[outline:none] focus-visible:[box-shadow:0_0_0_var(--ring-width)_color-mi" +
"x(in_oklch,_var(--ring)_16%,_transparent)] disabled:opacity-50 disabled:cursor-not-allowed data-[disabled]:" +
"opacity-50 data-[disabled]:cursor-not-allowed before:content-[''] before:absolute before:inset-0 before:z-[" +
"-1] before:rounded-[inherit] before:pointer-events-none before:[background-color:var(--uiify-button-wash)] " +
"before:[filter:blur(10px)] before:[translate:0_-50%] before:[clip-path:inset(50%_0_-50%_0_round_var(--radiu" +
"s-full))] after:content-[''] after:absolute after:inset-0 after:z-[-1] after:rounded-[inherit] after:pointe" +
"r-events-none after:[padding:0.5px] after:[background-image:linear-gradient(to_bottom,_var(--uiify-button-r" +
"im),_transparent_75%)] after:[mask:linear-gradient(#fff_0_0)_content-box_exclude,_linear-gradient(#fff_0_0)" +
"] [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:[--uiify-button-rim:rgb(255_255_255_/_20%" +
")] [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:[--uiify-button-wash:rgb(255_255_255_/_1" +
"5%)] [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:border-transparent [&:is([data-selecte" +
"d],[aria-pressed=true],[aria-current=page])]:bg-primary [&:is([data-selected],[aria-pressed=true],[aria-cur" +
"rent=page])]:text-primary-foreground [:checked+&]:[--uiify-button-rim:rgb(255_255_255_/_20%)] [:checked+&]:" +
"[--uiify-button-wash:rgb(255_255_255_/_15%)] [:checked+&]:border-transparent [:checked+&]:bg-primary [:chec" +
"ked+&]:text-primary-foreground [@media_(any-hover:_hover)_and_(any-pointer:_fine)]:[&:is([data-selected],[a" +
"ria-pressed=true],[aria-current=page]):hover:not(:disabled,[data-disabled])]:[background-color:color-mix(in" +
"_oklch,_var(--primary)_88%,_var(--primary-foreground))] [@media_(any-hover:_hover)_and_(any-pointer:_fine)]" +
":[:checked:not(:disabled)+&:hover]:[background-color:color-mix(in_oklch,_var(--primary)_88%,_var(--primary-" +
"foreground))] [--uiify-button-rim:color-mix(in_oklch,_var(--foreground)_25%,_transparent)] [border-color:co" +
"lor-mix(in_oklch,_var(--foreground)_20%,_transparent)] bg-transparent text-foreground before:content-none a" +
"fter:[inset:calc(-1_*_var(--border-width))] after:[padding:var(--border-width)] hover:[&:not(:disabled,[dat" +
"a-disabled])]:[background-color:color-mix(in_oklch,_var(--foreground)_10%,_transparent)] active:[&:not(:dis" +
"abled,[data-disabled])]:[background-color:color-mix(in_oklch,_var(--foreground)_16%,_transparent)]"
}
id="bold"
label="Bold"
/>
);
}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
/* app.css */
@layer reset, ui, app, tokens, base, demo, components, utilities, theme;
@import "@gunkin/uiify/styles/reset";
@import "@gunkin/uiify/components/behavior.css";
@import "@gunkin/uiify/styles/tokens";
@import "@gunkin/uiify/styles/tailwind";
@import "tailwindcss/utilities.css";
@theme { --spacing: 0.25rem; --font-weight-normal: 400; --font-weight-medium: 500; --font-weight-semibold: 600; --font-weight-bold: 700; }
@source inline('[[data-uiify-toggle]:has(>&)]:relative [[data-uiify-toggle]:has(>&)]:inline-flex [[data-uiify-toggle]:has(>&)>input]:absolute [[data-uiify-toggle]:has(>&)>input]:inset-0 [[data-uiify-toggle]:has(>&)>input]:[inline-size:100%] [[data-uiify-toggle]:has(>&)>input]:[block-size:100%] [[data-uiify-toggle]:has(>&)>input]:m-0 [[data-uiify-toggle]:has(>&)>input]:opacity-0 [[data-uiify-toggle]:has(>&)>input]:cursor-pointer [[data-uiify-toggle]:has(>&)>input:disabled]:cursor-not-allowed relative isolate inline-flex items-center justify-center gap-1.5 min-h-11 min-w-11 px-5 py-px border-[length:var(--border-width)] border-solid border-transparent rounded-full bg-clip-padding text-base font-medium leading-6 no-underline cursor-pointer [transition:var(--transition-colors)] focus-visible:[outline:none] focus-visible:[box-shadow:0_0_0_var(--ring-width)_color-mix(in_oklch,_var(--ring)_16%,_transparent)] disabled:opacity-50 disabled:cursor-not-allowed data-[disabled]:opacity-50 data-[disabled]:cursor-not-allowed before:absolute before:inset-0 before:z-[-1] before:rounded-[inherit] before:pointer-events-none before:[background-color:var(--uiify-button-wash)] before:[filter:blur(10px)] before:[translate:0_-50%] before:[clip-path:inset(50%_0_-50%_0_round_var(--radius-full))] after:absolute after:inset-0 after:z-[-1] after:rounded-[inherit] after:pointer-events-none after:[padding:0.5px] after:[background-image:linear-gradient(to_bottom,_var(--uiify-button-rim),_transparent_75%)] after:[mask:linear-gradient(#fff_0_0)_content-box_exclude,_linear-gradient(#fff_0_0)] [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:[--uiify-button-rim:rgb(255_255_255_/_20%)] [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:[--uiify-button-wash:rgb(255_255_255_/_15%)] [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:border-transparent [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:bg-primary [&:is([data-selected],[aria-pressed=true],[aria-current=page])]:text-primary-foreground [:checked+&]:[--uiify-button-rim:rgb(255_255_255_/_20%)] [:checked+&]:[--uiify-button-wash:rgb(255_255_255_/_15%)] [:checked+&]:border-transparent [:checked+&]:bg-primary [:checked+&]:text-primary-foreground [@media_(any-hover:_hover)_and_(any-pointer:_fine)]:[&:is([data-selected],[aria-pressed=true],[aria-current=page]):hover:not(:disabled,[data-disabled])]:[background-color:color-mix(in_oklch,_var(--primary)_88%,_var(--primary-foreground))] [@media_(any-hover:_hover)_and_(any-pointer:_fine)]:[:checked:not(:disabled)+&:hover]:[background-color:color-mix(in_oklch,_var(--primary)_88%,_var(--primary-foreground))] [--uiify-button-rim:color-mix(in_oklch,_var(--foreground)_25%,_transparent)] [border-color:color-mix(in_oklch,_var(--foreground)_20%,_transparent)] bg-transparent text-foreground before:content-none after:[inset:calc(-1_*_var(--border-width))] after:[padding:var(--border-width)] hover:[&:not(:disabled,[data-disabled])]:[background-color:color-mix(in_oklch,_var(--foreground)_10%,_transparent)] active:[&:not(:disabled,[data-disabled])]:[background-color:color-mix(in_oklch,_var(--foreground)_16%,_transparent)]');
@source inline("before:content-[''] after:content-['']");
@layer base { :root { --uiify-control-text: var(--text-sm); } @media (pointer: coarse) { :root { --uiify-control-text: var(--text-base); } } }
/* Authored animation and registered properties used by the Tailwind skin.
* Visual rules belong in utility recipes; only at-rule definitions live here. */
@layer components {
@keyframes uiify-presence-in {
from {
opacity: 0;
scale: var(--presence-scale);
}
}
}
@layer components {
@keyframes uiify-presence-out {
to {
opacity: 0;
scale: var(--presence-scale);
}
}
}
@property --uiify-spinner-tail {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
@property --uiify-spinner-length {
syntax: "<angle>";
inherits: false;
initial-value: 36deg;
}
@layer components {
@keyframes uiify-spinner-rotate {
to {
transform: rotate(360deg);
}
}
}
@layer components {
@keyframes uiify-spinner-orbit {
0% {
--uiify-spinner-tail: 0deg;
--uiify-spinner-length: 36deg;
}
50% {
--uiify-spinner-tail: 90deg;
--uiify-spinner-length: 324deg;
}
100% {
--uiify-spinner-tail: 360deg;
--uiify-spinner-length: 36deg;
}
}
}
@layer components {
@keyframes uiify-skeleton-pulse {
50% {
opacity: 0.45;
}
}
}
@layer components {
@keyframes uiify-skeleton-shimmer {
to {
background-position: -200% 0;
}
}
}
@layer components {
@keyframes uiify-skeleton-reveal {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}
@property --candle-body-height {
syntax: "<percentage>";
inherits: true;
initial-value: 0%;
}
@property --candle-body-top {
syntax: "<percentage>";
inherits: true;
initial-value: 50%;
}
@property --candle-slot {
syntax: "<percentage>";
inherits: true;
initial-value: 100%;
}
@property --candle-wick-height {
syntax: "<percentage>";
inherits: true;
initial-value: 0%;
}
@property --candle-wick-top {
syntax: "<percentage>";
inherits: true;
initial-value: 50%;
}
@property --candle-x {
syntax: "<percentage>";
inherits: true;
initial-value: 0%;
}
@property --candle-scale {
syntax: "<number>";
inherits: true;
initial-value: 1;
}
@property --line-y {
syntax: "<percentage>";
inherits: true;
initial-value: 50%;
}
@property --line-prev-y {
syntax: "<percentage>";
inherits: true;
initial-value: 50%;
}
@property --line-count {
syntax: "<integer>";
inherits: false;
initial-value: 1;
}
@property --line-scale {
syntax: "<number>";
inherits: true;
initial-value: 1;
}
@layer components {
@keyframes uiify-otp-input-caret-blink {
50% {
opacity: 0;
}
}
}API
Toggle
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
id* | string | — | 标签所指向的checkbox输入框id。 |
label* | ReactNode | — | 标签内容,点击标签可切换输入框。 |
pressed | boolean | — | 受控的按下状态;提供后由调用方管理状态。 |
defaultPressed | boolean | false | 首次渲染的按下状态。非受控,因此无JavaScript时也可工作。 |
disabled | boolean | false | 禁用输入框,并为外层元素添加data-disabled。 |
className | string | — | 应用于label元素的类名。 |
键盘
| 按键 | 操作 |
|---|---|
| Enter / Space | 获得焦点时切换按下状态 |
无障碍
原生<button aria-pressed>(组变体使用隐藏的checkbox输入框)。无须JavaScript即可读取按下状态。
样式钩子
data-statedata-disabled