@destyler/dialog 0.0.7-beta.5 → 0.0.7-beta.9

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/dist/index.mjs DELETED
@@ -1,337 +0,0 @@
1
- import { createAnatomy } from '@zag-js/anatomy';
2
- import { createScope, raf } from '@zag-js/dom-query';
3
- import { ariaHidden } from '@zag-js/aria-hidden';
4
- import { createMachine } from '@zag-js/core';
5
- import { trackDismissableElement } from '@zag-js/dismissable';
6
- import { trapFocus } from '@zag-js/focus-trap';
7
- import { preventBodyScroll } from '@zag-js/remove-scroll';
8
- import { compact, createSplitProps } from '@zag-js/utils';
9
- import { createProps } from '@zag-js/types';
10
-
11
- const anatomy = createAnatomy("dialog").parts(
12
- "trigger",
13
- "backdrop",
14
- "positioner",
15
- "content",
16
- "title",
17
- "description",
18
- "closeTrigger"
19
- );
20
- const parts = anatomy.build();
21
-
22
- const dom = createScope({
23
- getPositionerId: (ctx) => ctx.ids?.positioner ?? `dialog:${ctx.id}:positioner`,
24
- getBackdropId: (ctx) => ctx.ids?.backdrop ?? `dialog:${ctx.id}:backdrop`,
25
- getContentId: (ctx) => ctx.ids?.content ?? `dialog:${ctx.id}:content`,
26
- getTriggerId: (ctx) => ctx.ids?.trigger ?? `dialog:${ctx.id}:trigger`,
27
- getTitleId: (ctx) => ctx.ids?.title ?? `dialog:${ctx.id}:title`,
28
- getDescriptionId: (ctx) => ctx.ids?.description ?? `dialog:${ctx.id}:description`,
29
- getCloseTriggerId: (ctx) => ctx.ids?.closeTrigger ?? `dialog:${ctx.id}:close`,
30
- getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
31
- getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
32
- getBackdropEl: (ctx) => dom.getById(ctx, dom.getBackdropId(ctx)),
33
- getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
34
- getTitleEl: (ctx) => dom.getById(ctx, dom.getTitleId(ctx)),
35
- getDescriptionEl: (ctx) => dom.getById(ctx, dom.getDescriptionId(ctx)),
36
- getCloseTriggerEl: (ctx) => dom.getById(ctx, dom.getCloseTriggerId(ctx))
37
- });
38
-
39
- function connect(state, send, normalize) {
40
- const ariaLabel = state.context["aria-label"];
41
- const open = state.matches("open");
42
- const rendered = state.context.renderedElements;
43
- return {
44
- open,
45
- setOpen(nextOpen) {
46
- if (nextOpen === open)
47
- return;
48
- send(nextOpen ? "OPEN" : "CLOSE");
49
- },
50
- getTriggerProps() {
51
- return normalize.button({
52
- ...parts.trigger.attrs,
53
- "dir": state.context.dir,
54
- "id": dom.getTriggerId(state.context),
55
- "aria-haspopup": "dialog",
56
- "type": "button",
57
- "aria-expanded": open,
58
- "data-state": open ? "open" : "closed",
59
- "aria-controls": dom.getContentId(state.context),
60
- onClick(event) {
61
- if (event.defaultPrevented)
62
- return;
63
- send("TOGGLE");
64
- }
65
- });
66
- },
67
- getBackdropProps() {
68
- return normalize.element({
69
- ...parts.backdrop.attrs,
70
- "dir": state.context.dir,
71
- "hidden": !open,
72
- "id": dom.getBackdropId(state.context),
73
- "data-state": open ? "open" : "closed"
74
- });
75
- },
76
- getPositionerProps() {
77
- return normalize.element({
78
- ...parts.positioner.attrs,
79
- dir: state.context.dir,
80
- id: dom.getPositionerId(state.context),
81
- style: {
82
- pointerEvents: open ? void 0 : "none"
83
- }
84
- });
85
- },
86
- getContentProps() {
87
- return normalize.element({
88
- ...parts.content.attrs,
89
- "dir": state.context.dir,
90
- "role": state.context.role,
91
- "hidden": !open,
92
- "id": dom.getContentId(state.context),
93
- "tabIndex": -1,
94
- "data-state": open ? "open" : "closed",
95
- "aria-modal": true,
96
- "aria-label": ariaLabel || void 0,
97
- "aria-labelledby": ariaLabel || !rendered.title ? void 0 : dom.getTitleId(state.context),
98
- "aria-describedby": rendered.description ? dom.getDescriptionId(state.context) : void 0
99
- });
100
- },
101
- getTitleProps() {
102
- return normalize.element({
103
- ...parts.title.attrs,
104
- dir: state.context.dir,
105
- id: dom.getTitleId(state.context)
106
- });
107
- },
108
- getDescriptionProps() {
109
- return normalize.element({
110
- ...parts.description.attrs,
111
- dir: state.context.dir,
112
- id: dom.getDescriptionId(state.context)
113
- });
114
- },
115
- getCloseTriggerProps() {
116
- return normalize.button({
117
- ...parts.closeTrigger.attrs,
118
- dir: state.context.dir,
119
- id: dom.getCloseTriggerId(state.context),
120
- type: "button",
121
- onClick(event) {
122
- if (event.defaultPrevented)
123
- return;
124
- event.stopPropagation();
125
- send("CLOSE");
126
- }
127
- });
128
- }
129
- };
130
- }
131
-
132
- function machine(userContext) {
133
- const ctx = compact(userContext);
134
- return createMachine(
135
- {
136
- id: "dialog",
137
- initial: ctx.open ? "open" : "closed",
138
- context: {
139
- role: "dialog",
140
- renderedElements: {
141
- title: true,
142
- description: true
143
- },
144
- modal: true,
145
- trapFocus: true,
146
- preventScroll: true,
147
- closeOnInteractOutside: true,
148
- closeOnEscape: true,
149
- restoreFocus: true,
150
- ...ctx
151
- },
152
- created: ["setAlertDialogProps"],
153
- watch: {
154
- open: ["toggleVisibility"]
155
- },
156
- states: {
157
- open: {
158
- entry: ["checkRenderedElements", "syncZIndex"],
159
- activities: ["trackDismissableElement", "trapFocus", "preventScroll", "hideContentBelow"],
160
- on: {
161
- "CONTROLLED.CLOSE": {
162
- target: "closed"
163
- },
164
- "CLOSE": [
165
- {
166
- guard: "isOpenControlled",
167
- actions: ["invokeOnClose"]
168
- },
169
- {
170
- target: "closed",
171
- actions: ["invokeOnClose"]
172
- }
173
- ],
174
- "TOGGLE": [
175
- {
176
- guard: "isOpenControlled",
177
- actions: ["invokeOnClose"]
178
- },
179
- {
180
- target: "closed",
181
- actions: ["invokeOnClose"]
182
- }
183
- ]
184
- }
185
- },
186
- closed: {
187
- on: {
188
- "CONTROLLED.OPEN": {
189
- target: "open"
190
- },
191
- "OPEN": [
192
- {
193
- guard: "isOpenControlled",
194
- actions: ["invokeOnOpen"]
195
- },
196
- {
197
- target: "open",
198
- actions: ["invokeOnOpen"]
199
- }
200
- ],
201
- "TOGGLE": [
202
- {
203
- guard: "isOpenControlled",
204
- actions: ["invokeOnOpen"]
205
- },
206
- {
207
- target: "open",
208
- actions: ["invokeOnOpen"]
209
- }
210
- ]
211
- }
212
- }
213
- }
214
- },
215
- {
216
- guards: {
217
- isOpenControlled: (ctx2) => !!ctx2["open.controlled"]
218
- },
219
- activities: {
220
- trackDismissableElement(ctx2, _evt, { send }) {
221
- const getContentEl = () => dom.getContentEl(ctx2);
222
- return trackDismissableElement(getContentEl, {
223
- defer: true,
224
- pointerBlocking: ctx2.modal,
225
- exclude: [dom.getTriggerEl(ctx2)],
226
- onInteractOutside(event) {
227
- ctx2.onInteractOutside?.(event);
228
- if (!ctx2.closeOnInteractOutside) {
229
- event.preventDefault();
230
- }
231
- },
232
- persistentElements: ctx2.persistentElements,
233
- onFocusOutside: ctx2.onFocusOutside,
234
- onPointerDownOutside: ctx2.onPointerDownOutside,
235
- onEscapeKeyDown(event) {
236
- ctx2.onEscapeKeyDown?.(event);
237
- if (!ctx2.closeOnEscape) {
238
- event.preventDefault();
239
- }
240
- },
241
- onDismiss() {
242
- send({ type: "CLOSE", src: "interact-outside" });
243
- }
244
- });
245
- },
246
- preventScroll(ctx2) {
247
- if (!ctx2.preventScroll)
248
- return;
249
- return preventBodyScroll(dom.getDoc(ctx2));
250
- },
251
- trapFocus(ctx2) {
252
- if (!ctx2.trapFocus || !ctx2.modal)
253
- return;
254
- const contentEl = () => dom.getContentEl(ctx2);
255
- return trapFocus(contentEl, {
256
- preventScroll: true,
257
- returnFocusOnDeactivate: !!ctx2.restoreFocus,
258
- initialFocus: ctx2.initialFocusEl,
259
- setReturnFocus: (el) => ctx2.finalFocusEl?.() ?? el
260
- });
261
- },
262
- hideContentBelow(ctx2) {
263
- if (!ctx2.modal)
264
- return;
265
- const getElements = () => [dom.getContentEl(ctx2)];
266
- return ariaHidden(getElements, { defer: true });
267
- }
268
- },
269
- actions: {
270
- setAlertDialogProps(ctx2) {
271
- if (ctx2.role !== "alertdialog")
272
- return;
273
- ctx2.initialFocusEl ||= () => dom.getCloseTriggerEl(ctx2);
274
- ctx2.closeOnInteractOutside = false;
275
- },
276
- checkRenderedElements(ctx2) {
277
- raf(() => {
278
- ctx2.renderedElements.title = !!dom.getTitleEl(ctx2);
279
- ctx2.renderedElements.description = !!dom.getDescriptionEl(ctx2);
280
- });
281
- },
282
- syncZIndex(ctx2) {
283
- raf(() => {
284
- const contentEl = dom.getContentEl(ctx2);
285
- if (!contentEl)
286
- return;
287
- const win = dom.getWin(ctx2);
288
- const styles = win.getComputedStyle(contentEl);
289
- const elems = [dom.getPositionerEl(ctx2), dom.getBackdropEl(ctx2)];
290
- elems.forEach((node) => {
291
- node?.style.setProperty("--z-index", styles.zIndex);
292
- });
293
- });
294
- },
295
- invokeOnClose(ctx2) {
296
- ctx2.onOpenChange?.({ open: false });
297
- },
298
- invokeOnOpen(ctx2) {
299
- ctx2.onOpenChange?.({ open: true });
300
- },
301
- toggleVisibility(ctx2, evt, { send }) {
302
- send({ type: ctx2.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE", previousEvent: evt });
303
- }
304
- }
305
- }
306
- );
307
- }
308
-
309
- const props = createProps()([
310
- "aria-label",
311
- "closeOnEscape",
312
- "closeOnInteractOutside",
313
- "dir",
314
- "finalFocusEl",
315
- "getRootNode",
316
- "getRootNode",
317
- "id",
318
- "id",
319
- "ids",
320
- "initialFocusEl",
321
- "modal",
322
- "onEscapeKeyDown",
323
- "onFocusOutside",
324
- "onInteractOutside",
325
- "onOpenChange",
326
- "onPointerDownOutside",
327
- "open.controlled",
328
- "open",
329
- "persistentElements",
330
- "preventScroll",
331
- "restoreFocus",
332
- "role",
333
- "trapFocus"
334
- ]);
335
- const splitProps = createSplitProps(props);
336
-
337
- export { anatomy, connect, machine, props, splitProps };