-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
140 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@jeanne-mas/svelte-ui': minor | ||
--- | ||
|
||
Added switch component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<script context="module" lang="ts"> | ||
import { Switch as SwitchPrimitive } from 'bits-ui'; | ||
import type { SvelteHTMLElements } from 'svelte/elements'; | ||
import { cn } from '$lib/utils/cn.js'; | ||
import type { ComponentSlots } from '$lib/utils/types.js'; | ||
export type Attributes = Omit<SvelteHTMLElements['button'], 'disabled' | 'name' | 'value'>; | ||
export type Events = Pick<SwitchPrimitive.Props, 'onCheckedChange'>; | ||
export type Props = Omit<SwitchPrimitive.Props, keyof Attributes | keyof Events>; | ||
export type Slots = ComponentSlots<SwitchPrimitive.Root>; | ||
</script> | ||
|
||
<script lang="ts"> | ||
type $$Events = SwitchPrimitive.Events; | ||
type $$Props = Attributes & Events & Props; | ||
type $$Slots = Slots; | ||
export let asChild: Props['asChild'] = undefined; | ||
export let checked: Props['checked'] = undefined; | ||
export let disabled: Props['disabled'] = undefined; | ||
export let el: Props['el'] = undefined; | ||
export let includeInput: Props['includeInput'] = undefined; | ||
export let inputAttrs: Props['inputAttrs'] = undefined; | ||
export let name: Props['name'] = undefined; | ||
export let onCheckedChange: Events['onCheckedChange'] = undefined; | ||
export let required: Props['required'] = undefined; | ||
export let value: Props['value'] = undefined; | ||
$: attributes = $$restProps as Attributes; | ||
</script> | ||
|
||
<!-- <style lang="postcss"> | ||
</style> --> | ||
|
||
<SwitchPrimitive.Root | ||
{...attributes} | ||
asChild="{asChild}" | ||
class="{cn( | ||
'peer inline-flex h-[24px] w-[44px] shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors', | ||
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background', | ||
'disabled:cursor-not-allowed disabled:opacity-50', | ||
'data-[state=checked]:bg-primary', | ||
'data-[state=unchecked]:bg-input', | ||
attributes.class, | ||
)}" | ||
disabled="{disabled}" | ||
el="{el}" | ||
includeInput="{includeInput}" | ||
inputAttrs="{inputAttrs}" | ||
name="{name}" | ||
onCheckedChange="{onCheckedChange}" | ||
required="{required}" | ||
value="{value}" | ||
bind:checked="{checked}" | ||
let:builder | ||
on:click | ||
on:keydown | ||
> | ||
<slot builder="{builder}"> | ||
<SwitchPrimitive.Thumb | ||
class="{cn( | ||
'pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform', | ||
'data-[state=checked]:translate-x-5', | ||
'data-[state=unchecked]:translate-x-0', | ||
)}" | ||
/> | ||
</slot> | ||
</SwitchPrimitive.Root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, type Attributes, type Events, type Props, type Slots } from './Switch.svelte'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<script context="module" lang="ts"> | ||
import { derived } from 'svelte/store'; | ||
import { goto } from '$app/navigation'; | ||
import { page } from '$app/stores'; | ||
import Label from '$lib/components/label/index.js'; | ||
import Switch from '$lib/components/switch/index.js'; | ||
const disabledKey = 'disabled'; | ||
</script> | ||
|
||
<script lang="ts"> | ||
const disabled = derived(page, ($page) => $page.url.searchParams.has(disabledKey)); | ||
</script> | ||
|
||
<!-- <style lang="postcss"> | ||
</style> --> | ||
|
||
<div data-form> | ||
<Label for="{disabledKey}">Disabled</Label> | ||
|
||
<Switch | ||
checked="{$disabled}" | ||
id="{disabledKey}" | ||
name="{disabledKey}" | ||
onCheckedChange="{(checked) => { | ||
const url = new URL($page.url); | ||
|
||
if (checked) { | ||
url.searchParams.set(disabledKey, ''); | ||
} else { | ||
url.searchParams.delete(disabledKey); | ||
} | ||
|
||
goto(url); | ||
}}" | ||
/> | ||
</div> | ||
|
||
<hr class="my-4 border-y border-border" /> | ||
|
||
<Switch disabled="{$disabled}" /> |