Toggle
Кнопка, которая переключается между нажатым и ненажатым состояниями.
- Браузеры
- Chrome 123+Firefox 121+Safari 17.5+
- Уровень
- 0
- Сжатый размер
- 295 B
- Лимит принудительной компоновки
- 0 ms
- Лимит axe
- 0
- Совместим с SSR
- Да
- Публичный контракт
- Тестовый стенд
Песочница
Переключить
По умолчанию
Отключена
import { Toggle } from "@gunkin/uiify";
export default function Example() {
return <Toggle className="" id="bold" label="Bold" />;
}/* 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;
}
}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"
/>
);
}/* 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, на которое указывает подпись. |
label* | ReactNode | — | Содержимое подписи, которая переключает поле при щелчке. |
pressed | boolean | — | Управляемое нажатое состояние; при его передаче состоянием управляет вызывающая сторона. |
defaultPressed | boolean | false | Нажатое состояние при первом рендеринге. Неуправляемое, поэтому работает без JavaScript. |
disabled | boolean | false | Отключает поле ввода и помечает обёртку атрибутом data-disabled. |
className | string | — | Класс элемента подписи. |
Клавиатура
| Клавиша | Действие |
|---|---|
| Enter / Space | Переключить нажатое состояние при наличии фокуса |
Доступность
Нативный <button aria-pressed> (или скрытое поле checkbox для группового варианта). Нажатое состояние читается без JavaScript.
Привязка стилей
data-statedata-disabled