|
| 1 | +<!-- https://github.com/material-components/material-components-web/tree/master/packages/mdc-switch --> |
| 2 | +<script> |
| 3 | +import { MDCSwitch } from '@material/switch' |
| 4 | +import { createEventDispatcher, onMount } from 'svelte' |
| 5 | +
|
| 6 | +/** |
| 7 | + * displayed right of the switch |
| 8 | + */ |
| 9 | +export let label = 'off/on' |
| 10 | +/** |
| 11 | + * disables the switch |
| 12 | + */ |
| 13 | +export let disabled = false |
| 14 | +/** |
| 15 | + * used to set the switch initial state |
| 16 | + */ |
| 17 | +export let selected = false |
| 18 | +
|
| 19 | +let element = {} |
| 20 | +let switchControl = {} |
| 21 | +
|
| 22 | +onMount(() => { |
| 23 | + switchControl = new MDCSwitch(element) |
| 24 | +}) |
| 25 | +
|
| 26 | +const dispatch = createEventDispatcher() |
| 27 | +
|
| 28 | +// Used setTimeout because switchControl.selected needs to be updated before dispatching the event |
| 29 | +function onClick() { |
| 30 | + setTimeout(() => dispatch(switchControl.selected ? 'selected' : 'deselected')) |
| 31 | +} |
| 32 | +</script> |
| 33 | + |
| 34 | +<!-- |
| 35 | + @component |
| 36 | + Used to toggle an item on tablet and mobile devices or immediately activate/deactive something |
| 37 | + see https://silinternational.github.io/ui-components/?path=/docs/atoms-switch--primary for usage |
| 38 | +--> |
| 39 | + |
| 40 | +<button |
| 41 | + on:click={onClick} |
| 42 | + bind:this={element} |
| 43 | + id="basic-switch" |
| 44 | + class="mdc-switch" |
| 45 | + class:mdc-switch--selected={selected} |
| 46 | + class:mdc-switch--unselected={!selected} |
| 47 | + type="button" |
| 48 | + role="switch" |
| 49 | + aria-checked={selected} |
| 50 | + {disabled} |
| 51 | +> |
| 52 | + <div class="mdc-switch__track" /> |
| 53 | + <div class="mdc-switch__handle-track"> |
| 54 | + <div class="mdc-switch__handle"> |
| 55 | + <div class="mdc-switch__shadow"> |
| 56 | + <div class="mdc-elevation-overlay" /> |
| 57 | + </div> |
| 58 | + <div class="mdc-switch__ripple" /> |
| 59 | + <div class="mdc-switch__icons"> |
| 60 | + <svg class="mdc-switch__icon mdc-switch__icon--on" viewBox="0 0 24 24"> |
| 61 | + <path d="M19.69,5.23L8.96,15.96l-4.23-4.23L2.96,13.5l6,6L21.46,7L19.69,5.23z" /> |
| 62 | + </svg> |
| 63 | + <svg class="mdc-switch__icon mdc-switch__icon--off" viewBox="0 0 24 24"> |
| 64 | + <path d="M20 13H4v-2h16v2z" /> |
| 65 | + </svg> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + <span class="mdc-switch__focus-ring-wrapper"> |
| 70 | + <div class="mdc-switch__focus-ring" /> |
| 71 | + </span> |
| 72 | +</button> |
| 73 | +<label for="basic-switch">{label}</label> |
0 commit comments