Skip to content

Commit 0a42ea8

Browse files
committed
refactor: machines for nullable
1 parent 2fdf79c commit 0a42ea8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+417
-148
lines changed

.changeset/quiet-mayflies-do.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/carousel": patch
3+
---
4+
5+
Enforce required `slideCount` to ensure machine works as expected

.xstate/angle-slider.js

+134-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,44 @@ const {
1010
choose
1111
} = actions;
1212
const fetchMachine = createMachine({
13-
id: "angle-slider",
14-
initial: "idle",
15-
context: {},
13+
props({
14+
props
15+
}) {
16+
return {
17+
step: 1,
18+
defaultValue: 0,
19+
...props
20+
};
21+
},
22+
context({
23+
prop,
24+
bindable
25+
}) {
26+
return {
27+
value: bindable(() => ({
28+
defaultValue: prop("defaultValue"),
29+
value: prop("value"),
30+
onChange(value) {
31+
prop("onValueChange")?.({
32+
value,
33+
valueAsDegree: `${value}deg`
34+
});
35+
}
36+
}))
37+
};
38+
},
39+
watch({
40+
track,
41+
context: {},
42+
action
43+
}) {
44+
track([() => context.get("value")], () => {
45+
action(["syncInputElement"]);
46+
});
47+
},
48+
initialState() {
49+
return "idle";
50+
},
1651
on: {
1752
"VALUE.SET": {
1853
actions: ["setValue"]
@@ -59,18 +94,110 @@ const fetchMachine = createMachine({
5994
}
6095
},
6196
dragging: {
62-
entry: "focusThumb",
63-
activities: "trackPointerMove",
97+
entry: ["focusThumb"],
98+
effects: ["trackPointerMove"],
6499
on: {
65100
"DOC.POINTER_UP": {
66101
target: "focused",
67-
actions: "invokeOnChangeEnd"
102+
actions: ["invokeOnChangeEnd"]
68103
},
69104
"DOC.POINTER_MOVE": {
70-
actions: "setPointerValue"
105+
actions: ["setPointerValue"]
71106
}
72107
}
73108
}
109+
},
110+
implementations: {
111+
effects: {
112+
trackPointerMove({
113+
scope,
114+
send
115+
}) {
116+
return trackPointerMove(scope.getDoc(), {
117+
onPointerMove(info) {
118+
send({
119+
type: "DOC.POINTER_MOVE",
120+
point: info.point
121+
});
122+
},
123+
onPointerUp() {
124+
send({
125+
type: "DOC.POINTER_UP"
126+
});
127+
}
128+
});
129+
}
130+
},
131+
actions: {
132+
syncInputElement({
133+
scope,
134+
context
135+
}) {
136+
const inputEl = dom.getHiddenInputEl(scope);
137+
setElementValue(inputEl, context.get("value").toString());
138+
},
139+
invokeOnChangeEnd({
140+
context,
141+
prop
142+
}) {
143+
prop("onValueChangeEnd")?.({
144+
value: context.get("value"),
145+
valueAsDegree: computed("valueAsDegree")
146+
});
147+
},
148+
setPointerValue({
149+
scope,
150+
event,
151+
context,
152+
prop
153+
}) {
154+
const controlEl = dom.getControlEl(scope);
155+
if (!controlEl) return;
156+
const deg = getAngle(controlEl, event.point);
157+
context.set("value", constrainAngle(deg, prop("step")));
158+
},
159+
setValueToMin({
160+
context
161+
}) {
162+
context.set("value", MIN_VALUE);
163+
},
164+
setValueToMax({
165+
context
166+
}) {
167+
context.set("value", MAX_VALUE);
168+
},
169+
setValue({
170+
context,
171+
event
172+
}) {
173+
context.set("value", clampAngle(event.value));
174+
},
175+
decrementValue({
176+
context,
177+
event,
178+
prop
179+
}) {
180+
const value = snapValueToStep(context.get("value") - event.step, MIN_VALUE, MAX_VALUE, event.step ?? prop("step"));
181+
context.set("value", value);
182+
},
183+
incrementValue({
184+
context,
185+
event,
186+
prop
187+
}) {
188+
const value = snapValueToStep(context.get("value") + event.step, MIN_VALUE, MAX_VALUE, event.step ?? prop("step"));
189+
context.set("value", value);
190+
},
191+
focusThumb({
192+
scope
193+
}) {
194+
raf(() => {
195+
dom.getThumbEl(scope)?.focus({
196+
preventScroll: true
197+
});
198+
});
199+
}
200+
}
74201
}
75202
}, {
76203
actions: {

.xstate/hover-card.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const fetchMachine = createMachine({
1616
return {
1717
openDelay: 700,
1818
closeDelay: 300,
19-
...compact(props),
19+
...props,
2020
positioning: {
2121
placement: "bottom",
2222
...props.positioning

.xstate/rating-group.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const fetchMachine = createMachine({
1818
count: 5,
1919
dir: "ltr",
2020
defaultValue: -1,
21-
...compact(props),
21+
...props,
2222
translations: {
2323
ratingValueText: index => `${index} stars`,
2424
...props.translations

.xstate/slider.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const fetchMachine = createMachine({
2323
origin: "start",
2424
orientation: "horizontal",
2525
minStepsBetweenThumbs: 0,
26-
...compact(props)
26+
...props
2727
};
2828
},
2929
initialState() {
@@ -75,7 +75,6 @@ const fetchMachine = createMachine({
7575
action(["syncInputElements"]);
7676
});
7777
},
78-
entry: ["coarseValue"],
7978
effects: ["trackFormControlState", "trackThumbsSize"],
8079
on: {
8180
SET_VALUE: [{
@@ -309,13 +308,6 @@ const fetchMachine = createMachine({
309308
} = getRangeAtIndex(params, index);
310309
context.set("value", prev => setValueAtIndex(prev, index, max));
311310
},
312-
coarseValue(params) {
313-
const {
314-
context
315-
} = params;
316-
const value = normalizeValues(params, context.get("value"));
317-
context.set("value", value);
318-
},
319311
setValueAtIndex(params) {
320312
const {
321313
context,

0 commit comments

Comments
 (0)