INIT
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
<script lang="ts">
|
||||
import HiddenInput from "../../utilities/hidden-input.svelte";
|
||||
import { SwitchInputState } from "../switch.svelte.js";
|
||||
|
||||
const inputState = SwitchInputState.create();
|
||||
</script>
|
||||
|
||||
{#if inputState.shouldRender}
|
||||
<HiddenInput {...inputState.props} />
|
||||
{/if}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
||||
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
||||
$$bindings?: Bindings;
|
||||
} & Exports;
|
||||
(internal: unknown, props: {
|
||||
$$events?: Events;
|
||||
$$slots?: Slots;
|
||||
}): Exports & {
|
||||
$set?: any;
|
||||
$on?: any;
|
||||
};
|
||||
z_$$bindings?: Bindings;
|
||||
}
|
||||
declare const SwitchInput: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
||||
[evt: string]: CustomEvent<any>;
|
||||
}, {}, {}, string>;
|
||||
type SwitchInput = InstanceType<typeof SwitchInput>;
|
||||
export default SwitchInput;
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<script lang="ts">
|
||||
import { boxWith, mergeProps } from "svelte-toolbelt";
|
||||
import type { SwitchThumbProps } from "../types.js";
|
||||
import { SwitchThumbState } from "../switch.svelte.js";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
child,
|
||||
children,
|
||||
ref = $bindable(null),
|
||||
id = createId(uid),
|
||||
...restProps
|
||||
}: SwitchThumbProps = $props();
|
||||
|
||||
const thumbState = SwitchThumbState.create({
|
||||
id: boxWith(() => id),
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, thumbState.props));
|
||||
</script>
|
||||
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps, ...thumbState.snippetProps })}
|
||||
{:else}
|
||||
<span {...mergedProps}>
|
||||
{@render children?.(thumbState.snippetProps)}
|
||||
</span>
|
||||
{/if}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { SwitchThumbProps } from "../types.js";
|
||||
declare const SwitchThumb: import("svelte").Component<SwitchThumbProps, {}, "ref">;
|
||||
type SwitchThumb = ReturnType<typeof SwitchThumb>;
|
||||
export default SwitchThumb;
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
<script lang="ts">
|
||||
import { boxWith, mergeProps } from "svelte-toolbelt";
|
||||
import type { SwitchRootProps } from "../types.js";
|
||||
import { SwitchRootState } from "../switch.svelte.js";
|
||||
import SwitchInput from "./switch-input.svelte";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
import { noop } from "../../../internal/noop.js";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
child,
|
||||
children,
|
||||
ref = $bindable(null),
|
||||
id = createId(uid),
|
||||
disabled = false,
|
||||
required = false,
|
||||
checked = $bindable(false),
|
||||
value = "on",
|
||||
name = undefined,
|
||||
type = "button",
|
||||
onCheckedChange = noop,
|
||||
...restProps
|
||||
}: SwitchRootProps = $props();
|
||||
|
||||
const rootState = SwitchRootState.create({
|
||||
checked: boxWith(
|
||||
() => checked,
|
||||
(v) => {
|
||||
checked = v;
|
||||
onCheckedChange?.(v);
|
||||
}
|
||||
),
|
||||
disabled: boxWith(() => disabled ?? false),
|
||||
required: boxWith(() => required),
|
||||
value: boxWith(() => value),
|
||||
name: boxWith(() => name),
|
||||
id: boxWith(() => id),
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, rootState.props, { type }));
|
||||
</script>
|
||||
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps, ...rootState.snippetProps })}
|
||||
{:else}
|
||||
<button {...mergedProps}>
|
||||
{@render children?.(rootState.snippetProps)}
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
<SwitchInput />
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { SwitchRootProps } from "../types.js";
|
||||
declare const Switch: import("svelte").Component<SwitchRootProps, {}, "checked" | "ref">;
|
||||
type Switch = ReturnType<typeof Switch>;
|
||||
export default Switch;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { default as Root } from "./components/switch.svelte";
|
||||
export { default as Thumb } from "./components/switch-thumb.svelte";
|
||||
export type { SwitchRootProps as RootProps, SwitchThumbProps as ThumbProps } from "./types.js";
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { default as Root } from "./components/switch.svelte";
|
||||
export { default as Thumb } from "./components/switch-thumb.svelte";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as Switch from "./exports.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as Switch from "./exports.js";
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
import { type ReadableBoxedValues, type WritableBoxedValues } from "svelte-toolbelt";
|
||||
import type { BitsKeyboardEvent, BitsPointerEvent, RefAttachment, WithRefOpts } from "../../internal/types.js";
|
||||
interface SwitchRootStateOpts extends WithRefOpts, ReadableBoxedValues<{
|
||||
disabled: boolean;
|
||||
required: boolean;
|
||||
name: string | undefined;
|
||||
value: string;
|
||||
}>, WritableBoxedValues<{
|
||||
checked: boolean;
|
||||
}> {
|
||||
}
|
||||
export declare class SwitchRootState {
|
||||
#private;
|
||||
static create(opts: SwitchRootStateOpts): SwitchRootState;
|
||||
readonly opts: SwitchRootStateOpts;
|
||||
readonly attachment: RefAttachment;
|
||||
constructor(opts: SwitchRootStateOpts);
|
||||
onkeydown(e: BitsKeyboardEvent): void;
|
||||
onclick(_: BitsPointerEvent): void;
|
||||
readonly sharedProps: {
|
||||
"data-disabled": "" | undefined;
|
||||
"data-state": "checked" | "unchecked";
|
||||
"data-required": "" | undefined;
|
||||
};
|
||||
readonly snippetProps: {
|
||||
checked: boolean;
|
||||
};
|
||||
readonly props: {
|
||||
readonly id: string;
|
||||
readonly role: "switch";
|
||||
readonly disabled: true | undefined;
|
||||
readonly "aria-checked": "true" | "false" | "mixed";
|
||||
readonly "aria-required": "true" | "false";
|
||||
readonly onclick: (_: BitsPointerEvent) => void;
|
||||
readonly onkeydown: (e: BitsKeyboardEvent) => void;
|
||||
readonly "data-disabled": "" | undefined;
|
||||
readonly "data-state": "checked" | "unchecked";
|
||||
readonly "data-required": "" | undefined;
|
||||
};
|
||||
}
|
||||
export declare class SwitchInputState {
|
||||
static create(): SwitchInputState;
|
||||
readonly root: SwitchRootState;
|
||||
readonly shouldRender: boolean;
|
||||
constructor(root: SwitchRootState);
|
||||
readonly props: {
|
||||
readonly type: "checkbox";
|
||||
readonly name: string | undefined;
|
||||
readonly value: string;
|
||||
readonly checked: boolean;
|
||||
readonly disabled: boolean;
|
||||
readonly required: boolean;
|
||||
};
|
||||
}
|
||||
interface SwitchThumbStateOpts extends WithRefOpts {
|
||||
}
|
||||
export declare class SwitchThumbState {
|
||||
static create(opts: SwitchThumbStateOpts): SwitchThumbState;
|
||||
readonly opts: SwitchThumbStateOpts;
|
||||
readonly root: SwitchRootState;
|
||||
readonly attachment: RefAttachment;
|
||||
constructor(opts: SwitchThumbStateOpts, root: SwitchRootState);
|
||||
readonly snippetProps: {
|
||||
checked: boolean;
|
||||
};
|
||||
readonly props: {
|
||||
readonly id: string;
|
||||
readonly "data-disabled": "" | undefined;
|
||||
readonly "data-state": "checked" | "unchecked";
|
||||
readonly "data-required": "" | undefined;
|
||||
};
|
||||
}
|
||||
export {};
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
import { attachRef } from "svelte-toolbelt";
|
||||
import { Context } from "runed";
|
||||
import { getAriaChecked, boolToStr, getDataChecked, boolToTrueOrUndef, createBitsAttrs, boolToEmptyStrOrUndef, } from "../../internal/attrs.js";
|
||||
import { kbd } from "../../internal/kbd.js";
|
||||
const switchAttrs = createBitsAttrs({
|
||||
component: "switch",
|
||||
parts: ["root", "thumb"],
|
||||
});
|
||||
const SwitchRootContext = new Context("Switch.Root");
|
||||
export class SwitchRootState {
|
||||
static create(opts) {
|
||||
return SwitchRootContext.set(new SwitchRootState(opts));
|
||||
}
|
||||
opts;
|
||||
attachment;
|
||||
constructor(opts) {
|
||||
this.opts = opts;
|
||||
this.attachment = attachRef(opts.ref);
|
||||
this.onkeydown = this.onkeydown.bind(this);
|
||||
this.onclick = this.onclick.bind(this);
|
||||
}
|
||||
#toggle() {
|
||||
this.opts.checked.current = !this.opts.checked.current;
|
||||
}
|
||||
onkeydown(e) {
|
||||
if (!(e.key === kbd.ENTER || e.key === kbd.SPACE) || this.opts.disabled.current)
|
||||
return;
|
||||
e.preventDefault();
|
||||
this.#toggle();
|
||||
}
|
||||
onclick(_) {
|
||||
if (this.opts.disabled.current)
|
||||
return;
|
||||
this.#toggle();
|
||||
}
|
||||
sharedProps = $derived.by(() => ({
|
||||
"data-disabled": boolToEmptyStrOrUndef(this.opts.disabled.current),
|
||||
"data-state": getDataChecked(this.opts.checked.current),
|
||||
"data-required": boolToEmptyStrOrUndef(this.opts.required.current),
|
||||
}));
|
||||
snippetProps = $derived.by(() => ({
|
||||
checked: this.opts.checked.current,
|
||||
}));
|
||||
props = $derived.by(() => ({
|
||||
...this.sharedProps,
|
||||
id: this.opts.id.current,
|
||||
role: "switch",
|
||||
disabled: boolToTrueOrUndef(this.opts.disabled.current),
|
||||
"aria-checked": getAriaChecked(this.opts.checked.current, false),
|
||||
"aria-required": boolToStr(this.opts.required.current),
|
||||
[switchAttrs.root]: "",
|
||||
//
|
||||
onclick: this.onclick,
|
||||
onkeydown: this.onkeydown,
|
||||
...this.attachment,
|
||||
}));
|
||||
}
|
||||
export class SwitchInputState {
|
||||
static create() {
|
||||
return new SwitchInputState(SwitchRootContext.get());
|
||||
}
|
||||
root;
|
||||
shouldRender = $derived.by(() => this.root.opts.name.current !== undefined);
|
||||
constructor(root) {
|
||||
this.root = root;
|
||||
}
|
||||
props = $derived.by(() => ({
|
||||
type: "checkbox",
|
||||
name: this.root.opts.name.current,
|
||||
value: this.root.opts.value.current,
|
||||
checked: this.root.opts.checked.current,
|
||||
disabled: this.root.opts.disabled.current,
|
||||
required: this.root.opts.required.current,
|
||||
}));
|
||||
}
|
||||
export class SwitchThumbState {
|
||||
static create(opts) {
|
||||
return new SwitchThumbState(opts, SwitchRootContext.get());
|
||||
}
|
||||
opts;
|
||||
root;
|
||||
attachment;
|
||||
constructor(opts, root) {
|
||||
this.opts = opts;
|
||||
this.root = root;
|
||||
this.attachment = attachRef(opts.ref);
|
||||
}
|
||||
snippetProps = $derived.by(() => ({
|
||||
checked: this.root.opts.checked.current,
|
||||
}));
|
||||
props = $derived.by(() => ({
|
||||
...this.root.sharedProps,
|
||||
id: this.opts.id.current,
|
||||
[switchAttrs.thumb]: "",
|
||||
...this.attachment,
|
||||
}));
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
import type { OnChangeFn, WithChild, Without } from "../../internal/types.js";
|
||||
import type { BitsPrimitiveButtonAttributes, BitsPrimitiveSpanAttributes } from "../../shared/attributes.js";
|
||||
type SwitchRootSnippetProps = {
|
||||
checked: boolean;
|
||||
};
|
||||
export type SwitchRootPropsWithoutHTML = WithChild<{
|
||||
/**
|
||||
* Whether the switch is disabled.
|
||||
*
|
||||
* @defaultValue false
|
||||
*/
|
||||
disabled?: boolean | null | undefined;
|
||||
/**
|
||||
* Whether the switch is required (for form validation).
|
||||
*
|
||||
* @defaultValue false
|
||||
*/
|
||||
required?: boolean;
|
||||
/**
|
||||
* The name of the switch used in form submission.
|
||||
* If not provided, the hidden input will not be rendered.
|
||||
*
|
||||
* @defaultValue undefined
|
||||
*/
|
||||
name?: string;
|
||||
/**
|
||||
* The value of the switch used in form submission.
|
||||
*
|
||||
* @defaultValue undefined
|
||||
*/
|
||||
value?: any;
|
||||
/**
|
||||
* The checked state of the switch.
|
||||
*
|
||||
* @defaultValue false
|
||||
*/
|
||||
checked?: boolean;
|
||||
/**
|
||||
* A callback function called when the checked state changes.
|
||||
*/
|
||||
onCheckedChange?: OnChangeFn<boolean>;
|
||||
}, SwitchRootSnippetProps>;
|
||||
export type SwitchRootProps = SwitchRootPropsWithoutHTML & Without<BitsPrimitiveButtonAttributes, SwitchRootPropsWithoutHTML>;
|
||||
export type SwitchThumbSnippetProps = SwitchRootSnippetProps;
|
||||
export type SwitchThumbPropsWithoutHTML = WithChild<{}, SwitchThumbSnippetProps>;
|
||||
export type SwitchThumbProps = SwitchThumbPropsWithoutHTML & Without<BitsPrimitiveSpanAttributes, SwitchThumbPropsWithoutHTML>;
|
||||
export {};
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in New Issue
Block a user