zag-ripple 0.0.10 → 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.10",
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,17 +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, trackSplit, 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 access(userProps) {
21
- if (isFunction(userProps)) return userProps()
22
- return userProps
20
+ function onMount(mount) {
21
+ effect(() => {
22
+ return untrack(mount);
23
+ });
23
24
  }
24
25
 
25
- function compact(obj) {
26
+ function compact<T extends Record<string, unknown> | undefined>(obj: T): T {
26
27
  if (!isPlainObject(obj) || obj === undefined) return obj
27
28
  const keys = Reflect.ownKeys(obj).filter((key) => typeof key === "string")
28
29
  const filtered = {}
@@ -35,13 +36,12 @@ function compact(obj) {
35
36
  return filtered
36
37
  }
37
38
 
38
- export function useMachine(
39
- machine,
40
- _userProps,
41
- ) {
42
- let [id, ids, getRootNode, rest]= trackSplit(access(_userProps), ['id', 'ids', 'getRootNode'])
39
+ export function useMachine<T extends MachineSchema>(
40
+ machine: Machine<T>,
41
+ userProps: Partial<T["props"]> = {},
42
+ ): Service<T> {
43
43
  let scope = track(() => {
44
- return createScope({ id: @id, ids: @ids, getRootNode: @getRootNode })
44
+ return createScope({ id: @userProps.@id, ids: @userProps.@ids, getRootNode: @userProps.@getRootNode })
45
45
  })
46
46
 
47
47
  const debug = (...args: any[]) => {
@@ -49,8 +49,7 @@ export function useMachine(
49
49
  }
50
50
 
51
51
  let props = track(() => {
52
- const p = { ...@rest, id: @id, ids: @ids, getRootNode: @getRootNode }
53
- return machine.props?.({ props: compact(p), scope: @scope }) ?? p
52
+ return machine.props?.({ props: compact(@userProps), scope: @scope }) ?? @userProps
54
53
  })
55
54
 
56
55
  const prop = useProp(() => @props)
@@ -76,7 +75,7 @@ export function useMachine(
76
75
  },
77
76
  })
78
77
 
79
- const ctx = {
78
+ const ctx: BindableContext<T> = {
80
79
  get(key) {
81
80
  return context?.[key]?.get()
82
81
  },
@@ -122,7 +121,7 @@ export function useMachine(
122
121
 
123
122
  const refs = useRefs(machine.refs?.({ prop, context: ctx }) ?? {})
124
123
 
125
- const getParams = () => ({
124
+ const getParams = (): Params<T> => ({
126
125
  state: getState(),
127
126
  context: ctx,
128
127
  event: getEvent(),
@@ -138,7 +137,7 @@ export function useMachine(
138
137
  choose,
139
138
  })
140
139
 
141
- const action = (keys) => {
140
+ const action = (keys: ActionsOrFn<T> | undefined) => {
142
141
  const strs = isFunction(keys) ? keys(getParams()) : keys
143
142
  if (!strs) return
144
143
  const fns = strs.map((s) => {
@@ -151,12 +150,12 @@ export function useMachine(
151
150
  }
152
151
  }
153
152
 
154
- const guard = (str) => {
153
+ const guard = (str: T["guard"] | GuardFn<T>) => {
155
154
  if (isFunction(str)) return str(getParams())
156
155
  return machine.implementations?.guards?.[str](getParams())
157
156
  }
158
157
 
159
- const effectFn = (keys) => {
158
+ const effectFn = (keys: EffectsOrFn<T> | undefined) => {
160
159
  const strs = isFunction(keys) ? keys(getParams()) : keys
161
160
  if (!strs) return
162
161
  const fns = strs.map((s) => {
@@ -172,7 +171,7 @@ export function useMachine(
172
171
  return () => cleanups.forEach((fn) => fn?.())
173
172
  }
174
173
 
175
- const choose = (transitions) => {
174
+ const choose: ChooseFn<T> = (transitions) => {
176
175
  return toArray(transitions).find((t) => {
177
176
  let result = !t.guard
178
177
  if (isString(t.guard)) result = !!guard(t.guard)
@@ -181,7 +180,7 @@ export function useMachine(
181
180
  })
182
181
  }
183
182
 
184
- const computed = (key) => {
183
+ const computed: ComputedFn<T> = (key) => {
185
184
  ensure(machine.computed, () => `[zag-js] No computed object found on machine`)
186
185
  const fn = machine.computed[key]
187
186
  return fn({
@@ -232,13 +231,11 @@ export function useMachine(
232
231
 
233
232
  let status = MachineStatus.NotStarted
234
233
 
235
- effect(() => {
236
- queueMicrotask(() => {
237
- const started = status === MachineStatus.Started
238
- status = MachineStatus.Started
239
- debug(started ? "rehydrating..." : "initializing...")
240
- state.invoke(state.initial, INIT_STATE)
241
- })
234
+ onMount(() => {
235
+ const started = status === MachineStatus.Started
236
+ status = MachineStatus.Started
237
+ debug(started ? "rehydrating..." : "initializing...")
238
+ state.invoke(state.initial, INIT_STATE)
242
239
 
243
240
  return () => {
244
241
  debug("unmounting...")
@@ -248,9 +245,7 @@ export function useMachine(
248
245
  effects = new Map()
249
246
  transitionRef.current = null
250
247
 
251
- queueMicrotask(() => {
252
- action(machine.exit)
253
- })
248
+ action(machine.exit)
254
249
  }
255
250
  })
256
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
  }