This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
<script lang="ts">
|
||||
import { boxWith, mergeProps } from "svelte-toolbelt";
|
||||
import type { ToggleRootProps } from "../types.js";
|
||||
import { ToggleRootState } from "../toggle.svelte.js";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
import { noop } from "../../../internal/noop.js";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
ref = $bindable(null),
|
||||
id = createId(uid),
|
||||
pressed = $bindable(false),
|
||||
onPressedChange = noop,
|
||||
disabled = false,
|
||||
type = "button",
|
||||
children,
|
||||
child,
|
||||
...restProps
|
||||
}: ToggleRootProps = $props();
|
||||
|
||||
const toggleState = ToggleRootState.create({
|
||||
pressed: boxWith(
|
||||
() => pressed,
|
||||
(v) => {
|
||||
pressed = v;
|
||||
onPressedChange(v);
|
||||
}
|
||||
),
|
||||
disabled: boxWith(() => disabled ?? false),
|
||||
id: boxWith(() => id),
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, toggleState.props, { type }));
|
||||
</script>
|
||||
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps, ...toggleState.snippetProps })}
|
||||
{:else}
|
||||
<button {...mergedProps}>
|
||||
{@render children?.(toggleState.snippetProps)}
|
||||
</button>
|
||||
{/if}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { ToggleRootProps } from "../types.js";
|
||||
declare const Toggle: import("svelte").Component<ToggleRootProps, {}, "ref" | "pressed">;
|
||||
type Toggle = ReturnType<typeof Toggle>;
|
||||
export default Toggle;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { default as Root } from "./components/toggle.svelte";
|
||||
export type { ToggleRootProps as RootProps } from "./types.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { default as Root } from "./components/toggle.svelte";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as Toggle from "./exports.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as Toggle from "./exports.js";
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { type ReadableBoxedValues, type WritableBoxedValues } from "svelte-toolbelt";
|
||||
import type { BitsMouseEvent, RefAttachment, WithRefOpts } from "../../internal/types.js";
|
||||
export declare const toggleAttrs: import("../../internal/attrs.js").CreateBitsAttrsReturn<readonly ["root"]>;
|
||||
interface ToggleRootStateOpts extends WithRefOpts, ReadableBoxedValues<{
|
||||
disabled: boolean;
|
||||
}>, WritableBoxedValues<{
|
||||
pressed: boolean;
|
||||
}> {
|
||||
}
|
||||
export declare class ToggleRootState {
|
||||
static create(opts: ToggleRootStateOpts): ToggleRootState;
|
||||
readonly opts: ToggleRootStateOpts;
|
||||
readonly attachment: RefAttachment;
|
||||
constructor(opts: ToggleRootStateOpts);
|
||||
onclick(_: BitsMouseEvent): void;
|
||||
readonly snippetProps: {
|
||||
pressed: boolean;
|
||||
};
|
||||
readonly props: {
|
||||
readonly id: string;
|
||||
readonly "data-disabled": "" | undefined;
|
||||
readonly "aria-pressed": "true" | "false";
|
||||
readonly "data-state": "off" | "on";
|
||||
readonly disabled: true | undefined;
|
||||
readonly onclick: (_: BitsMouseEvent) => void;
|
||||
};
|
||||
}
|
||||
export declare function getToggleDataState(condition: boolean): "on" | "off";
|
||||
export {};
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
import { attachRef } from "svelte-toolbelt";
|
||||
import { createBitsAttrs, boolToStr, boolToEmptyStrOrUndef, boolToTrueOrUndef, } from "../../internal/attrs.js";
|
||||
export const toggleAttrs = createBitsAttrs({
|
||||
component: "toggle",
|
||||
parts: ["root"],
|
||||
});
|
||||
export class ToggleRootState {
|
||||
static create(opts) {
|
||||
return new ToggleRootState(opts);
|
||||
}
|
||||
opts;
|
||||
attachment;
|
||||
constructor(opts) {
|
||||
this.opts = opts;
|
||||
this.attachment = attachRef(this.opts.ref);
|
||||
this.onclick = this.onclick.bind(this);
|
||||
}
|
||||
onclick(_) {
|
||||
if (this.opts.disabled.current)
|
||||
return;
|
||||
this.opts.pressed.current = !this.opts.pressed.current;
|
||||
}
|
||||
snippetProps = $derived.by(() => ({
|
||||
pressed: this.opts.pressed.current,
|
||||
}));
|
||||
props = $derived.by(() => ({
|
||||
[toggleAttrs.root]: "",
|
||||
id: this.opts.id.current,
|
||||
"data-disabled": boolToEmptyStrOrUndef(this.opts.disabled.current),
|
||||
"aria-pressed": boolToStr(this.opts.pressed.current),
|
||||
"data-state": getToggleDataState(this.opts.pressed.current),
|
||||
disabled: boolToTrueOrUndef(this.opts.disabled.current),
|
||||
onclick: this.onclick,
|
||||
...this.attachment,
|
||||
}));
|
||||
}
|
||||
export function getToggleDataState(condition) {
|
||||
return condition ? "on" : "off";
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
import type { OnChangeFn, WithChild, Without } from "../../internal/types.js";
|
||||
import type { BitsPrimitiveButtonAttributes } from "../../shared/attributes.js";
|
||||
export type ToggleRootSnippetProps = {
|
||||
pressed: boolean;
|
||||
};
|
||||
export type ToggleRootPropsWithoutHTML = WithChild<{
|
||||
/**
|
||||
* Whether the toggle is pressed or not.
|
||||
*
|
||||
* @defaultValue false
|
||||
*/
|
||||
pressed?: boolean | undefined;
|
||||
/**
|
||||
* A callback function called when the toggle is pressed.
|
||||
*/
|
||||
onPressedChange?: OnChangeFn<boolean>;
|
||||
/**
|
||||
* Whether the toggle is disabled or not.
|
||||
*
|
||||
* @defaultValue false
|
||||
*/
|
||||
disabled?: boolean | null | undefined;
|
||||
}, ToggleRootSnippetProps>;
|
||||
export type ToggleRootProps = ToggleRootPropsWithoutHTML & Without<BitsPrimitiveButtonAttributes, ToggleRootPropsWithoutHTML>;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in New Issue
Block a user