Skip to content

Commit 957534a

Browse files
committed
✨ Add Switch component for Svelte & React
1 parent a56fb4e commit 957534a

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"svelte.plugin.svelte.compilerWarnings": {
33
"a11y-click-events-have-key-events": "ignore",
4-
"a11y-no-static-element-interactions": "ignore"
4+
"a11y-no-static-element-interactions": "ignore",
5+
"a11y-no-noninteractive-element-interactions": "ignore"
56
}
67
}

src/components/Switch/Switch.svelte

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script lang="ts">
2+
import type { SwitchProps } from './switch'
3+
import './switch.scss'
4+
5+
export let label: SwitchProps['label'] = ''
6+
export let toggled: SwitchProps['toggled'] = false
7+
export let offColor: SwitchProps['offColor'] = ''
8+
export let onColor: SwitchProps['onColor'] = ''
9+
export let reverse: SwitchProps['reverse'] = false
10+
export let small: SwitchProps['small'] = false
11+
export let square: SwitchProps['square'] = false
12+
export let disabled: SwitchProps['disabled'] = false
13+
export let className: SwitchProps['className'] = ''
14+
export let onClick: () => any = () => {}
15+
16+
const classes = [
17+
'w-switch',
18+
reverse && 'reverse',
19+
small && 'small',
20+
square && 'square',
21+
disabled && 'disabled',
22+
className
23+
].filter(Boolean).join(' ')
24+
25+
const styles = [
26+
offColor && `--w-switch-off-color: ${offColor};`,
27+
onColor && `--w-switch-on-color: ${onColor};`
28+
].filter(Boolean).join(' ')
29+
</script>
30+
31+
<label class={classes} style={styles || null} on:click={onClick}>
32+
<input type="checkbox" checked={toggled} disabled={disabled} />
33+
<span class="toggle"></span>
34+
{#if label}
35+
<span class="label">{label}</span>
36+
{/if}
37+
</label>

src/components/Switch/Switch.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,44 @@ import React from 'react'
22
import type { SwitchProps } from './switch'
33
import './switch.scss'
44

5+
type ReactSwitchProps = {
6+
onClick: () => any
7+
} & SwitchProps
8+
59
const Switch = ({
6-
7-
}: SwitchProps) => {
10+
label,
11+
toggled,
12+
offColor,
13+
onColor,
14+
reverse,
15+
small,
16+
square,
17+
disabled,
18+
className,
19+
onClick
20+
}: ReactSwitchProps) => {
21+
const classes = [
22+
'w-switch',
23+
reverse && 'reverse',
24+
small && 'small',
25+
square && 'square',
26+
disabled && 'disabled',
27+
className
28+
].filter(Boolean).join(' ')
829

30+
const styles = {
31+
...(offColor && { '--w-switch-off-color': offColor }),
32+
...(onColor && { '--w-switch-on-color': onColor })
33+
} as React.CSSProperties
934

1035
return (
11-
<div>switch</div>
36+
<label className={classes} style={styles || null} onClick={onClick}>
37+
<input type="checkbox" checked={toggled} disabled={disabled} />
38+
<span className="toggle"></span>
39+
{label && <span className="label">{label}</span>}
40+
</label>
1241
)
42+
1343
}
1444

1545
export default Switch

src/pages/switch.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ const sections = [
3030
</ComponentWrapper>
3131

3232
<ComponentWrapper type="Svelte">
33-
<SvelteSwitch color="#ee5253" />
33+
<SvelteSwitch offColor="#ee5253" />
3434
</ComponentWrapper>
3535

3636
<ComponentWrapper type="React">
37-
<ReactSwitch color="#48dbfb" />
37+
<ReactSwitch offColor="#48dbfb" />
3838
</ComponentWrapper>
3939
</div>
4040

svelte.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default {
88
const ignoreWarnings = [
99
'a11y-click-events-have-key-events',
1010
'a11y-no-static-element-interactions',
11+
'a11y-no-noninteractive-element-interactions',
1112
'.accordion-title'
1213
]
1314

0 commit comments

Comments
 (0)