zag-ripple 0.0.1 → 0.0.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Abraham
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zag-ripple",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "RippleJS Adapter for Zag JS",
5
5
  "keywords": [
6
6
  "js",
@@ -16,6 +16,7 @@
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",
19
20
  "repository": "https://github.com/anubra266/zag-ripple/tree/main/packages/zag-ripple",
20
21
  "sideEffects": false,
21
22
  "publishConfig": {
@@ -24,15 +25,18 @@
24
25
  "bugs": {
25
26
  "url": "https://github.com/anubra266i/zag-ripple/issues"
26
27
  },
27
- "scripts": {
28
- "test": "echo \"No test specified\""
29
- },
30
28
  "dependencies": {
31
- "@zag-js/core": "^1.3.3",
32
- "@zag-js/types": "latest",
33
- "@zag-js/utils": "latest"
29
+ "@zag-js/core": "^1.24.2",
30
+ "@zag-js/types": "^1.24.2",
31
+ "@zag-js/utils": "^1.24.2"
32
+ },
33
+ "peerDependencies": {
34
+ "ripple": ">=0.2.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "typescript": "~5.6.2"
38
+ },
39
+ "scripts": {
40
+ "test": "echo \"No test specified\""
37
41
  }
38
- }
42
+ }
@@ -53,8 +53,8 @@ export function createBindable(props) {
53
53
  }
54
54
  }
55
55
 
56
- createBindable.cleanup = (_fn: VoidFunction) => {
57
- effect(() => fn, [])
56
+ createBindable.cleanup = (fn: VoidFunction) => {
57
+ effect(() => fn(), [])
58
58
  }
59
59
 
60
60
  createBindable.ref = (defaultValue: any) => {
package/src/index.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ import type { Machine, Scope } from "@zag-js/core";
2
+
3
+ export { mergeProps } from "@zag-js/core";
4
+
5
+ // From normalize-props.ripple
6
+ export declare const normalizeProps: <T>(props: T) => T;
7
+
8
+ // From machine.ripple
9
+ export interface MachineApi<TContext = Record<string, any>, TState extends string = string> {
10
+ state: {
11
+ value: TState;
12
+ matches(...values: TState[]): boolean;
13
+ hasTag(tag: string): boolean;
14
+ get(): TState;
15
+ };
16
+ send: (event: { type: string;[key: string]: any }) => void;
17
+ context: {
18
+ get(key: keyof TContext): any;
19
+ set(key: keyof TContext, value: any): void;
20
+ initial(key: keyof TContext): any;
21
+ hash(key: keyof TContext): string;
22
+ };
23
+ prop: (key: string) => any;
24
+ scope: Scope;
25
+ refs: Record<string, { current: any }>;
26
+ computed: (key: string) => any;
27
+ event: {
28
+ type: string;
29
+ current(): any;
30
+ previous(): any;
31
+ [key: string]: any;
32
+ };
33
+ getStatus: () => "started" | "stopped" | "not_started";
34
+ }
35
+
36
+ export interface MachineOptions {
37
+ id?: string;
38
+ ids?: Record<string, string>;
39
+ getRootNode?: () => Document | ShadowRoot | Node;
40
+ }
41
+
42
+ export declare function useMachine<
43
+ TContext = Record<string, any>,
44
+ TState extends string = string
45
+ >(
46
+ machine: Machine<TContext>,
47
+ options?: MachineOptions | (() => MachineOptions)
48
+ ): MachineApi<TContext, TState>;
@@ -108,6 +108,40 @@ export function useMachine(
108
108
 
109
109
  const refs = createRefs(machine.refs?.({ prop, context: ctx }) ?? {})
110
110
 
111
+ const send = (event) => {
112
+
113
+ if (status !== MachineStatus.Started) return
114
+
115
+ previousEventRef.current = eventRef.current
116
+ eventRef.current = event
117
+
118
+ let currentState = state.get()
119
+
120
+ const transitions =
121
+ machine.states[currentState]?.on?.[event.type] ??
122
+ machine.on?.[event.type]
123
+
124
+ const transition = choose(transitions)
125
+ if (!transition) return
126
+
127
+ // save current transition
128
+ transitionRef.current = transition
129
+ const target = transition.target ?? currentState
130
+
131
+ debug("transition", event.type, transition.target || currentState, `(${transition.actions})`)
132
+
133
+ const changed = target !== currentState
134
+ if (changed) {
135
+ state.set(target)
136
+ } else if (transition.reenter && !changed) {
137
+ // reenter will re-invoke the current state
138
+ state.invoke?.(currentState, currentState)
139
+ } else {
140
+ // call transition actions
141
+ action(transition.actions ?? [])
142
+ }
143
+ }
144
+
111
145
  const getParams = () => ({
112
146
  state: getState(),
113
147
  context: ctx,
@@ -226,39 +260,6 @@ export function useMachine(
226
260
  const initialState = machine.initialState({ prop })
227
261
  state.set(initialState)
228
262
 
229
- const send = (event) => {
230
- if (status !== MachineStatus.Started) return
231
-
232
- previousEventRef.current = eventRef.current
233
- eventRef.current = event
234
-
235
- let currentState = state.get()
236
-
237
- const transitions =
238
- machine.states[currentState]?.on?.[event.type] ??
239
- machine.on?.[event.type]
240
-
241
- const transition = choose(transitions)
242
- if (!transition) return
243
-
244
- // save current transition
245
- transitionRef.current = transition
246
- const target = transition.target ?? currentState
247
-
248
- debug("transition", event.type, transition.target || currentState, `(${transition.actions})`)
249
-
250
- const changed = target !== currentState
251
- if (changed) {
252
- state.set(target)
253
- } else if (transition.reenter && !changed) {
254
- // reenter will re-invoke the current state
255
- state.invoke?.(currentState, currentState)
256
- } else {
257
- // call transition actions
258
- action(transition.actions ?? [])
259
- }
260
- }
261
-
262
263
  machine.watch?.(getParams())
263
264
 
264
265
  return {
@@ -1,6 +1,62 @@
1
1
  import { createNormalizer } from "@zag-js/types"
2
2
 
3
-
4
3
 
5
- // Simple prop normalizer for RippleJS - just pass through
6
- export const normalizeProps = createNormalizer((props) => props)
4
+ type Dict = Record<string, boolean | number | string | undefined>
5
+
6
+ const propMap: Record<string, string> = {
7
+ className: "class",
8
+ defaultChecked: "checked",
9
+ defaultValue: "value",
10
+ htmlFor: "for",
11
+ onBlur: "onFocusOut",
12
+ onChange: "onInput",
13
+ onFocus: "onFocusIn",
14
+ }
15
+
16
+ export function toStyleString(style: Record<string, number | string>) {
17
+ let string = ""
18
+
19
+ for (let key in style) {
20
+ /**
21
+ * Ignore null and undefined values.
22
+ */
23
+ const value = style[key]
24
+ if (value === null || value === undefined) continue
25
+
26
+ /**
27
+ * Convert camelCase to kebab-case except for CSS custom properties.
28
+ */
29
+ if (!key.startsWith("--")) key = key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`)
30
+
31
+ string += `${key}:${value};`
32
+ }
33
+
34
+ return string
35
+ }
36
+
37
+ const preserveKeys = new Set<string>(
38
+ "viewBox,className,preserveAspectRatio,fillRule,clipPath,clipRule,strokeWidth,strokeLinecap,strokeLinejoin,strokeDasharray,strokeDashoffset,strokeMiterlimit".split(
39
+ ",",
40
+ ),
41
+ )
42
+
43
+ function toRippleProp(key: string) {
44
+ if (key in propMap) return propMap[key]
45
+ if (preserveKeys.has(key)) return key
46
+ return key.toLowerCase()
47
+ }
48
+
49
+ function toRipplePropValue(key: string, value: Dict[string]) {
50
+ if (key === "style" && typeof value === "object") return toStyleString(value)
51
+ return value
52
+ }
53
+
54
+ export const normalizeProps = createNormalizer<Record<string, any>>((props) => {
55
+ const normalized: Dict = {}
56
+
57
+ for (const key in props) {
58
+ normalized[toRippleProp(key)] = toRipplePropValue(key, props[key])
59
+ }
60
+
61
+ return normalized
62
+ })