@destyler/popover 0.0.7-beta.7 → 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.cjs DELETED
@@ -1,443 +0,0 @@
1
- 'use strict';
2
-
3
- const anatomy$1 = require('@zag-js/anatomy');
4
- const domQuery = require('@zag-js/dom-query');
5
- const popper = require('@zag-js/popper');
6
- const ariaHidden = require('@zag-js/aria-hidden');
7
- const core = require('@zag-js/core');
8
- const dismissable = require('@zag-js/dismissable');
9
- const focusTrap = require('@zag-js/focus-trap');
10
- const removeScroll = require('@zag-js/remove-scroll');
11
- const utils = require('@zag-js/utils');
12
- const types = require('@zag-js/types');
13
-
14
- const anatomy = anatomy$1.createAnatomy("popover").parts(
15
- "arrow",
16
- "arrowTip",
17
- "anchor",
18
- "trigger",
19
- "indicator",
20
- "positioner",
21
- "content",
22
- "title",
23
- "description",
24
- "closeTrigger"
25
- );
26
- const parts = anatomy.build();
27
-
28
- const dom = domQuery.createScope({
29
- getAnchorId: (ctx) => ctx.ids?.anchor ?? `popover:${ctx.id}:anchor`,
30
- getTriggerId: (ctx) => ctx.ids?.trigger ?? `popover:${ctx.id}:trigger`,
31
- getContentId: (ctx) => ctx.ids?.content ?? `popover:${ctx.id}:content`,
32
- getPositionerId: (ctx) => ctx.ids?.positioner ?? `popover:${ctx.id}:popper`,
33
- getArrowId: (ctx) => ctx.ids?.arrow ?? `popover:${ctx.id}:arrow`,
34
- getTitleId: (ctx) => ctx.ids?.title ?? `popover:${ctx.id}:title`,
35
- getDescriptionId: (ctx) => ctx.ids?.description ?? `popover:${ctx.id}:desc`,
36
- getCloseTriggerId: (ctx) => ctx.ids?.closeTrigger ?? `popover:${ctx.id}:close`,
37
- getAnchorEl: (ctx) => dom.getById(ctx, dom.getAnchorId(ctx)),
38
- getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
39
- getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
40
- getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
41
- getTitleEl: (ctx) => dom.getById(ctx, dom.getTitleId(ctx)),
42
- getDescriptionEl: (ctx) => dom.getById(ctx, dom.getDescriptionId(ctx)),
43
- getFocusableEls: (ctx) => domQuery.getFocusables(dom.getContentEl(ctx)),
44
- getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0]
45
- });
46
-
47
- function connect(state, send, normalize) {
48
- const open = state.matches("open");
49
- const currentPlacement = state.context.currentPlacement;
50
- const portalled = state.context.currentPortalled;
51
- const rendered = state.context.renderedElements;
52
- const popperStyles = popper.getPlacementStyles({
53
- ...state.context.positioning,
54
- placement: currentPlacement
55
- });
56
- return {
57
- portalled,
58
- open,
59
- setOpen(nextOpen) {
60
- if (nextOpen === open)
61
- return;
62
- send(nextOpen ? "OPEN" : "CLOSE");
63
- },
64
- reposition(options = {}) {
65
- send({ type: "POSITIONING.SET", options });
66
- },
67
- getArrowProps() {
68
- return normalize.element({
69
- id: dom.getArrowId(state.context),
70
- ...parts.arrow.attrs,
71
- dir: state.context.dir,
72
- style: popperStyles.arrow
73
- });
74
- },
75
- getArrowTipProps() {
76
- return normalize.element({
77
- ...parts.arrowTip.attrs,
78
- dir: state.context.dir,
79
- style: popperStyles.arrowTip
80
- });
81
- },
82
- getAnchorProps() {
83
- return normalize.element({
84
- ...parts.anchor.attrs,
85
- dir: state.context.dir,
86
- id: dom.getAnchorId(state.context)
87
- });
88
- },
89
- getTriggerProps() {
90
- return normalize.button({
91
- ...parts.trigger.attrs,
92
- "dir": state.context.dir,
93
- "type": "button",
94
- "data-placement": currentPlacement,
95
- "id": dom.getTriggerId(state.context),
96
- "aria-haspopup": "dialog",
97
- "aria-expanded": open,
98
- "data-state": open ? "open" : "closed",
99
- "aria-controls": dom.getContentId(state.context),
100
- onPointerDown(event) {
101
- if (domQuery.isSafari()) {
102
- event.currentTarget.focus();
103
- }
104
- },
105
- onClick(event) {
106
- if (event.defaultPrevented)
107
- return;
108
- send("TOGGLE");
109
- },
110
- onBlur(event) {
111
- send({ type: "TRIGGER_BLUR", target: event.relatedTarget });
112
- }
113
- });
114
- },
115
- getIndicatorProps() {
116
- return normalize.element({
117
- ...parts.indicator.attrs,
118
- "dir": state.context.dir,
119
- "data-state": open ? "open" : "closed"
120
- });
121
- },
122
- getPositionerProps() {
123
- return normalize.element({
124
- id: dom.getPositionerId(state.context),
125
- ...parts.positioner.attrs,
126
- dir: state.context.dir,
127
- style: popperStyles.floating
128
- });
129
- },
130
- getContentProps() {
131
- return normalize.element({
132
- ...parts.content.attrs,
133
- "dir": state.context.dir,
134
- "id": dom.getContentId(state.context),
135
- "tabIndex": -1,
136
- "role": "dialog",
137
- "hidden": !open,
138
- "data-state": open ? "open" : "closed",
139
- "data-expanded": domQuery.dataAttr(open),
140
- "aria-labelledby": rendered.title ? dom.getTitleId(state.context) : void 0,
141
- "aria-describedby": rendered.description ? dom.getDescriptionId(state.context) : void 0,
142
- "data-placement": currentPlacement
143
- });
144
- },
145
- getTitleProps() {
146
- return normalize.element({
147
- ...parts.title.attrs,
148
- id: dom.getTitleId(state.context),
149
- dir: state.context.dir
150
- });
151
- },
152
- getDescriptionProps() {
153
- return normalize.element({
154
- ...parts.description.attrs,
155
- id: dom.getDescriptionId(state.context),
156
- dir: state.context.dir
157
- });
158
- },
159
- getCloseTriggerProps() {
160
- return normalize.button({
161
- ...parts.closeTrigger.attrs,
162
- "dir": state.context.dir,
163
- "id": dom.getCloseTriggerId(state.context),
164
- "type": "button",
165
- "aria-label": "close",
166
- onClick(event) {
167
- if (event.defaultPrevented)
168
- return;
169
- send("CLOSE");
170
- }
171
- });
172
- }
173
- };
174
- }
175
-
176
- function machine(userContext) {
177
- const ctx = utils.compact(userContext);
178
- return core.createMachine(
179
- {
180
- id: "popover",
181
- initial: ctx.open ? "open" : "closed",
182
- context: {
183
- closeOnInteractOutside: true,
184
- closeOnEscape: true,
185
- autoFocus: true,
186
- modal: false,
187
- portalled: true,
188
- positioning: {
189
- placement: "bottom",
190
- ...ctx.positioning
191
- },
192
- currentPlacement: void 0,
193
- ...ctx,
194
- renderedElements: {
195
- title: true,
196
- description: true
197
- }
198
- },
199
- computed: {
200
- currentPortalled: (ctx2) => !!ctx2.modal || !!ctx2.portalled
201
- },
202
- watch: {
203
- open: ["toggleVisibility"]
204
- },
205
- entry: ["checkRenderedElements"],
206
- states: {
207
- closed: {
208
- on: {
209
- "CONTROLLED.OPEN": {
210
- target: "open",
211
- actions: ["setInitialFocus"]
212
- },
213
- "TOGGLE": [
214
- {
215
- guard: "isOpenControlled",
216
- actions: ["invokeOnOpen"]
217
- },
218
- {
219
- target: "open",
220
- actions: ["invokeOnOpen", "setInitialFocus"]
221
- }
222
- ],
223
- "OPEN": [
224
- {
225
- guard: "isOpenControlled",
226
- actions: ["invokeOnOpen"]
227
- },
228
- {
229
- target: "open",
230
- actions: ["invokeOnOpen", "setInitialFocus"]
231
- }
232
- ]
233
- }
234
- },
235
- open: {
236
- activities: [
237
- "trapFocus",
238
- "preventScroll",
239
- "hideContentBelow",
240
- "trackPositioning",
241
- "trackDismissableElement",
242
- "proxyTabFocus"
243
- ],
244
- on: {
245
- "CONTROLLED.CLOSE": {
246
- target: "closed",
247
- actions: ["setFinalFocus"]
248
- },
249
- "CLOSE": [
250
- {
251
- guard: "isOpenControlled",
252
- actions: ["invokeOnClose"]
253
- },
254
- {
255
- target: "closed",
256
- actions: ["invokeOnClose", "setFinalFocus"]
257
- }
258
- ],
259
- "TOGGLE": [
260
- {
261
- guard: "isOpenControlled",
262
- actions: ["invokeOnClose"]
263
- },
264
- {
265
- target: "closed",
266
- actions: ["invokeOnClose"]
267
- }
268
- ],
269
- "POSITIONING.SET": {
270
- actions: "reposition"
271
- }
272
- }
273
- }
274
- }
275
- },
276
- {
277
- guards: {
278
- isOpenControlled: (ctx2) => !!ctx2["open.controlled"]
279
- },
280
- activities: {
281
- trackPositioning(ctx2) {
282
- ctx2.currentPlacement = ctx2.positioning.placement;
283
- const anchorEl = dom.getAnchorEl(ctx2) ?? dom.getTriggerEl(ctx2);
284
- const getPositionerEl = () => dom.getPositionerEl(ctx2);
285
- return popper.getPlacement(anchorEl, getPositionerEl, {
286
- ...ctx2.positioning,
287
- defer: true,
288
- onComplete(data) {
289
- ctx2.currentPlacement = data.placement;
290
- }
291
- });
292
- },
293
- trackDismissableElement(ctx2, _evt, { send }) {
294
- const getContentEl = () => dom.getContentEl(ctx2);
295
- let restoreFocus = true;
296
- return dismissable.trackDismissableElement(getContentEl, {
297
- pointerBlocking: ctx2.modal,
298
- exclude: dom.getTriggerEl(ctx2),
299
- defer: true,
300
- onEscapeKeyDown(event) {
301
- ctx2.onEscapeKeyDown?.(event);
302
- if (ctx2.closeOnEscape)
303
- return;
304
- event.preventDefault();
305
- },
306
- onInteractOutside(event) {
307
- ctx2.onInteractOutside?.(event);
308
- if (event.defaultPrevented)
309
- return;
310
- restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
311
- if (!ctx2.closeOnInteractOutside) {
312
- event.preventDefault();
313
- }
314
- },
315
- onPointerDownOutside: ctx2.onPointerDownOutside,
316
- onFocusOutside: ctx2.onFocusOutside,
317
- persistentElements: ctx2.persistentElements,
318
- onDismiss() {
319
- send({ type: "CLOSE", src: "interact-outside", restoreFocus });
320
- }
321
- });
322
- },
323
- proxyTabFocus(ctx2) {
324
- if (ctx2.modal || !ctx2.portalled)
325
- return;
326
- const getContentEl = () => dom.getContentEl(ctx2);
327
- return domQuery.proxyTabFocus(getContentEl, {
328
- triggerElement: dom.getTriggerEl(ctx2),
329
- defer: true,
330
- onFocus(el) {
331
- el.focus({ preventScroll: true });
332
- }
333
- });
334
- },
335
- hideContentBelow(ctx2) {
336
- if (!ctx2.modal)
337
- return;
338
- const getElements = () => [dom.getContentEl(ctx2), dom.getTriggerEl(ctx2)];
339
- return ariaHidden.ariaHidden(getElements, { defer: true });
340
- },
341
- preventScroll(ctx2) {
342
- if (!ctx2.modal)
343
- return;
344
- return removeScroll.preventBodyScroll(dom.getDoc(ctx2));
345
- },
346
- trapFocus(ctx2) {
347
- if (!ctx2.modal)
348
- return;
349
- const contentEl = () => dom.getContentEl(ctx2);
350
- return focusTrap.trapFocus(contentEl, {
351
- initialFocus: () => domQuery.getInitialFocus({
352
- root: dom.getContentEl(ctx2),
353
- getInitialEl: ctx2.initialFocusEl,
354
- enabled: ctx2.autoFocus
355
- })
356
- });
357
- }
358
- },
359
- actions: {
360
- reposition(ctx2, evt) {
361
- const anchorEl = dom.getAnchorEl(ctx2) ?? dom.getTriggerEl(ctx2);
362
- const getPositionerEl = () => dom.getPositionerEl(ctx2);
363
- popper.getPlacement(anchorEl, getPositionerEl, {
364
- ...ctx2.positioning,
365
- ...evt.options,
366
- defer: true,
367
- listeners: false,
368
- onComplete(data) {
369
- ctx2.currentPlacement = data.placement;
370
- }
371
- });
372
- },
373
- checkRenderedElements(ctx2) {
374
- domQuery.raf(() => {
375
- Object.assign(ctx2.renderedElements, {
376
- title: !!dom.getTitleEl(ctx2),
377
- description: !!dom.getDescriptionEl(ctx2)
378
- });
379
- });
380
- },
381
- setInitialFocus(ctx2) {
382
- if (ctx2.modal)
383
- return;
384
- domQuery.raf(() => {
385
- const element = domQuery.getInitialFocus({
386
- root: dom.getContentEl(ctx2),
387
- getInitialEl: ctx2.initialFocusEl,
388
- enabled: ctx2.autoFocus
389
- });
390
- element?.focus({ preventScroll: true });
391
- });
392
- },
393
- setFinalFocus(ctx2, evt) {
394
- const restoreFocus = evt.restoreFocus ?? evt.previousEvent?.restoreFocus;
395
- if (restoreFocus != null && !restoreFocus)
396
- return;
397
- domQuery.raf(() => {
398
- const element = dom.getTriggerEl(ctx2);
399
- element?.focus({ preventScroll: true });
400
- });
401
- },
402
- invokeOnOpen(ctx2) {
403
- ctx2.onOpenChange?.({ open: true });
404
- },
405
- invokeOnClose(ctx2) {
406
- ctx2.onOpenChange?.({ open: false });
407
- },
408
- toggleVisibility(ctx2, evt, { send }) {
409
- send({ type: ctx2.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE", previousEvent: evt });
410
- }
411
- }
412
- }
413
- );
414
- }
415
-
416
- const props = types.createProps()([
417
- "autoFocus",
418
- "closeOnEscape",
419
- "closeOnInteractOutside",
420
- "dir",
421
- "getRootNode",
422
- "id",
423
- "ids",
424
- "initialFocusEl",
425
- "modal",
426
- "onEscapeKeyDown",
427
- "onFocusOutside",
428
- "onInteractOutside",
429
- "onOpenChange",
430
- "onPointerDownOutside",
431
- "open.controlled",
432
- "open",
433
- "persistentElements",
434
- "portalled",
435
- "positioning"
436
- ]);
437
- const splitProps = utils.createSplitProps(props);
438
-
439
- exports.anatomy = anatomy;
440
- exports.connect = connect;
441
- exports.machine = machine;
442
- exports.props = props;
443
- exports.splitProps = splitProps;
package/dist/index.d.cts DELETED
@@ -1,152 +0,0 @@
1
- import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { CommonProperties, DirectionProperty, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
3
- import * as _zag_js_core from '@zag-js/core';
4
- import { StateMachine, Machine } from '@zag-js/core';
5
- import { DismissableElementHandlers, PersistentElementOptions } from '@zag-js/dismissable';
6
- export { FocusOutsideEvent, InteractOutsideEvent, PointerDownOutsideEvent } from '@zag-js/dismissable';
7
- import { PositioningOptions, Placement } from '@zag-js/popper';
8
- export { Placement, PositioningOptions } from '@zag-js/popper';
9
-
10
- declare const anatomy: _zag_js_anatomy.AnatomyInstance<"arrow" | "arrowTip" | "anchor" | "trigger" | "indicator" | "positioner" | "content" | "title" | "description" | "closeTrigger">;
11
-
12
- interface OpenChangeDetails {
13
- open: boolean;
14
- }
15
- type ElementIds = Partial<{
16
- anchor: string;
17
- trigger: string;
18
- content: string;
19
- title: string;
20
- description: string;
21
- closeTrigger: string;
22
- positioner: string;
23
- arrow: string;
24
- }>;
25
- interface PublicContext extends CommonProperties, DirectionProperty, DismissableElementHandlers, PersistentElementOptions {
26
- /**
27
- * The ids of the elements in the popover. Useful for composition.
28
- */
29
- 'ids'?: ElementIds | undefined;
30
- /**
31
- * Whether the popover should be modal. When set to `true`:
32
- * - interaction with outside elements will be disabled
33
- * - only popover content will be visible to screen readers
34
- * - scrolling is blocked
35
- * - focus is trapped within the popover
36
- *
37
- * @default false
38
- */
39
- 'modal'?: boolean | undefined;
40
- /**
41
- * Whether the popover is portalled. This will proxy the tabbing behavior regardless of the DOM position
42
- * of the popover content.
43
- *
44
- * @default true
45
- */
46
- 'portalled'?: boolean | undefined;
47
- /**
48
- * Whether to automatically set focus on the first focusable
49
- * content within the popover when opened.
50
- *
51
- * @default true
52
- */
53
- 'autoFocus'?: boolean | undefined;
54
- /**
55
- * The element to focus on when the popover is opened.
56
- */
57
- 'initialFocusEl'?: (() => HTMLElement | null) | undefined;
58
- /**
59
- * Whether to close the popover when the user clicks outside of the popover.
60
- * @default true
61
- */
62
- 'closeOnInteractOutside'?: boolean | undefined;
63
- /**
64
- * Whether to close the popover when the escape key is pressed.
65
- * @default true
66
- */
67
- 'closeOnEscape'?: boolean | undefined;
68
- /**
69
- * Function invoked when the popover opens or closes
70
- */
71
- 'onOpenChange'?: ((details: OpenChangeDetails) => void) | undefined;
72
- /**
73
- * The user provided options used to position the popover content
74
- */
75
- 'positioning': PositioningOptions;
76
- /**
77
- * Whether the popover is open
78
- */
79
- 'open'?: boolean | undefined;
80
- /**
81
- * Whether the popover is controlled by the user
82
- */
83
- 'open.controlled'?: boolean | undefined;
84
- }
85
- type UserDefinedContext = RequiredBy<PublicContext, 'id'>;
86
- type ComputedContext = Readonly<{
87
- /**
88
- * @computed
89
- * The computed value of `portalled`
90
- */
91
- currentPortalled: boolean;
92
- }>;
93
- interface PrivateContext {
94
- /**
95
- * @internal
96
- * The elements that are rendered on mount
97
- */
98
- renderedElements: {
99
- title: boolean;
100
- description: boolean;
101
- };
102
- /**
103
- * @internal
104
- * The computed placement (maybe different from initial placement)
105
- */
106
- currentPlacement?: Placement | undefined;
107
- }
108
- interface MachineContext extends PublicContext, ComputedContext, PrivateContext {
109
- }
110
- interface MachineState {
111
- value: 'open' | 'closed';
112
- }
113
- type State = StateMachine.State<MachineContext, MachineState>;
114
- type Send = StateMachine.Send<StateMachine.AnyEventObject>;
115
- type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
116
- interface MachineApi<T extends PropTypes = PropTypes> {
117
- /**
118
- * Whether the popover is portalled.
119
- */
120
- portalled: boolean;
121
- /**
122
- * Whether the popover is open
123
- */
124
- open: boolean;
125
- /**
126
- * Function to open or close the popover
127
- */
128
- setOpen: (open: boolean) => void;
129
- /**
130
- * Function to reposition the popover
131
- */
132
- reposition: (options?: Partial<PositioningOptions>) => void;
133
- getArrowProps: () => T['element'];
134
- getArrowTipProps: () => T['element'];
135
- getAnchorProps: () => T['element'];
136
- getTriggerProps: () => T['button'];
137
- getIndicatorProps: () => T['element'];
138
- getPositionerProps: () => T['element'];
139
- getContentProps: () => T['element'];
140
- getTitleProps: () => T['element'];
141
- getDescriptionProps: () => T['element'];
142
- getCloseTriggerProps: () => T['button'];
143
- }
144
-
145
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
146
-
147
- declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
148
-
149
- declare const props: ("ids" | "modal" | "portalled" | "autoFocus" | "initialFocusEl" | "closeOnInteractOutside" | "closeOnEscape" | "onOpenChange" | "positioning" | "open" | "open.controlled" | "id" | "getRootNode" | "dir" | "onEscapeKeyDown" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside" | "persistentElements")[];
150
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "ids" | "modal" | "portalled" | "autoFocus" | "initialFocusEl" | "closeOnInteractOutside" | "closeOnEscape" | "onOpenChange" | "positioning" | "open" | "open.controlled" | "id" | "getRootNode" | "dir" | "onEscapeKeyDown" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside" | "persistentElements">];
151
-
152
- export { type MachineApi as Api, type UserDefinedContext as Context, type ElementIds, type OpenChangeDetails, type Service, anatomy, connect, machine, props, splitProps };