This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
<script lang="ts">
|
||||
import { boxWith, mergeProps } from "svelte-toolbelt";
|
||||
import type { ToggleGroupItemProps } from "../types.js";
|
||||
import { ToggleGroupItemState } from "../toggle-group.svelte.js";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
children,
|
||||
child,
|
||||
ref = $bindable(null),
|
||||
value,
|
||||
disabled = false,
|
||||
id = createId(uid),
|
||||
type = "button",
|
||||
...restProps
|
||||
}: ToggleGroupItemProps = $props();
|
||||
|
||||
const itemState = ToggleGroupItemState.create({
|
||||
id: boxWith(() => id),
|
||||
value: boxWith(() => value),
|
||||
disabled: boxWith(() => disabled ?? false),
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, itemState.props, { type }));
|
||||
</script>
|
||||
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps, ...itemState.snippetProps })}
|
||||
{:else}
|
||||
<button {...mergedProps}>
|
||||
{@render children?.(itemState.snippetProps)}
|
||||
</button>
|
||||
{/if}
|
||||
Generated
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
import type { ToggleGroupItemProps } from "../types.js";
|
||||
declare const ToggleGroupItem: import("svelte").Component<ToggleGroupItemProps, {}, "ref">;
|
||||
type ToggleGroupItem = ReturnType<typeof ToggleGroupItem>;
|
||||
export default ToggleGroupItem;
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
<script lang="ts">
|
||||
import { type WritableBox, boxWith } from "svelte-toolbelt";
|
||||
import { mergeProps } from "svelte-toolbelt";
|
||||
import type { ToggleGroupRootProps } from "../types.js";
|
||||
import { ToggleGroupRootState } from "../toggle-group.svelte.js";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
import { noop } from "../../../internal/noop.js";
|
||||
import { watch } from "runed";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
id = createId(uid),
|
||||
ref = $bindable(null),
|
||||
value = $bindable(),
|
||||
onValueChange = noop,
|
||||
type,
|
||||
disabled = false,
|
||||
loop = true,
|
||||
orientation = "horizontal",
|
||||
rovingFocus = true,
|
||||
child,
|
||||
children,
|
||||
...restProps
|
||||
}: ToggleGroupRootProps = $props();
|
||||
|
||||
function handleDefaultValue() {
|
||||
if (value !== undefined) return;
|
||||
value = type === "single" ? "" : [];
|
||||
}
|
||||
|
||||
// SSR
|
||||
handleDefaultValue();
|
||||
|
||||
watch.pre(
|
||||
() => value,
|
||||
() => {
|
||||
handleDefaultValue();
|
||||
}
|
||||
);
|
||||
|
||||
const rootState = ToggleGroupRootState.create({
|
||||
id: boxWith(() => id),
|
||||
value: boxWith(
|
||||
() => value!,
|
||||
(v) => {
|
||||
value = v;
|
||||
// @ts-expect-error - we know
|
||||
onValueChange(v);
|
||||
}
|
||||
) as WritableBox<string> | WritableBox<string[]>,
|
||||
disabled: boxWith(() => disabled),
|
||||
loop: boxWith(() => loop),
|
||||
orientation: boxWith(() => orientation),
|
||||
rovingFocus: boxWith(() => rovingFocus),
|
||||
type,
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, rootState.props));
|
||||
</script>
|
||||
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps })}
|
||||
{:else}
|
||||
<div {...mergedProps}>
|
||||
{@render children?.()}
|
||||
</div>
|
||||
{/if}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { ToggleGroupRootProps } from "../types.js";
|
||||
declare const ToggleGroup: import("svelte").Component<ToggleGroupRootProps, {}, "value" | "ref">;
|
||||
type ToggleGroup = ReturnType<typeof ToggleGroup>;
|
||||
export default ToggleGroup;
|
||||
Reference in New Issue
Block a user