From d33b5c48701f1c6b7bc22ca5af723562c537c4a3 Mon Sep 17 00:00:00 2001 From: Lea Verou Date: Wed, 18 Dec 2024 21:33:52 -0500 Subject: [PATCH] [Switch] Simplify DOM & CSS --- src/components/switch/switch.css | 34 ++++++------- src/components/switch/switch.ts | 85 +++++++++++++++----------------- 2 files changed, 55 insertions(+), 64 deletions(-) diff --git a/src/components/switch/switch.css b/src/components/switch/switch.css index f7f767027..c6e4566c5 100644 --- a/src/components/switch/switch.css +++ b/src/components/switch/switch.css @@ -16,7 +16,7 @@ display: inline-block; } -.switch { +label { position: relative; display: flex; align-items: center; @@ -26,7 +26,7 @@ cursor: pointer; } -.control { +.switch { flex: 0 0 auto; position: relative; display: flex; @@ -40,23 +40,19 @@ border-style: var(--border-style); border-width: var(--border-width); box-shadow: var(--box-shadow); - transition: - background var(--wa-transition-normal) var(--wa-transition-easing), - border-color var(--wa-transition-normal) var(--wa-transition-easing); + transition-property: translate, background, border-color, box-shadow; + transition-duration: var(--wa-transition-normal); + transition-timing-function: var(--wa-transition-easing); } -.control .thumb { +.switch .thumb { width: var(--thumb-size); height: var(--thumb-size); background-color: var(--thumb-color); border-radius: 50%; box-shadow: var(--thumb-shadow); translate: calc((var(--width) - var(--height)) / -2); - transition: - translate var(--wa-transition-normal) var(--wa-transition-easing), - background-color var(--wa-transition-normal) var(--wa-transition-easing), - border-color var(--wa-transition-normal) var(--wa-transition-easing), - box-shadow var(--wa-transition-normal) var(--wa-transition-easing); + transition: inherit; } .input { @@ -68,29 +64,29 @@ } /* Focus */ -.switch:not(.switch--disabled) .input:focus-visible ~ .control .thumb { +label:not(.disabled) .input:focus-visible ~ .switch .thumb { outline: var(--wa-focus-ring); outline-offset: var(--wa-focus-ring-offset); } /* Checked */ -.switch--checked .control { +.checked .switch { background-color: var(--background-color-checked); border-color: var(--border-color-checked); } -.switch--checked .control .thumb { +.checked .switch .thumb { background-color: var(--thumb-color-checked); translate: calc((var(--width) - var(--height)) / 2); } /* Disabled */ -.switch--disabled { +label:has(> :disabled) { opacity: 0.5; cursor: not-allowed; } -.label { +[part~='label'] { display: inline-block; line-height: var(--height); margin-inline-start: 0.5em; @@ -98,15 +94,15 @@ -webkit-user-select: none; } -:host([required]) .label::after { +:host([required]) [part~='label']::after { content: var(--wa-form-control-required-content); color: var(--wa-form-control-required-content-color); margin-inline-start: var(--wa-form-control-required-content-offset); } @media (forced-colors: active) { - .switch.switch--checked:not(.switch--disabled) .control:hover .thumb, - .switch--checked .control .thumb { + :checked:enabled + .switch:hover .thumb, + :checked + .switch .thumb { background-color: ButtonText; } } diff --git a/src/components/switch/switch.ts b/src/components/switch/switch.ts index df53a9177..99996a985 100644 --- a/src/components/switch/switch.ts +++ b/src/components/switch/switch.ts @@ -239,54 +239,49 @@ export default class WaSwitch extends WebAwesomeFormAssociatedElement { const hasHint = this.hint ? true : !!hasHintSlot; return html` -
- - ${this.hint} -
+ ${this.hint} `; } }