5.2.0 • Published 6 months ago

@w0s/input-switch v5.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Implement something like <input type=checkbox switch>

npm version Workflow status

Demo

Examples

<script type="importmap">
  {
    "imports": {
      "@w0s/input-switch": "...",
      "@w0s/shadow-append-css": "..."
    }
  }
</script>
<script type="module">
  import InputSwitch from '@w0s/input-switch';

  customElements.define('x-input-switch', InputSwitch);
</script>

<label><x-input-switch
  name="foo"
  value="1"
  checked=""
  disabled=""
  storage-key="foo"
></x-input-switch> switch</label>

HTML attributes

Style customization

The following CSS custom properties can be used to customize the style.

namedeaultDescription
--outline-offset1pxoutline-offset property value of switch control
--block-size1emBlock size of switch control
--inline-size2emInline size of switch control
--animation-duration0.5sTime a transition animation (transition-duration property)
--track-color-on#2299ffTrack color when switch is on
--track-color-off#ccccccTrack color when switch is off
--track-color-disabled-on#666666Track color when switch is on disabled
--track-color-disabled-off#666666Track color when switch is off and disabled
--thumb-radiuscalc(0.5em - 1px)Radius of slider ball
--thumb-color#ffffffColor of slider ball

The shadow DOM of the switch looks like this.

<x-input-switch class="my-switch">
  #shadow-root (open)
  <div part="track"></div>
  <div part="thumb"></div>
</x-input-switch>

Therefore, the ::part CSS pseudo-element may be used to customize the style as desired.

.my-switch {
  --outline-offset: ...;
  --block-size: ...;
  ...

  &::part(track) {
    ...
  }

  &::part(thumb) {
    ...
  }
}

However, customizations other than CSS custom properties may be broken in future version updates.

5.2.0

6 months ago

5.1.1

7 months ago

5.1.0

7 months ago

5.0.0

11 months ago

4.0.1

1 year ago

4.0.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago