@destyler/dialog 0.0.1-beta.9 → 0.0.2

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.
@@ -0,0 +1,537 @@
1
+ import { defineComponent, ref, toRefs, h, mergeProps, onMounted, withDirectives } from 'vue';
2
+ import { DestylerPrimitive } from '@destyler/primitive';
3
+ import { createContext, getOpenState } from '@destyler/shared';
4
+ import { useVModel, useId, useForwardExpose, useEmitAsProps, useHideOthers } from '@destyler/composition';
5
+ import { DestylerPresence } from '@destyler/presence';
6
+ import { DestylerFocusScope } from '@destyler/focus-scope';
7
+ import { DestylerDismissableLayer } from '@destyler/dismissable-layer';
8
+ import { BindOnceDirective } from '@destyler/directives';
9
+ import { DestylerTeleport } from '@destyler/teleport';
10
+
11
+ const destylerDialogRootProps = {
12
+ open: {
13
+ type: Boolean,
14
+ required: false,
15
+ default: void 0
16
+ },
17
+ defaultOpen: {
18
+ type: Boolean,
19
+ required: false,
20
+ default: false
21
+ },
22
+ modal: {
23
+ type: Boolean,
24
+ required: false,
25
+ default: true
26
+ }
27
+ };
28
+ const [injectDialogRootContext, provideDialogRootContext] = createContext("DestylerDialogRoot");
29
+ const DestylerDialogRoot = defineComponent({
30
+ name: "DestylerDialogRoot",
31
+ props: destylerDialogRootProps,
32
+ emits: ["update:open"],
33
+ setup(props, { emit }) {
34
+ const open = useVModel(props, "open", emit, {
35
+ defaultValue: props.defaultOpen,
36
+ passive: props.open === void 0
37
+ });
38
+ const triggerElement = ref();
39
+ const contentElement = ref();
40
+ const { modal } = toRefs(props);
41
+ provideDialogRootContext({
42
+ open,
43
+ modal,
44
+ openModal: () => {
45
+ open.value = true;
46
+ },
47
+ onOpenChange: (value) => {
48
+ open.value = value;
49
+ },
50
+ onOpenToggle: () => {
51
+ open.value = !open.value;
52
+ },
53
+ contentId: useId(),
54
+ titleId: useId(),
55
+ descriptionId: useId(),
56
+ triggerElement,
57
+ contentElement
58
+ });
59
+ },
60
+ render() {
61
+ return this.$slots.default?.();
62
+ }
63
+ });
64
+
65
+ const destylerDialogCloseProps = {
66
+ asChild: {
67
+ type: Boolean,
68
+ required: false,
69
+ default: false
70
+ },
71
+ as: {
72
+ type: [String, Object],
73
+ required: false,
74
+ default: "button"
75
+ }
76
+ };
77
+ const DestylerDialogClose = defineComponent({
78
+ name: "DestylerDialogClose",
79
+ props: destylerDialogCloseProps,
80
+ setup() {
81
+ const rootContext = injectDialogRootContext();
82
+ return {
83
+ rootContext
84
+ };
85
+ },
86
+ render() {
87
+ return h(DestylerPrimitive, mergeProps(this.$props, {
88
+ type: this.$props.as === "button" ? "button" : void 0,
89
+ onClick: () => {
90
+ this.rootContext.onOpenChange(false);
91
+ }
92
+ }), {
93
+ default: () => this.$slots.default?.()
94
+ });
95
+ }
96
+ });
97
+
98
+ const destylerDialogContentImplProps = {
99
+ as: {
100
+ type: [String, Object],
101
+ required: false,
102
+ default: "div"
103
+ },
104
+ asChild: {
105
+ type: Boolean,
106
+ required: false,
107
+ default: false
108
+ },
109
+ forceMount: {
110
+ type: Boolean,
111
+ required: false
112
+ },
113
+ trapFocus: {
114
+ type: Boolean,
115
+ required: false,
116
+ default: false
117
+ }
118
+ };
119
+ const DestylerDialogContentImpl = defineComponent({
120
+ name: "DestylerDialogContentImpl",
121
+ props: destylerDialogContentImplProps,
122
+ emits: ["openAutoFocus", "closeAutoFocus", "escapeKeyDown", "pointerDownOutside", "focusOutside", "interactOutside", "dismiss"],
123
+ setup() {
124
+ const rootContext = injectDialogRootContext();
125
+ const { forwardRef, currentElement: contentElement } = useForwardExpose();
126
+ onMounted(() => {
127
+ rootContext.contentElement = contentElement;
128
+ });
129
+ return {
130
+ rootContext,
131
+ forwardRef
132
+ };
133
+ },
134
+ render() {
135
+ return h(DestylerFocusScope, {
136
+ asChild: true,
137
+ loop: true,
138
+ trapped: this.$props.trapFocus,
139
+ onMountAutoFocus: (event) => {
140
+ this.$emit("openAutoFocus", event);
141
+ },
142
+ onUnmountAutoFocus: (event) => {
143
+ this.$emit("closeAutoFocus", event);
144
+ }
145
+ }, withDirectives(h(DestylerDismissableLayer, mergeProps(this.$attrs, {
146
+ "ref": (el) => this.forwardRef(el),
147
+ "as": this.$props.as,
148
+ "asChild": this.$props.asChild,
149
+ "role": "dialog",
150
+ "aria-describedby": this.rootContext.descriptionId,
151
+ "aria-labelledby": this.rootContext.titleId,
152
+ "data-state": getOpenState(this.rootContext.open.value),
153
+ "isDismissable": true,
154
+ "onDismiss": () => {
155
+ this.rootContext.onOpenChange(false);
156
+ },
157
+ "onEscapeKeyDown": (event) => {
158
+ this.$emit("escapeKeyDown", event);
159
+ },
160
+ "onFocusOutside": (event) => {
161
+ this.$emit("focusOutside", event);
162
+ },
163
+ "onInteractOutside": (event) => {
164
+ this.$emit("interactOutside", event);
165
+ },
166
+ "onPointerDownOutside": (event) => {
167
+ this.$emit("pointerDownOutside", event);
168
+ }
169
+ }), {
170
+ default: () => this.$slots.default?.()
171
+ }), [
172
+ [BindOnceDirective, { id: this.rootContext.contentId }]
173
+ ]));
174
+ }
175
+ });
176
+
177
+ const DestylerDialogContentModal = defineComponent({
178
+ name: "DestylerDialogContentModal",
179
+ props: destylerDialogContentImplProps,
180
+ emits: ["openAutoFocus", "closeAutoFocus", "escapeKeyDown", "pointerDownOutside", "focusOutside", "interactOutside", "dismiss"],
181
+ setup(_, { emit }) {
182
+ const rootContext = injectDialogRootContext();
183
+ const emitsAsProps = useEmitAsProps(emit);
184
+ const { forwardRef, currentElement } = useForwardExpose();
185
+ useHideOthers(currentElement);
186
+ return {
187
+ rootContext,
188
+ emitsAsProps,
189
+ forwardRef
190
+ };
191
+ },
192
+ render() {
193
+ return h(DestylerDialogContentImpl, mergeProps(this.$props, this.emitsAsProps, {
194
+ "ref": (el) => this.forwardRef(el),
195
+ "trap-focus": this.rootContext.open.value,
196
+ "disableOutsidePointerEvents": true,
197
+ "onCloseAutoFocus": (event) => {
198
+ this.$emit("closeAutoFocus", event);
199
+ if (!event.defaultPrevented) {
200
+ event.preventDefault();
201
+ this.rootContext.triggerElement.value?.focus();
202
+ }
203
+ },
204
+ "onPointerDownOutside": (event) => {
205
+ const originalEvent = event.detail.originalEvent;
206
+ const ctrlLeftClick = originalEvent.button === 0 && originalEvent.ctrlKey === true;
207
+ const isRightClick = originalEvent.button === 2 || ctrlLeftClick;
208
+ if (isRightClick)
209
+ event.preventDefault();
210
+ },
211
+ "onFocusOutside": (event) => {
212
+ event.preventDefault();
213
+ },
214
+ "onOpenAutoFocus": (event) => {
215
+ this.$emit("openAutoFocus", event);
216
+ }
217
+ }), {
218
+ default: () => this.$slots.default?.()
219
+ });
220
+ }
221
+ });
222
+
223
+ const DestylerDialogContentNonModal = defineComponent({
224
+ name: "DestylerDialogContentNonModal",
225
+ props: destylerDialogContentImplProps,
226
+ emits: ["openAutoFocus", "closeAutoFocus", "escapeKeyDown", "pointerDownOutside", "focusOutside", "interactOutside", "dismiss"],
227
+ setup(_, { emit }) {
228
+ const rootContext = injectDialogRootContext();
229
+ const hasInteractedOutsideRef = ref(false);
230
+ const hasPointerDownOutsideRef = ref(false);
231
+ const emitsAsProps = useEmitAsProps(emit);
232
+ return {
233
+ rootContext,
234
+ hasInteractedOutsideRef,
235
+ hasPointerDownOutsideRef,
236
+ emitsAsProps
237
+ };
238
+ },
239
+ render() {
240
+ return h(DestylerDialogContentImpl, mergeProps(this.$props, this.emitsAsProps, {
241
+ "trap-focus": false,
242
+ "disableOutsidePointerEvents": false,
243
+ "onCloseAutoFocus": (event) => {
244
+ this.$emit("closeAutoFocus", event);
245
+ if (!event.defaultPrevented) {
246
+ if (!this.hasInteractedOutsideRef)
247
+ this.rootContext.triggerElement.value?.focus();
248
+ event.preventDefault();
249
+ }
250
+ this.hasInteractedOutsideRef = false;
251
+ this.hasPointerDownOutsideRef = false;
252
+ },
253
+ "onInteractOutside": (event) => {
254
+ if (!event.defaultPrevented) {
255
+ this.hasInteractedOutsideRef = true;
256
+ if (event.detail.originalEvent.type === "pointerdown")
257
+ this.hasPointerDownOutsideRef = true;
258
+ }
259
+ const target = event.target;
260
+ const targetIsTrigger = this.rootContext.triggerElement.value?.contains(target);
261
+ if (targetIsTrigger)
262
+ event.preventDefault();
263
+ if (event.detail.originalEvent.type === "focusin" && this.hasPointerDownOutsideRef)
264
+ event.preventDefault();
265
+ }
266
+ }), {
267
+ default: () => this.$slots.default?.()
268
+ });
269
+ }
270
+ });
271
+
272
+ const destylerDialogContentProps = {
273
+ as: {
274
+ type: [String, Object],
275
+ required: false,
276
+ default: "div"
277
+ },
278
+ asChild: {
279
+ type: Boolean,
280
+ required: false,
281
+ default: false
282
+ },
283
+ forceMount: {
284
+ type: Boolean,
285
+ required: false
286
+ },
287
+ trapFocus: {
288
+ type: Boolean,
289
+ required: false,
290
+ default: false
291
+ }
292
+ };
293
+ const DestylerDialogContent = defineComponent({
294
+ name: "DestylerDialogContent",
295
+ props: destylerDialogContentProps,
296
+ emits: ["openAutoFocus", "closeAutoFocus", "escapeKeyDown", "pointerDownOutside", "focusOutside", "interactOutside", "dismiss"],
297
+ setup(_, { emit }) {
298
+ const rootContext = injectDialogRootContext();
299
+ const emitsAsProps = useEmitAsProps(emit);
300
+ const { forwardRef } = useForwardExpose();
301
+ return {
302
+ rootContext,
303
+ emitsAsProps,
304
+ forwardRef
305
+ };
306
+ },
307
+ render() {
308
+ const useVShow = this.rootContext.modal.value;
309
+ return h(DestylerPresence, {
310
+ present: this.$props.forceMount || this.rootContext.open.value
311
+ }, [
312
+ useVShow ? h(DestylerDialogContentModal, mergeProps(this.$props, this.emitsAsProps, this.$attrs, {
313
+ ref: (el) => this.forwardRef(el),
314
+ onOpenAutoFocus: (event) => {
315
+ this.$emit("openAutoFocus", event);
316
+ }
317
+ }), {
318
+ default: () => this.$slots.default?.()
319
+ }) : h(DestylerDialogContentNonModal, mergeProps(this.$props, this.emitsAsProps, this.$attrs), {
320
+ ref: (el) => this.forwardRef(el),
321
+ default: () => this.$slots.default?.()
322
+ })
323
+ ]);
324
+ }
325
+ });
326
+
327
+ const destylerDialogDescriptionProps = {
328
+ asChild: {
329
+ type: Boolean,
330
+ required: false,
331
+ default: false
332
+ },
333
+ as: {
334
+ type: [String, Object],
335
+ required: false,
336
+ default: "p"
337
+ }
338
+ };
339
+ const DestylerDialogDescription = defineComponent({
340
+ name: "DestylerDialogDescription",
341
+ props: destylerDialogDescriptionProps,
342
+ setup() {
343
+ const rootContext = injectDialogRootContext();
344
+ return {
345
+ rootContext
346
+ };
347
+ },
348
+ render() {
349
+ return withDirectives(h(DestylerPrimitive, {
350
+ as: this.$props.as,
351
+ asChild: this.$props.asChild
352
+ }, {
353
+ default: () => this.$slots.default?.()
354
+ }), [
355
+ [BindOnceDirective, { id: this.rootContext.descriptionId }]
356
+ ]);
357
+ }
358
+ });
359
+
360
+ const destylerDialogOverlayImplProps = {
361
+ as: {
362
+ type: [String, Object],
363
+ required: false,
364
+ default: "div"
365
+ },
366
+ asChild: {
367
+ type: Boolean,
368
+ required: false,
369
+ default: false
370
+ }
371
+ };
372
+ const DestylerDialogOverlayImpl = defineComponent({
373
+ name: "DestylerDialogOverlayImpl",
374
+ props: destylerDialogOverlayImplProps,
375
+ setup() {
376
+ const rootContext = injectDialogRootContext();
377
+ useForwardExpose();
378
+ return {
379
+ rootContext
380
+ };
381
+ },
382
+ render() {
383
+ return h(DestylerPrimitive, {
384
+ "as": this.$props.as,
385
+ "asChild": this.$props.asChild,
386
+ "data-state": this.rootContext.open.value ? "open" : "closed",
387
+ "style": {
388
+ "pointer-events": "auto"
389
+ }
390
+ }, {
391
+ default: () => this.$slots.default?.()
392
+ });
393
+ }
394
+ });
395
+
396
+ const destylerDialogOverlayProps = {
397
+ as: {
398
+ type: [String, Object],
399
+ required: false,
400
+ default: "div"
401
+ },
402
+ asChild: {
403
+ type: Boolean,
404
+ required: false,
405
+ default: false
406
+ },
407
+ forceMount: {
408
+ type: Boolean,
409
+ required: false
410
+ }
411
+ };
412
+ const DestylerDialogOverlay = defineComponent({
413
+ name: "DestylerDialogOverlay",
414
+ props: destylerDialogOverlayProps,
415
+ setup() {
416
+ const rootContext = injectDialogRootContext();
417
+ const { forwardRef } = useForwardExpose();
418
+ return {
419
+ rootContext,
420
+ forwardRef
421
+ };
422
+ },
423
+ render() {
424
+ const useVShow = this.rootContext?.modal.value;
425
+ return useVShow ? h(DestylerPresence, {
426
+ present: this.$props.forceMount || this.rootContext.open.value
427
+ }, h(DestylerDialogOverlayImpl, mergeProps(this.$attrs, {
428
+ ref: (el) => this.forwardRef(el),
429
+ as: this.$props.as,
430
+ asChild: this.$props.asChild
431
+ }), {
432
+ default: () => this.$slots.default?.()
433
+ })) : null;
434
+ }
435
+ });
436
+
437
+ const destylerDialogPortalProps = {
438
+ as: {
439
+ type: [String, Object],
440
+ required: false,
441
+ default: "div"
442
+ },
443
+ asChild: {
444
+ type: Boolean,
445
+ required: false,
446
+ default: false
447
+ }
448
+ };
449
+ const DestylerDialogPortal = defineComponent({
450
+ name: "DestylerDialogPortal",
451
+ props: destylerDialogPortalProps,
452
+ render() {
453
+ return h(DestylerTeleport, {}, {
454
+ default: () => this.$slots.default?.()
455
+ });
456
+ }
457
+ });
458
+
459
+ const destylerDialogTitleProps = {
460
+ asChild: {
461
+ type: Boolean,
462
+ required: false,
463
+ default: false
464
+ },
465
+ as: {
466
+ type: [String, Object],
467
+ required: false,
468
+ default: "h2"
469
+ }
470
+ };
471
+ const DestylerDialogTitle = defineComponent({
472
+ name: "DestylerDialogTitle",
473
+ props: destylerDialogTitleProps,
474
+ setup() {
475
+ const rootContext = injectDialogRootContext();
476
+ return {
477
+ rootContext
478
+ };
479
+ },
480
+ render() {
481
+ return withDirectives(h(DestylerPrimitive, {
482
+ as: this.$props.as,
483
+ asChild: this.$props.asChild
484
+ }, {
485
+ default: () => this.$slots.default?.()
486
+ }), [
487
+ [BindOnceDirective, { id: this.rootContext.titleId }]
488
+ ]);
489
+ }
490
+ });
491
+
492
+ const destylerDialogTriggerProps = {
493
+ asChild: {
494
+ type: Boolean,
495
+ required: false,
496
+ default: false
497
+ },
498
+ as: {
499
+ type: [String, Object],
500
+ required: false,
501
+ default: "button"
502
+ }
503
+ };
504
+ const DestylerDialogTrigger = defineComponent({
505
+ name: "DestylerDialogTrigger",
506
+ props: destylerDialogTriggerProps,
507
+ setup() {
508
+ const rootContext = injectDialogRootContext();
509
+ const { forwardRef, currentElement } = useForwardExpose();
510
+ onMounted(() => {
511
+ rootContext.triggerElement = currentElement;
512
+ });
513
+ return {
514
+ rootContext,
515
+ forwardRef
516
+ };
517
+ },
518
+ render() {
519
+ return h(DestylerPrimitive, mergeProps(this.$attrs, {
520
+ "as": this.$props.as,
521
+ "asChild": this.$props.asChild,
522
+ "ref": (el) => this.forwardRef(el),
523
+ "type": this.$props.as === "button" ? "button" : void 0,
524
+ "aria-haspopup": "dialog",
525
+ "aria-expanded": this.rootContext.open.value || false,
526
+ "aria-controls": this.rootContext.contentId,
527
+ "data-state": this.rootContext.open.value ? "open" : "closed",
528
+ "onClick": () => {
529
+ this.rootContext.onOpenToggle();
530
+ }
531
+ }), {
532
+ default: () => this.$slots.default?.()
533
+ });
534
+ }
535
+ });
536
+
537
+ export { DestylerDialogClose, DestylerDialogContent, DestylerDialogContentImpl, DestylerDialogContentModal, DestylerDialogContentNonModal, DestylerDialogDescription, DestylerDialogOverlay, DestylerDialogOverlayImpl, DestylerDialogPortal, DestylerDialogRoot, DestylerDialogTitle, DestylerDialogTrigger };