zag-ripple 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zag-ripple",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "RippleJS Adapter for Zag JS",
5
5
  "keywords": [
6
6
  "js",
@@ -16,12 +16,12 @@
16
16
  "homepage": "https://github.com/anubra266/zag-ripple#readme",
17
17
  "license": "MIT",
18
18
  "main": "src/index.ripple",
19
- "types": "src/index.d.ts",
20
19
  "exports": {
21
20
  ".": {
22
21
  "import": "./src/index.ripple",
23
22
  "types": "./src/index.d.ts"
24
- }
23
+ },
24
+ "./package.json": "./package.json"
25
25
  },
26
26
  "repository": "https://github.com/anubra266/zag-ripple/tree/main/packages/zag-ripple",
27
27
  "sideEffects": false,
@@ -32,9 +32,9 @@
32
32
  "url": "https://github.com/anubra266i/zag-ripple/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@zag-js/core": "^1.24.2",
36
- "@zag-js/types": "^1.24.2",
37
- "@zag-js/utils": "^1.24.2"
35
+ "@zag-js/core": "^1.34.1",
36
+ "@zag-js/types": "^1.34.1",
37
+ "@zag-js/utils": "^1.34.1"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "ripple": ">=0.2.0"
@@ -1,7 +1,8 @@
1
+ import type { Bindable, BindableParams } from "@zag-js/core"
1
2
  import { isFunction } from "@zag-js/utils"
2
3
  import { track, effect } from "ripple"
3
4
 
4
- export function bindable(props) {
5
+ export function bindable<T>(props: () => BindableParams<T>): Bindable<T> {
5
6
  const initial = props().defaultValue ?? props().value
6
7
  const eq = props().isEqual ?? Object.is
7
8
 
package/src/index.d.ts CHANGED
@@ -1,64 +1,65 @@
1
- import type { Machine, Scope } from "@zag-js/core";
2
- import { Component } from "ripple";
1
+ import type {
2
+ Bindable,
3
+ BindableParams,
4
+ Machine,
5
+ MachineSchema,
6
+ Service,
7
+ } from "@zag-js/core";
8
+ import type { Component } from "ripple";
3
9
 
10
+ // Re-export from @zag-js/core
4
11
  export { mergeProps } from "@zag-js/core";
5
12
 
6
- // From normalize-props.ripple
7
- import { NormalizeProps, PropTypes } from "@zag-js/types";
8
- export declare const normalizeProps: NormalizeProps<PropTypes>;
9
-
10
- // From machine.ripple
11
- export interface MachineApi<TContext = Record<string, any>, TState extends string = string> {
12
- state: {
13
- value: TState;
14
- matches(...values: TState[]): boolean;
15
- hasTag(tag: string): boolean;
16
- get(): TState;
17
- };
18
- send: (event: { type: string;[key: string]: any }) => void;
19
- context: {
20
- get(key: keyof TContext): any;
21
- set(key: keyof TContext, value: any): void;
22
- initial(key: keyof TContext): any;
23
- hash(key: keyof TContext): string;
24
- };
25
- prop: (key: string) => any;
26
- scope: Scope;
27
- refs: Record<string, { current: any }>;
28
- computed: (key: string) => any;
29
- event: {
30
- type: string;
31
- current(): any;
32
- previous(): any;
33
- [key: string]: any;
34
- };
35
- getStatus: () => "started" | "stopped" | "not_started";
13
+ // Bindable function and related types
14
+ export declare function bindable<T>(props: () => BindableParams<T>): Bindable<T>;
15
+ export declare namespace bindable {
16
+ function cleanup(fn: VoidFunction): void;
17
+ function ref<T>(defaultValue: T): { get(): T; set(next: T): void };
36
18
  }
37
19
 
38
- export interface MachineOptions {
39
- id?: string;
40
- ids?: Record<string, string>;
41
- getRootNode?: () => Document | ShadowRoot | Node;
42
- }
20
+ // Machine hook
21
+ export declare function useMachine<T extends MachineSchema>(
22
+ machine: Machine<T>,
23
+ userProps?: Partial<T["props"]>
24
+ ): Service<T>;
43
25
 
44
- export declare function useMachine<
45
- TContext = Record<string, any>,
46
- TState extends string = string
47
- >(
48
- machine: Machine<TContext>,
49
- options?: MachineOptions | (() => MachineOptions)
50
- ): MachineApi<TContext, TState>;
26
+ // Normalize props functionality
27
+ export declare function normalizeProps<T extends Record<string, any>>(
28
+ props: T
29
+ ): T;
51
30
 
31
+ export declare function toStyleString(style: Record<string, number | string>): string;
52
32
 
33
+ // Portal component and types
34
+ type RootNode = ShadowRoot | Document | Node;
35
+ type GetRootNode = () => RootNode;
53
36
 
37
+ export interface PortalActionProps {
38
+ disabled?: boolean | undefined;
39
+ container?: HTMLElement | undefined;
40
+ getRootNode?: GetRootNode | undefined;
41
+ }
54
42
 
55
- export interface PortalProps {
56
- disabled?: boolean | undefined
57
- container?: HTMLElement | undefined
58
- getRootNode?: (() => ShadowRoot | Document | Node) | undefined
59
- children: Component
43
+ export interface PortalProps extends PortalActionProps {
44
+ children?: Component;
60
45
  }
61
46
 
62
- export declare const Portal: Component<PortalProps>;
63
- // export declare function Portal(props: PortalProps): Component;
47
+ export declare function Portal(props: PortalProps): void;
48
+
49
+ // Refs hook
50
+ export declare function useRefs<T>(refs: T): {
51
+ get<K extends keyof T>(key: K): T[K];
52
+ set<K extends keyof T>(key: K, value: T[K]): void;
53
+ };
54
+
55
+ // Track utility
56
+ export declare function createTrack(
57
+ deps: any[],
58
+ effectCallback: VoidFunction
59
+ ): void;
60
+
61
+
62
+
63
+
64
+
64
65
 
@@ -12,12 +12,18 @@ import type {
12
12
  } from "@zag-js/core"
13
13
  import { createScope, INIT_STATE, MachineStatus } from "@zag-js/core"
14
14
  import { ensure, isFunction, isPlainObject, isString, toArray, warn } from "@zag-js/utils"
15
- import { track, effect, flushSync } from "ripple"
15
+ import { track, effect, flushSync, untrack } from "ripple"
16
16
  import { bindable } from "./bindable.ripple"
17
17
  import { useRefs } from "./refs.ripple"
18
18
  import { createTrack } from "./track.ripple"
19
19
 
20
- function compact(obj) {
20
+ function onMount(mount) {
21
+ effect(() => {
22
+ return untrack(mount);
23
+ });
24
+ }
25
+
26
+ function compact<T extends Record<string, unknown> | undefined>(obj: T): T {
21
27
  if (!isPlainObject(obj) || obj === undefined) return obj
22
28
  const keys = Reflect.ownKeys(obj).filter((key) => typeof key === "string")
23
29
  const filtered = {}
@@ -30,13 +36,12 @@ function compact(obj) {
30
36
  return filtered
31
37
  }
32
38
 
33
- export function useMachine(
34
- machine,
35
- _userProps,
36
- ) {
37
- const userProps = track(() => @_userProps)
39
+ export function useMachine<T extends MachineSchema>(
40
+ machine: Machine<T>,
41
+ userProps: Partial<T["props"]> = {},
42
+ ): Service<T> {
38
43
  let scope = track(() => {
39
- return createScope({ id: @userProps.id, ids: @userProps.ids, getRootNode: @userProps.getRootNode })
44
+ return createScope({ id: @userProps.@id, ids: @userProps.@ids, getRootNode: @userProps.@getRootNode })
40
45
  })
41
46
 
42
47
  const debug = (...args: any[]) => {
@@ -70,7 +75,7 @@ export function useMachine(
70
75
  },
71
76
  })
72
77
 
73
- const ctx = {
78
+ const ctx: BindableContext<T> = {
74
79
  get(key) {
75
80
  return context?.[key]?.get()
76
81
  },
@@ -116,7 +121,7 @@ export function useMachine(
116
121
 
117
122
  const refs = useRefs(machine.refs?.({ prop, context: ctx }) ?? {})
118
123
 
119
- const getParams = () => ({
124
+ const getParams = (): Params<T> => ({
120
125
  state: getState(),
121
126
  context: ctx,
122
127
  event: getEvent(),
@@ -132,7 +137,7 @@ export function useMachine(
132
137
  choose,
133
138
  })
134
139
 
135
- const action = (keys) => {
140
+ const action = (keys: ActionsOrFn<T> | undefined) => {
136
141
  const strs = isFunction(keys) ? keys(getParams()) : keys
137
142
  if (!strs) return
138
143
  const fns = strs.map((s) => {
@@ -145,12 +150,12 @@ export function useMachine(
145
150
  }
146
151
  }
147
152
 
148
- const guard = (str) => {
153
+ const guard = (str: T["guard"] | GuardFn<T>) => {
149
154
  if (isFunction(str)) return str(getParams())
150
155
  return machine.implementations?.guards?.[str](getParams())
151
156
  }
152
157
 
153
- const effectFn = (keys) => {
158
+ const effectFn = (keys: EffectsOrFn<T> | undefined) => {
154
159
  const strs = isFunction(keys) ? keys(getParams()) : keys
155
160
  if (!strs) return
156
161
  const fns = strs.map((s) => {
@@ -166,7 +171,7 @@ export function useMachine(
166
171
  return () => cleanups.forEach((fn) => fn?.())
167
172
  }
168
173
 
169
- const choose = (transitions) => {
174
+ const choose: ChooseFn<T> = (transitions) => {
170
175
  return toArray(transitions).find((t) => {
171
176
  let result = !t.guard
172
177
  if (isString(t.guard)) result = !!guard(t.guard)
@@ -175,7 +180,7 @@ export function useMachine(
175
180
  })
176
181
  }
177
182
 
178
- const computed = (key) => {
183
+ const computed: ComputedFn<T> = (key) => {
179
184
  ensure(machine.computed, () => `[zag-js] No computed object found on machine`)
180
185
  const fn = machine.computed[key]
181
186
  return fn({
@@ -226,13 +231,11 @@ export function useMachine(
226
231
 
227
232
  let status = MachineStatus.NotStarted
228
233
 
229
- effect(() => {
230
- queueMicrotask(() => {
231
- const started = status === MachineStatus.Started
232
- status = MachineStatus.Started
233
- debug(started ? "rehydrating..." : "initializing...")
234
- state.invoke(state.initial, INIT_STATE)
235
- })
234
+ onMount(() => {
235
+ const started = status === MachineStatus.Started
236
+ status = MachineStatus.Started
237
+ debug(started ? "rehydrating..." : "initializing...")
238
+ state.invoke(state.initial, INIT_STATE)
236
239
 
237
240
  return () => {
238
241
  debug("unmounting...")
@@ -242,9 +245,7 @@ export function useMachine(
242
245
  effects = new Map()
243
246
  transitionRef.current = null
244
247
 
245
- queueMicrotask(() => {
246
- action(machine.exit)
247
- })
248
+ action(machine.exit)
248
249
  }
249
250
  })
250
251
 
package/src/portal.ripple CHANGED
@@ -1,6 +1,15 @@
1
1
  import { Portal as RipplePortal } from 'ripple';
2
2
 
3
- export component Portal({ disabled, getRootNode, container, children }:any) {
3
+ type RootNode = ShadowRoot | Document | Node
4
+ type GetRootNode = () => RootNode
5
+
6
+ export interface PortalActionProps {
7
+ disabled?: boolean | undefined
8
+ container?: HTMLElement | undefined
9
+ getRootNode?: GetRootNode | undefined
10
+ }
11
+
12
+ export component Portal({ disabled, getRootNode, container, children }: PortalActionProps) {
4
13
  const isServer = typeof window === "undefined"
5
14
 
6
15
  if(isServer || disabled){
package/src/refs.ripple CHANGED
@@ -1,10 +1,10 @@
1
- export function useRefs(refs) {
1
+ export function useRefs<T>(refs: T) {
2
2
  const ref = { current: refs }
3
3
  return {
4
- get(key) {
4
+ get<K extends keyof T>(key: K): T[K] {
5
5
  return ref.current[key]
6
6
  },
7
- set(key, value) {
7
+ set<K extends keyof T>(key: K, value: T[K]) {
8
8
  ref.current[key] = value
9
9
  },
10
10
  }
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "extends": "../../tsconfig.json",
3
3
  "compilerOptions": {
4
- "tsBuildInfoFile": "node_modules/.cache/.tsbuildinfo"
4
+ "tsBuildInfoFile": "node_modules/.cache/.tsbuildinfo",
5
5
  }
6
6
  }