@use "../../../utils/css/mixins"; @use "@wordpress/base-styles/variables"; @layer wp-ui { @layer utilities, components, compositions, overrides; @layer components { .root { --_wp-ui-switch-height: var(--wpds-dimension-size-2xs); --_wp-ui-switch-thumb-width: var(--wpds-dimension-size-xs); --_wp-ui-switch-border-width: var(--wpds-border-width-xs); --_wp-ui-switch-label-size: var(--wpds-dimension-size-4xs); /* How far the thumb, and the labels with it, travel between the two states. */ --_wp-ui-switch-travel: calc(var(--_wp-ui-switch-height) * 2 - var(--_wp-ui-switch-thumb-width)); /* Centers each label in the part of the track the thumb leaves exposed. */ --_wp-ui-switch-label-offset: calc((var(--_wp-ui-switch-travel) - var(--_wp-ui-switch-label-size)) / 2 + var(--_wp-ui-switch-border-width)); /* Where the brand panel starts when the switch is off. Its inline-end edge sits under the middle of the thumb. */ --_wp-ui-switch-panel-offset: calc(var(--_wp-ui-switch-thumb-width) / 2 - var(--_wp-ui-switch-height) * 2); position: relative; display: inline-block; flex-shrink: 0; appearance: none; padding: 0; width: calc(var(--_wp-ui-switch-height) * 2); height: var(--_wp-ui-switch-height); border: var(--_wp-ui-switch-border-width) solid var(--wpds-color-stroke-interactive-neutral); border-radius: 999px; background-color: var(--wpds-color-background-track-neutral); &:not([data-disabled]) { cursor: var(--wpds-cursor-control); } &:focus { @include mixins.focus-ring(); } @media not (prefers-reduced-motion) { &:not(:hover):not(:active) { transition: border-color var(--wpds-motion-duration-md) var(--wpds-motion-easing-subtle); } } [data-highlighted] &:not([data-checked]), &:hover:not([data-disabled], [data-checked]) { border-color: var(--wpds-color-stroke-interactive-neutral-active); } &[data-checked] { border-color: var(--wpds-color-background-interactive-brand-strong); @media (forced-colors: active) { border-color: Highlight; } } &[data-disabled] { background-color: var(--wpds-color-background-track-neutral-disabled); border-color: var(--wpds-color-stroke-interactive-neutral-disabled); @media (forced-colors: active) { border-color: GrayText; } } } .thumb { position: absolute; /* The thumb covers the track border, so it spans the full switch height. */ inset-block-start: calc(-1 * var(--_wp-ui-switch-border-width)); inset-inline-start: calc(-1 * var(--_wp-ui-switch-border-width)); display: block; width: var(--_wp-ui-switch-thumb-width); height: var(--_wp-ui-switch-height); border: var(--_wp-ui-switch-border-width) solid var(--wpds-color-stroke-interactive-neutral); border-radius: 999px; background-color: var(--wpds-color-background-thumb-neutral); box-shadow: variables.$elevation-x-small; @media not (prefers-reduced-motion) { transition: border-color var(--wpds-motion-duration-md) var(--wpds-motion-easing-subtle), inset-inline-start var(--wpds-motion-duration-md) var(--wpds-motion-easing-balanced); } @media (forced-colors: active) { background-color: Canvas; border-color: CanvasText; transition: none; } [data-highlighted] .root:not([data-checked]) > &, .root:hover:not([data-disabled], [data-checked]) > & { border-color: var(--wpds-color-stroke-interactive-neutral-active); } &[data-checked] { background-color: var(--wpds-color-background-thumb-brand-strong); border-color: var(--wpds-color-stroke-interactive-brand); inset-inline-start: calc(var(--_wp-ui-switch-travel) - var(--_wp-ui-switch-border-width)); @media (forced-colors: active) { border-color: Highlight; } } &[data-disabled] { background-color: var(--wpds-color-background-thumb-neutral-disabled); border-color: var(--wpds-color-stroke-interactive-neutral-disabled); box-shadow: none; @media (forced-colors: active) { border-color: GrayText; } } } /* * Clips the brand panel and the labels as they slide in and out. It covers * the track border, so the panel's rounded edge meets the page rather than * the neutral track. */ .labels { position: absolute; inset: calc(-1 * var(--_wp-ui-switch-border-width)); overflow: hidden; border-radius: inherit; pointer-events: none; /* * The brand half of the track. It slides with the thumb rather than * changing color, and the thumb always covers the edge where it meets * the neutral half. */ &::before { content: ""; position: absolute; inset-block: 0; inset-inline-start: var(--_wp-ui-switch-panel-offset); width: calc(var(--_wp-ui-switch-height) * 2); background-color: var(--wpds-color-background-interactive-brand-strong); @media not (prefers-reduced-motion) { transition: inset-inline-start var(--wpds-motion-duration-md) var(--wpds-motion-easing-balanced); } @media (forced-colors: active) { background-color: Highlight; transition: none; } [data-checked] > & { inset-inline-start: calc(var(--_wp-ui-switch-panel-offset) + var(--_wp-ui-switch-travel)); } /* Disabled switches use the same track whether on or off. */ [data-disabled] > & { display: none; } } } /* * The labels slide along with the thumb: the label for the current state * sits in the exposed part of the track, and the other waits just outside * the track's edge, where it is clipped. */ .label { position: absolute; inset-block: 0; margin-block: auto; width: var(--_wp-ui-switch-label-size); height: var(--_wp-ui-switch-label-size); @media not (prefers-reduced-motion) { transition: inset-inline-start var(--wpds-motion-duration-md) var(--wpds-motion-easing-balanced), inset-inline-end var(--wpds-motion-duration-md) var(--wpds-motion-easing-balanced); } /* Keep in step with the thumb, which doesn't animate in forced colors mode. */ @media (forced-colors: active) { transition: none; } [data-disabled] > .labels > & { color: var(--wpds-color-foreground-interactive-neutral-disabled); @media (forced-colors: active) { color: GrayText; } } } .on-label { inset-inline-start: calc(var(--_wp-ui-switch-label-offset) - var(--_wp-ui-switch-travel)); color: var(--wpds-color-foreground-interactive-brand-strong); @media (forced-colors: active) { color: HighlightText; } [data-checked] > .labels > & { inset-inline-start: var(--_wp-ui-switch-label-offset); } } .off-label { inset-inline-end: var(--_wp-ui-switch-label-offset); color: var(--wpds-color-foreground-interactive-neutral-weak); @media (forced-colors: active) { color: CanvasText; } [data-checked] > .labels > & { inset-inline-end: calc(var(--_wp-ui-switch-label-offset) - var(--_wp-ui-switch-travel)); } } } }