ui-ingredients 0.1.1 → 0.2.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,13 @@
1
+ import type { HTMLButtonAttributes } from 'svelte/elements';
2
+ export interface CreateToggleProps {
3
+ disabled?: boolean;
4
+ pressed?: boolean;
5
+ pressedControlled?: boolean;
6
+ onPressedChange?: (pressed: boolean) => void;
7
+ }
8
+ export interface CreateToggleReturn {
9
+ pressed: boolean;
10
+ setPressed: (pressed: boolean) => void;
11
+ getRootProps: () => HTMLButtonAttributes;
12
+ }
13
+ export declare function createToggle(props: CreateToggleProps): any;
@@ -0,0 +1,37 @@
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?.(value);
8
+ if (!props.pressedControlled) {
9
+ pressed = value;
10
+ }
11
+ }
12
+ function getRootProps() {
13
+ return {
14
+ disabled: props.disabled,
15
+ onclick() {
16
+ if (props.disabled)
17
+ return;
18
+ setPressed(!pressed);
19
+ },
20
+ 'aria-pressed': ariaAttr(pressed),
21
+ 'data-state': pressed ? 'on' : 'off',
22
+ 'data-pressed': dataAttr(pressed),
23
+ 'data-disabled': dataAttr(props.disabled),
24
+ ...parts.root.attrs,
25
+ };
26
+ }
27
+ $effect(() => {
28
+ if (props.pressedControlled) {
29
+ pressed = props.pressed ?? false;
30
+ }
31
+ });
32
+ return reflect(() => ({
33
+ pressed,
34
+ setPressed,
35
+ getRootProps,
36
+ }));
37
+ }
@@ -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,51 @@
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
+ 'pressedControlled',
35
+ ])(props),
36
+ );
37
+
38
+ let toggle = createToggle(reflect(() => createToggleProps));
39
+
40
+ let mergedProps = $derived(mergeProps(toggle.getRootProps(), localProps));
41
+
42
+ setToggleContext(toggle);
43
+ </script>
44
+
45
+ {#if asChild}
46
+ {@render asChild(mergedProps, toggle)}
47
+ {:else}
48
+ <button bind:this={ref} {...mergedProps}>
49
+ {@render children?.(toggle)}
50
+ </button>
51
+ {/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.2.0",
6
6
  "packageManager": "pnpm@9.7.0",
7
7
  "svelte": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",