Skip to content

Commit 8889e11

Browse files
committed
feat(components): add Switch story
1 parent 11943d3 commit 8889e11

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

Diff for: components/mdc/Switch/Switch.svelte

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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>

Diff for: components/mdc/Switch/_index.scss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@use '@material/switch/styles';

Diff for: components/mdc/Switch/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './_index.scss'
2+
import Switch from './Switch.svelte'
3+
4+
export default Switch

Diff for: components/mdc/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Page from './Page'
2121
import Progress from './Progress'
2222
import Select from './Select'
2323
import Snackbar from './Snackbar'
24+
import Switch from './Switch'
2425
import TabBar from './TabBar'
2526
import { TextArea, TextField, MoneyInput } from './TextInput'
2627
import Tooltip from './Tooltip'
@@ -55,4 +56,5 @@ export {
5556
Snackbar,
5657
Select,
5758
setNotice,
59+
Switch,
5860
}

Diff for: stories/Switch.stories.svelte

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script>
2+
import { Meta, Template, Story } from '@storybook/addon-svelte-csf'
3+
import { Switch } from '../components/mdc'
4+
5+
const args = {
6+
class: '',
7+
'on:selected': () => alert('switch is on'),
8+
'on:deselected': () => alert('switch is off'),
9+
}
10+
</script>
11+
12+
<Meta title="Atoms/Switch" component={Switch} />
13+
14+
<Template let:args>
15+
<Switch {...args} on:selected={args['on:selected']} on:deselected={args['on:deselected']} />
16+
</Template>
17+
18+
<Story name="Primary" {args} />
19+
20+
<Story name="Label" args={{ ...args, label: 'ON/OFF' }} />
21+
22+
<Story name="Disabled" args={{ ...args, disabled: true }} />
23+
24+
<Story name="Initially Selected" args={{ ...args, selected: true }} />

0 commit comments

Comments
 (0)