Skip to content

Commit 61237be

Browse files
committed
refactor: react bindings
1 parent c0f722c commit 61237be

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

packages/frameworks/react/src/bindable.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { isFunction } from "@zag-js/utils"
22
import type { Bindable, BindableParams } from "@zag-js/core"
33
import { useLayoutEffect, useRef, useState } from "react"
4+
import { flushSync } from "react-dom"
5+
6+
const identity = (v: VoidFunction) => v()
47

58
export function useBindable<T>(props: () => BindableParams<T>): Bindable<T> {
69
const initial = props().value ?? props().defaultValue
@@ -24,7 +27,7 @@ export function useBindable<T>(props: () => BindableParams<T>): Bindable<T> {
2427
prevValue.current = valueRef.current
2528
}, [value, props().value])
2629

27-
const set = (value: T | ((prev: T) => T)) => {
30+
const setFn = (value: T | ((prev: T) => T)) => {
2831
const prev = prevValue.current
2932
const next = isFunction(value) ? value(prev as T) : value
3033

@@ -46,7 +49,10 @@ export function useBindable<T>(props: () => BindableParams<T>): Bindable<T> {
4649
initial,
4750
ref: valueRef,
4851
get,
49-
set,
52+
set(value: T | ((prev: T) => T)) {
53+
const exec = props().sync ? flushSync : identity
54+
exec(() => setFn(value))
55+
},
5056
invoke(nextValue: T, prevValue: T) {
5157
props().onChange?.(nextValue, prevValue)
5258
},

packages/frameworks/react/src/machine.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ export function useMachine<T extends BaseSchema>(
5151
return contextRef.current?.[key].get()
5252
},
5353
set(key, value) {
54-
flushSync(() => {
55-
contextRef.current?.[key].set(value)
56-
})
54+
contextRef.current?.[key].set(value)
5755
},
5856
initial(key) {
5957
return contextRef.current?.[key].initial

packages/machines/date-picker/src/date-picker.machine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export const machine = createMachine<DatePickerSchema>({
136136
})),
137137
activeIndex: bindable(() => ({
138138
defaultValue: 0,
139+
sync: true,
139140
onChange(value) {
140141
console.log("activeIndex", value)
141142
},

packages/machines/tour/src/tour.machine.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const machine = createMachine<TourSchema>({
4747
})),
4848
stepId: bindable<string | null>(() => ({
4949
defaultValue: prop("stepId"),
50+
sync: true,
5051
onChange(value) {
5152
const computed = getComputed()
5253
const context = getContext()
@@ -97,7 +98,7 @@ export const machine = createMachine<TourSchema>({
9798

9899
effects: ["trackBoundarySize"],
99100

100-
exit: ["clearStep", "cleanupRefs"],
101+
exit: ["cleanupRefs"],
101102

102103
on: {
103104
"STEPS.SET": {
@@ -524,7 +525,7 @@ function syncZIndex(scope: Scope) {
524525
}
525526

526527
function setStep(params: Params<TourSchema>, idx: number) {
527-
const { context, refs, send } = params
528+
const { context, refs, computed, prop } = params
528529
const steps = context.get("steps")
529530
const step = steps[idx]
530531

@@ -540,19 +541,23 @@ function setStep(params: Params<TourSchema>, idx: number) {
540541
}
541542

542543
const next = () => {
543-
send({ type: "STEP.NEXT" })
544+
const idx = nextIndex(steps, computed("stepIndex"))
545+
context.set("stepId", steps[idx].id)
544546
}
545547

546548
const goto = (id: string) => {
547-
send({ type: "STEP.SET", id })
549+
const step = findStep(steps, id)
550+
if (!step) return
551+
context.set("stepId", step.id)
548552
}
549553

550554
const dismiss = () => {
551-
send({ type: "DISMISS" })
555+
context.set("stepId", null)
556+
prop("onStatusChange")?.({ status: "dismissed", stepId: null, stepIndex: -1 })
552557
}
553558

554559
const show = () => {
555-
send({ type: "STEP.SET", id: step.id })
560+
context.set("stepId", step.id)
556561
}
557562

558563
if (!step.effect) {

0 commit comments

Comments
 (0)