@@ -51,16 +51,79 @@ element part needs to have a unique id.
51
51
Each time you initiate the machine with the ` useMachine ` hook, you'll need to
52
52
ensure that you provide a unique id.
53
53
54
- You can rely on framework-specific utilities to generate unique ids. React
55
- provides ` useId() ` hook and Solid.js provides a ` createId() ` function for this
56
- purpose.
57
-
58
- ``` js
59
- // / ... 👇 must be unique
60
- const service = useMachine (toggle .machine , { id: useId () })
61
- // / ...
54
+ In most cases, you can rely on the framework providing unique a unique id for each component.
55
+
56
+ ### React
57
+
58
+ ``` jsx
59
+ import * as accordion from " @zag-js/accordion"
60
+ import { useMachine , normalizeProps } from " @zag-js/framework"
61
+ import { useId } from " react"
62
+
63
+ function Component () {
64
+ const service = useMachine (accordion .machine , { id: useId () })
65
+ const api = machine .connect (service, normalizeProps)
66
+
67
+ // ...
68
+ }
69
+ ```
70
+
71
+ See [ useId] ( https://react.dev/reference/react/useId ) .
72
+
73
+ ### Solid
74
+
75
+ ``` jsx
76
+ import * as accordion from " @zag-js/accordion"
77
+ import { useMachine , normalizeProps } from " @zag-js/solid"
78
+ import { createUniqueId } from " solid-js"
79
+
80
+ function Component () {
81
+ const service = useMachine (accordion .machine , { id: createUniqueId () })
82
+ const api = machine .connect (service, normalizeProps)
83
+
84
+ // ...
85
+ }
62
86
```
63
87
88
+ See [ createUniqueId] ( https://docs.solidjs.com/reference/component-apis/create-unique-id ) .
89
+
90
+ ### Vue
91
+
92
+ ``` html
93
+ <script setup >
94
+ import * as accordion from " @zag-js/accordion"
95
+ import { useMachine , normalizeProps } from " @zag-js/vue"
96
+ import { useId } from " vue"
97
+
98
+ const service = useMachine (accordion .machine , { id: useId () })
99
+ const api = machine .connect (service, normalizeProps)
100
+ </script >
101
+
102
+ <template >
103
+ <!-- ... -->
104
+ </template >
105
+ ```
106
+
107
+ See [ useId] ( https://vuejs.org/api/composition-api-helpers#useid ) .
108
+
109
+ ### Svelte
110
+
111
+ ``` html
112
+ <script >
113
+ import * as accordion from " @zag-js/accordion"
114
+ import { useMachine , normalizeProps } from " @zag-js/svelte"
115
+
116
+ const id = $props .id ()
117
+ const service = useMachine (accordion .machine , { id })
118
+ const api = machine .connect (service, normalizeProps)
119
+ </script >
120
+
121
+
122
+ <!-- ... -->
123
+ ```
124
+
125
+ See [ $props.id] ( https://svelte.dev/docs/svelte/$props#$props.id() ) .
126
+
64
127
Internally, Zag maps the unique id provided to each component parts needed for
65
128
the widget to work.
66
129
@@ -72,7 +135,7 @@ To achieve this, you will need to pass custom `ids` to the machine's context.
72
135
This will ensure that calling ` document.getElementById(...) ` within the tooltip
73
136
and/or popover will return the same element.
74
137
75
- ``` tsx {7,12 }
138
+ ``` tsx {6,10 }
76
139
import * as tooltip from " @zag-js/tooltip"
77
140
import * as popover from " @zag-js/popover"
78
141
@@ -84,10 +147,9 @@ function Example() {
84
147
const popoverService = useMachine (popover .machine , {
85
148
ids: { trigger: " id-1" },
86
149
})
150
+
151
+ // ...
87
152
}
88
-
89
- // render UI
90
- return null
91
153
```
92
154
93
155
In the example above, you will notice that the popover and tooltip trigger share
@@ -107,7 +169,7 @@ To provide the correct reference to root node or document, you can pass
107
169
> In shadow DOM, the root node can be derived from calling
108
170
> ` element.getRootNode() ` method on any element.
109
171
110
- ``` jsx {12,15 ,42}
172
+ ``` jsx {12,16 ,42}
111
173
import * as accordion from " @zag-js/accordion"
112
174
import { useMachine , normalizeProps } from " @zag-js/react"
113
175
import Frame , { useFrame } from " react-frame-component"
0 commit comments