INIT
CI / build (push) Has been skipped
CI / deploy (push) Successful in 1m11s

This commit is contained in:
eewing
2026-02-18 15:17:47 -06:00
parent e74c106f85
commit ec317eb17c
11532 changed files with 1631690 additions and 1 deletions
@@ -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}
@@ -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;
@@ -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}
@@ -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;
+3
View File
@@ -0,0 +1,3 @@
export { default as Root } from "./components/toggle-group.svelte";
export { default as Item } from "./components/toggle-group-item.svelte";
export type { ToggleGroupRootProps as RootProps, ToggleGroupItemProps as ItemProps, } from "./types.js";
+2
View File
@@ -0,0 +1,2 @@
export { default as Root } from "./components/toggle-group.svelte";
export { default as Item } from "./components/toggle-group-item.svelte";
+1
View File
@@ -0,0 +1 @@
export * as ToggleGroup from "./exports.js";
+1
View File
@@ -0,0 +1 @@
export * as ToggleGroup from "./exports.js";
+95
View File
@@ -0,0 +1,95 @@
import { type WritableBox, type ReadableBoxedValues, type WritableBoxedValues } from "svelte-toolbelt";
import type { Orientation } from "../../shared/index.js";
import type { BitsKeyboardEvent, BitsMouseEvent, RefAttachment, WithRefOpts } from "../../internal/types.js";
import { RovingFocusGroup } from "../../internal/roving-focus-group.js";
export declare const toggleGroupAttrs: import("../../internal/attrs.js").CreateBitsAttrsReturn<readonly ["root", "item"]>;
interface ToggleGroupBaseStateOpts extends WithRefOpts, ReadableBoxedValues<{
disabled: boolean;
rovingFocus: boolean;
loop: boolean;
orientation: Orientation;
}> {
}
declare abstract class ToggleGroupBaseState {
readonly opts: ToggleGroupBaseStateOpts;
readonly rovingFocusGroup: RovingFocusGroup;
readonly attachment: RefAttachment;
constructor(opts: ToggleGroupBaseStateOpts);
readonly props: {
readonly id: string;
readonly role: "group";
readonly "data-orientation": Orientation;
readonly "data-disabled": "" | undefined;
};
}
interface ToggleGroupSingleStateOpts extends ToggleGroupBaseStateOpts, WritableBoxedValues<{
value: string;
}> {
}
declare class ToggleGroupSingleState extends ToggleGroupBaseState {
readonly opts: ToggleGroupSingleStateOpts;
isMulti: boolean;
readonly anyPressed: boolean;
constructor(opts: ToggleGroupSingleStateOpts);
includesItem(item: string): boolean;
toggleItem(item: string, id: string): void;
}
interface ToggleGroupMultipleStateOpts extends ToggleGroupBaseStateOpts, WritableBoxedValues<{
value: string[];
}> {
}
declare class ToggleGroupMultipleState extends ToggleGroupBaseState {
readonly opts: ToggleGroupMultipleStateOpts;
isMulti: boolean;
readonly anyPressed: boolean;
constructor(opts: ToggleGroupMultipleStateOpts);
includesItem(item: string): boolean;
toggleItem(item: string, id: string): void;
}
type ToggleGroup = ToggleGroupSingleState | ToggleGroupMultipleState;
interface ToggleGroupRootOpts extends WithRefOpts, ReadableBoxedValues<{
disabled: boolean;
rovingFocus: boolean;
loop: boolean;
orientation: Orientation;
}> {
type: "single" | "multiple";
value: WritableBox<string> | WritableBox<string[]>;
}
export declare class ToggleGroupRootState {
static create(opts: ToggleGroupRootOpts): ToggleGroup;
}
interface ToggleGroupItemStateOpts extends WithRefOpts, ReadableBoxedValues<{
value: string;
disabled: boolean;
}> {
}
export declare class ToggleGroupItemState {
#private;
static create(opts: ToggleGroupItemStateOpts): ToggleGroupItemState;
readonly opts: ToggleGroupItemStateOpts;
readonly root: ToggleGroup;
readonly attachment: RefAttachment;
readonly isPressed: boolean;
constructor(opts: ToggleGroupItemStateOpts, root: ToggleGroup);
onclick(_: BitsMouseEvent): void;
onkeydown(e: BitsKeyboardEvent): void;
readonly snippetProps: {
pressed: boolean;
};
readonly props: {
readonly id: string;
readonly role: "radio" | undefined;
readonly tabindex: number;
readonly "data-orientation": Orientation;
readonly "data-disabled": "" | undefined;
readonly "data-state": "off" | "on";
readonly "data-value": string;
readonly "aria-pressed": "true" | "false" | undefined;
readonly "aria-checked": "true" | "false" | "mixed" | undefined;
readonly disabled: true | undefined;
readonly onclick: (_: BitsMouseEvent) => void;
readonly onkeydown: (e: BitsKeyboardEvent) => void;
};
}
export {};
+161
View File
@@ -0,0 +1,161 @@
import { attachRef, } from "svelte-toolbelt";
import { Context } from "runed";
import { createBitsAttrs, getAriaChecked, boolToStr, boolToEmptyStrOrUndef, boolToTrueOrUndef, } from "../../internal/attrs.js";
import { kbd } from "../../internal/kbd.js";
import { RovingFocusGroup } from "../../internal/roving-focus-group.js";
export const toggleGroupAttrs = createBitsAttrs({
component: "toggle-group",
parts: ["root", "item"],
});
const ToggleGroupRootContext = new Context("ToggleGroup.Root");
class ToggleGroupBaseState {
opts;
rovingFocusGroup;
attachment;
constructor(opts) {
this.opts = opts;
this.attachment = attachRef(this.opts.ref);
this.rovingFocusGroup = new RovingFocusGroup({
candidateAttr: toggleGroupAttrs.item,
rootNode: opts.ref,
loop: opts.loop,
orientation: opts.orientation,
});
}
props = $derived.by(() => ({
id: this.opts.id.current,
[toggleGroupAttrs.root]: "",
role: "group",
"data-orientation": this.opts.orientation.current,
"data-disabled": boolToEmptyStrOrUndef(this.opts.disabled.current),
...this.attachment,
}));
}
class ToggleGroupSingleState extends ToggleGroupBaseState {
opts;
isMulti = false;
anyPressed = $derived.by(() => this.opts.value.current !== "");
constructor(opts) {
super(opts);
this.opts = opts;
}
includesItem(item) {
return this.opts.value.current === item;
}
toggleItem(item, id) {
if (this.includesItem(item)) {
this.opts.value.current = "";
}
else {
this.opts.value.current = item;
this.rovingFocusGroup.setCurrentTabStopId(id);
}
}
}
class ToggleGroupMultipleState extends ToggleGroupBaseState {
opts;
isMulti = true;
anyPressed = $derived.by(() => this.opts.value.current.length > 0);
constructor(opts) {
super(opts);
this.opts = opts;
}
includesItem(item) {
return this.opts.value.current.includes(item);
}
toggleItem(item, id) {
if (this.includesItem(item)) {
this.opts.value.current = this.opts.value.current.filter((v) => v !== item);
}
else {
this.opts.value.current = [...this.opts.value.current, item];
this.rovingFocusGroup.setCurrentTabStopId(id);
}
}
}
export class ToggleGroupRootState {
static create(opts) {
const { type, ...rest } = opts;
const rootState = type === "single"
? new ToggleGroupSingleState(rest)
: new ToggleGroupMultipleState(rest);
return ToggleGroupRootContext.set(rootState);
}
}
export class ToggleGroupItemState {
static create(opts) {
return new ToggleGroupItemState(opts, ToggleGroupRootContext.get());
}
opts;
root;
attachment;
#isDisabled = $derived.by(() => this.opts.disabled.current || this.root.opts.disabled.current);
isPressed = $derived.by(() => this.root.includesItem(this.opts.value.current));
#ariaChecked = $derived.by(() => {
return this.root.isMulti ? undefined : getAriaChecked(this.isPressed, false);
});
#ariaPressed = $derived.by(() => {
return this.root.isMulti ? boolToStr(this.isPressed) : undefined;
});
constructor(opts, root) {
this.opts = opts;
this.root = root;
this.attachment = attachRef(this.opts.ref);
$effect(() => {
if (!this.root.opts.rovingFocus.current) {
this.#tabIndex = 0;
}
else {
this.#tabIndex = this.root.rovingFocusGroup.getTabIndex(this.opts.ref.current);
}
});
this.onclick = this.onclick.bind(this);
this.onkeydown = this.onkeydown.bind(this);
}
#toggleItem() {
if (this.#isDisabled)
return;
this.root.toggleItem(this.opts.value.current, this.opts.id.current);
}
onclick(_) {
if (this.#isDisabled)
return;
this.root.toggleItem(this.opts.value.current, this.opts.id.current);
}
onkeydown(e) {
if (this.#isDisabled)
return;
if (e.key === kbd.ENTER || e.key === kbd.SPACE) {
e.preventDefault();
this.#toggleItem();
return;
}
if (!this.root.opts.rovingFocus.current)
return;
this.root.rovingFocusGroup.handleKeydown(this.opts.ref.current, e);
}
#tabIndex = $state(0);
snippetProps = $derived.by(() => ({
pressed: this.isPressed,
}));
props = $derived.by(() => ({
id: this.opts.id.current,
role: this.root.isMulti ? undefined : "radio",
tabindex: this.#tabIndex,
"data-orientation": this.root.opts.orientation.current,
"data-disabled": boolToEmptyStrOrUndef(this.#isDisabled),
"data-state": getToggleItemDataState(this.isPressed),
"data-value": this.opts.value.current,
"aria-pressed": this.#ariaPressed,
"aria-checked": this.#ariaChecked,
disabled: boolToTrueOrUndef(this.#isDisabled),
[toggleGroupAttrs.item]: "",
//
onclick: this.onclick,
onkeydown: this.onkeydown,
...this.attachment,
}));
}
function getToggleItemDataState(condition) {
return condition ? "on" : "off";
}
+61
View File
@@ -0,0 +1,61 @@
import type { OnChangeFn, WithChild, Without } from "../../internal/types.js";
import type { BitsPrimitiveButtonAttributes, BitsPrimitiveDivAttributes } from "../../shared/attributes.js";
import type { Orientation } from "../../index.js";
export type BaseToggleGroupRootProps = {
/**
* Whether the toggle group is disabled or not.
*
* @defaultValue false
*/
disabled?: boolean;
/**
* Whether to loop through the items when reaching the end
* when using the keyboard.
*
* @defaultValue true
*/
loop?: boolean;
/**
* The orientation of the toggle group. Used to handle keyboard
* navigation between items.
*
* @defaultValue 'horizontal'
*/
orientation?: Orientation;
/**
* Whether to enable roving focus or not. When enabled, users
* navigate between the items using the arrow keys. When disabled,
* users navigate between the items using the tab key.
*/
rovingFocus?: boolean;
};
export type SingleToggleGroupRootPropsWithoutHTML = WithChild<BaseToggleGroupRootProps & {
type: "single";
value?: string;
onValueChange?: OnChangeFn<string>;
}>;
export type SingleToggleGroupRootProps = SingleToggleGroupRootPropsWithoutHTML & Without<BitsPrimitiveDivAttributes, SingleToggleGroupRootPropsWithoutHTML>;
export type MultipleToggleGroupRootPropsWithoutHTML = WithChild<BaseToggleGroupRootProps> & {
type: "multiple";
value?: string[];
onValueChange?: OnChangeFn<string[]>;
};
export type MultipleToggleGroupRootProps = MultipleToggleGroupRootPropsWithoutHTML & Without<BitsPrimitiveDivAttributes, MultipleToggleGroupRootPropsWithoutHTML>;
export type ToggleGroupRootPropsWithoutHTML = SingleToggleGroupRootPropsWithoutHTML | MultipleToggleGroupRootPropsWithoutHTML;
export type ToggleGroupRootProps = ToggleGroupRootPropsWithoutHTML & Without<BitsPrimitiveDivAttributes, ToggleGroupRootPropsWithoutHTML>;
export type ToggleGroupItemSnippetProps = {
pressed: boolean;
};
export type ToggleGroupItemPropsWithoutHTML = WithChild<{
/**
* Whether the toggle item is disabled or not.
*
* @defaultValue false
*/
disabled?: boolean | null | undefined;
/**
* The value of the toggle item.
*/
value: string;
}, ToggleGroupItemSnippetProps>;
export type ToggleGroupItemProps = ToggleGroupItemPropsWithoutHTML & Without<BitsPrimitiveButtonAttributes, ToggleGroupItemPropsWithoutHTML>;
+1
View File
@@ -0,0 +1 @@
export {};