ui-ingredients 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -81,6 +81,7 @@ npm install ui-ingredients
81
81
  - [TimePicker](https://github.com/calvo-jp/ui-ingredients/blob/main/docs/time-picker.md)
82
82
  - [Timer](https://github.com/calvo-jp/ui-ingredients/blob/main/docs/timer.md)
83
83
  - [Toast](https://github.com/calvo-jp/ui-ingredients/blob/main/docs/toast.md)
84
+ - [Toggle](https://github.com/calvo-jp/ui-ingredients/blob/main/docs/toggle.md)
84
85
  - [ToggleGroup](https://github.com/calvo-jp/ui-ingredients/blob/main/docs/toggle-group.md)
85
86
  - [Tooltip](https://github.com/calvo-jp/ui-ingredients/blob/main/docs/tooltip.md)
86
87
  - [TreeView](https://github.com/calvo-jp/ui-ingredients/blob/main/docs/tree-view.md)
package/dist/index.d.ts CHANGED
@@ -41,6 +41,7 @@ export * from './time-picker/index.js';
41
41
  export * from './timer/index.js';
42
42
  export * from './toast/index.js';
43
43
  export * from './toggle-group/index.js';
44
+ export * from './toggle/index.js';
44
45
  export * from './tooltip/index.js';
45
46
  export * from './tour/index.js';
46
47
  export * from './tree-view/index.js';
package/dist/index.js CHANGED
@@ -41,6 +41,7 @@ export * from './time-picker/index.js';
41
41
  export * from './timer/index.js';
42
42
  export * from './toast/index.js';
43
43
  export * from './toggle-group/index.js';
44
+ export * from './toggle/index.js';
44
45
  export * from './tooltip/index.js';
45
46
  export * from './tour/index.js';
46
47
  export * from './tree-view/index.js';
@@ -0,0 +1,2 @@
1
+ export declare const anatomy: import("@zag-js/anatomy").AnatomyInstance<"root">;
2
+ export declare const parts: Record<"root", import("@zag-js/anatomy").AnatomyPart>;
@@ -0,0 +1,3 @@
1
+ import { createAnatomy } from '@zag-js/anatomy';
2
+ export const anatomy = createAnatomy('toggle').parts('root');
3
+ export const parts = anatomy.build();
@@ -0,0 +1,16 @@
1
+ import type { HTMLButtonAttributes } from 'svelte/elements';
2
+ interface PressedChangeDetails {
3
+ pressed: boolean;
4
+ }
5
+ export interface CreateToggleProps {
6
+ disabled?: boolean;
7
+ pressed?: boolean;
8
+ onPressedChange?: (detail: PressedChangeDetails) => void;
9
+ }
10
+ export interface CreateToggleReturn {
11
+ pressed: boolean;
12
+ setPressed: (pressed: boolean) => void;
13
+ getRootProps: () => HTMLButtonAttributes;
14
+ }
15
+ export declare function createToggle(props: CreateToggleProps): any;
16
+ export {};
@@ -0,0 +1,30 @@
1
+ import { ariaAttr, dataAttr } from '@zag-js/dom-query';
2
+ import { reflect } from '@zag-js/svelte';
3
+ import { parts } from './anatomy.js';
4
+ export function createToggle(props) {
5
+ let pressed = $state(props.pressed ?? false);
6
+ function setPressed(value) {
7
+ props.onPressedChange?.({ pressed: value });
8
+ pressed = value;
9
+ }
10
+ function getRootProps() {
11
+ return {
12
+ disabled: props.disabled,
13
+ onclick() {
14
+ if (props.disabled)
15
+ return;
16
+ setPressed(!pressed);
17
+ },
18
+ 'aria-pressed': ariaAttr(pressed),
19
+ 'data-state': pressed ? 'on' : 'off',
20
+ 'data-pressed': dataAttr(pressed),
21
+ 'data-disabled': dataAttr(props.disabled),
22
+ ...parts.root.attrs,
23
+ };
24
+ }
25
+ return reflect(() => ({
26
+ pressed,
27
+ setPressed,
28
+ getRootProps,
29
+ }));
30
+ }
@@ -0,0 +1,3 @@
1
+ export * as Toggle from './toggle.js';
2
+ export type { ToggleProps } from './toggle-root.svelte';
3
+ export { anatomy as toggleAnatomy } from './anatomy.js';
@@ -0,0 +1,2 @@
1
+ export * as Toggle from './toggle.js';
2
+ export { anatomy as toggleAnatomy } from './anatomy.js';
@@ -0,0 +1,2 @@
1
+ import type { CreateToggleReturn } from './create-toggle.svelte.js';
2
+ export declare const getToggleContext: () => CreateToggleReturn, setToggleContext: (context: CreateToggleReturn | (() => CreateToggleReturn)) => void;
@@ -0,0 +1,2 @@
1
+ import { createContext } from '../create-context.svelte.js';
2
+ export const [getToggleContext, setToggleContext] = createContext('Toggle');
@@ -0,0 +1,50 @@
1
+ <script lang="ts" module>
2
+ import type {Assign, HtmlIngredientProps} from '../types.js';
3
+ import type {
4
+ CreateToggleProps,
5
+ CreateToggleReturn,
6
+ } from './create-toggle.svelte.js';
7
+
8
+ export interface ToggleProps
9
+ extends Assign<
10
+ HtmlIngredientProps<'button', HTMLButtonElement, CreateToggleReturn>,
11
+ CreateToggleProps
12
+ > {}
13
+ </script>
14
+
15
+ <script lang="ts">
16
+ import {mergeProps} from '../merge-props.js';
17
+ import {reflect} from '@zag-js/svelte';
18
+ import {createSplitProps} from '@zag-js/utils';
19
+ import {createToggle} from './create-toggle.svelte.js';
20
+ import {setToggleContext} from './toggle-context.svelte.js';
21
+
22
+ let {
23
+ ref = $bindable(null),
24
+ asChild,
25
+ children,
26
+ ...props
27
+ }: ToggleProps = $props();
28
+
29
+ let [createToggleProps, localProps] = $derived(
30
+ createSplitProps<CreateToggleProps>([
31
+ 'disabled',
32
+ 'pressed',
33
+ 'onPressedChange',
34
+ ])(props),
35
+ );
36
+
37
+ let toggle = createToggle(reflect(() => createToggleProps));
38
+
39
+ let mergedProps = $derived(mergeProps(toggle.getRootProps(), localProps));
40
+
41
+ setToggleContext(toggle);
42
+ </script>
43
+
44
+ {#if asChild}
45
+ {@render asChild(mergedProps, toggle)}
46
+ {:else}
47
+ <button bind:this={ref} {...mergedProps}>
48
+ {@render children?.(toggle)}
49
+ </button>
50
+ {/if}
@@ -0,0 +1,22 @@
1
+ import type { Assign, HtmlIngredientProps } from '../types.js';
2
+ import type { CreateToggleProps, CreateToggleReturn } from './create-toggle.svelte.js';
3
+ export interface ToggleProps extends Assign<HtmlIngredientProps<'button', HTMLButtonElement, CreateToggleReturn>, CreateToggleProps> {
4
+ }
5
+ 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> {
6
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
7
+ $$bindings?: Bindings;
8
+ } & Exports;
9
+ (internal: unknown, props: Props & {
10
+ $$events?: Events;
11
+ $$slots?: Slots;
12
+ }): Exports & {
13
+ $set?: any;
14
+ $on?: any;
15
+ };
16
+ z_$$bindings?: Bindings;
17
+ }
18
+ declare const ToggleRoot: $$__sveltets_2_IsomorphicComponent<ToggleProps, {
19
+ [evt: string]: CustomEvent<any>;
20
+ }, {}, {}, "ref">;
21
+ type ToggleRoot = InstanceType<typeof ToggleRoot>;
22
+ export default ToggleRoot;
@@ -0,0 +1 @@
1
+ export { default as Root } from './toggle-root.svelte';
@@ -0,0 +1 @@
1
+ export { default as Root } from './toggle-root.svelte';
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "ui-ingredients",
3
3
  "type": "module",
4
4
  "license": "MIT",
5
- "version": "0.1.1",
5
+ "version": "0.3.0",
6
6
  "packageManager": "pnpm@9.7.0",
7
7
  "svelte": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",