@zag-js/popover 0.10.2 → 0.10.4

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.
@@ -1,232 +0,0 @@
1
- import {
2
- dom
3
- } from "./chunk-UA4OMIJI.mjs";
4
-
5
- // src/popover.machine.ts
6
- import { ariaHidden } from "@zag-js/aria-hidden";
7
- import { createMachine } from "@zag-js/core";
8
- import { trackDismissableElement } from "@zag-js/dismissable";
9
- import { nextTick, raf } from "@zag-js/dom-query";
10
- import { getPlacement } from "@zag-js/popper";
11
- import { preventBodyScroll } from "@zag-js/remove-scroll";
12
- import { proxyTabFocus } from "@zag-js/tabbable";
13
- import { compact, runIfFn } from "@zag-js/utils";
14
- import { createFocusTrap } from "focus-trap";
15
- function machine(userContext) {
16
- const ctx = compact(userContext);
17
- return createMachine(
18
- {
19
- id: "popover",
20
- initial: ctx.open ? "open" : "closed",
21
- context: {
22
- closeOnInteractOutside: true,
23
- closeOnEsc: true,
24
- autoFocus: true,
25
- modal: false,
26
- positioning: {
27
- placement: "bottom",
28
- ...ctx.positioning
29
- },
30
- currentPlacement: void 0,
31
- ...ctx,
32
- renderedElements: {
33
- title: true,
34
- description: true
35
- }
36
- },
37
- computed: {
38
- currentPortalled: (ctx2) => !!ctx2.modal || !!ctx2.portalled
39
- },
40
- watch: {
41
- open: ["toggleVisibility"]
42
- },
43
- entry: ["checkRenderedElements"],
44
- states: {
45
- closed: {
46
- on: {
47
- TOGGLE: {
48
- target: "open",
49
- actions: ["invokeOnOpen"]
50
- },
51
- OPEN: {
52
- target: "open",
53
- actions: ["invokeOnOpen"]
54
- }
55
- }
56
- },
57
- open: {
58
- activities: [
59
- "trapFocus",
60
- "preventScroll",
61
- "hideContentBelow",
62
- "trackPositioning",
63
- "trackDismissableElement",
64
- "proxyTabFocus"
65
- ],
66
- entry: ["setInitialFocus"],
67
- on: {
68
- CLOSE: {
69
- target: "closed",
70
- actions: ["invokeOnClose"]
71
- },
72
- REQUEST_CLOSE: {
73
- target: "closed",
74
- actions: ["restoreFocusIfNeeded", "invokeOnClose"]
75
- },
76
- TOGGLE: {
77
- target: "closed",
78
- actions: ["invokeOnClose"]
79
- },
80
- SET_POSITIONING: {
81
- actions: "setPositioning"
82
- }
83
- }
84
- }
85
- }
86
- },
87
- {
88
- activities: {
89
- trackPositioning(ctx2) {
90
- ctx2.currentPlacement = ctx2.positioning.placement;
91
- const anchorEl = dom.getAnchorEl(ctx2) ?? dom.getTriggerEl(ctx2);
92
- const getPositionerEl = () => dom.getPositionerEl(ctx2);
93
- return getPlacement(anchorEl, getPositionerEl, {
94
- ...ctx2.positioning,
95
- defer: true,
96
- onComplete(data) {
97
- ctx2.currentPlacement = data.placement;
98
- },
99
- onCleanup() {
100
- ctx2.currentPlacement = void 0;
101
- }
102
- });
103
- },
104
- trackDismissableElement(ctx2, _evt, { send }) {
105
- const getContentEl = () => dom.getContentEl(ctx2);
106
- let restoreFocus = true;
107
- return trackDismissableElement(getContentEl, {
108
- pointerBlocking: ctx2.modal,
109
- exclude: dom.getTriggerEl(ctx2),
110
- defer: true,
111
- onEscapeKeyDown(event) {
112
- ctx2.onEscapeKeyDown?.(event);
113
- if (ctx2.closeOnEsc)
114
- return;
115
- event.preventDefault();
116
- },
117
- onInteractOutside(event) {
118
- ctx2.onInteractOutside?.(event);
119
- if (event.defaultPrevented)
120
- return;
121
- restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
122
- if (!ctx2.closeOnInteractOutside) {
123
- event.preventDefault();
124
- }
125
- },
126
- onPointerDownOutside(event) {
127
- ctx2.onPointerDownOutside?.(event);
128
- },
129
- onFocusOutside(event) {
130
- ctx2.onFocusOutside?.(event);
131
- },
132
- onDismiss() {
133
- send({ type: "REQUEST_CLOSE", src: "interact-outside", restoreFocus });
134
- }
135
- });
136
- },
137
- proxyTabFocus(ctx2) {
138
- if (ctx2.modal || !ctx2.portalled)
139
- return;
140
- const getContentEl = () => dom.getContentEl(ctx2);
141
- return proxyTabFocus(getContentEl, {
142
- triggerElement: dom.getTriggerEl(ctx2),
143
- defer: true,
144
- onFocus(el) {
145
- el.focus({ preventScroll: true });
146
- }
147
- });
148
- },
149
- hideContentBelow(ctx2) {
150
- if (!ctx2.modal)
151
- return;
152
- const getElements = () => [dom.getContentEl(ctx2), dom.getTriggerEl(ctx2)];
153
- return ariaHidden(getElements, { defer: true });
154
- },
155
- preventScroll(ctx2) {
156
- if (!ctx2.modal)
157
- return;
158
- return preventBodyScroll(dom.getDoc(ctx2));
159
- },
160
- trapFocus(ctx2) {
161
- if (!ctx2.modal)
162
- return;
163
- let trap;
164
- nextTick(() => {
165
- const el = dom.getContentEl(ctx2);
166
- if (!el)
167
- return;
168
- trap = createFocusTrap(el, {
169
- escapeDeactivates: false,
170
- allowOutsideClick: true,
171
- preventScroll: true,
172
- returnFocusOnDeactivate: true,
173
- document: dom.getDoc(ctx2),
174
- fallbackFocus: el,
175
- initialFocus: runIfFn(ctx2.initialFocusEl)
176
- });
177
- try {
178
- trap.activate();
179
- } catch {
180
- }
181
- });
182
- return () => trap?.deactivate();
183
- }
184
- },
185
- actions: {
186
- setPositioning(ctx2, evt) {
187
- const anchorEl = dom.getAnchorEl(ctx2) ?? dom.getTriggerEl(ctx2);
188
- const getPositionerEl = () => dom.getPositionerEl(ctx2);
189
- getPlacement(anchorEl, getPositionerEl, {
190
- ...ctx2.positioning,
191
- ...evt.options,
192
- defer: true,
193
- listeners: false
194
- });
195
- },
196
- checkRenderedElements(ctx2) {
197
- raf(() => {
198
- Object.assign(ctx2.renderedElements, {
199
- title: !!dom.getTitleEl(ctx2),
200
- description: !!dom.getDescriptionEl(ctx2)
201
- });
202
- });
203
- },
204
- setInitialFocus(ctx2) {
205
- raf(() => {
206
- dom.getInitialFocusEl(ctx2)?.focus({ preventScroll: true });
207
- });
208
- },
209
- restoreFocusIfNeeded(ctx2, evt) {
210
- if (!evt.restoreFocus)
211
- return;
212
- raf(() => {
213
- dom.getTriggerEl(ctx2)?.focus({ preventScroll: true });
214
- });
215
- },
216
- invokeOnOpen(ctx2) {
217
- ctx2.onOpen?.();
218
- },
219
- invokeOnClose(ctx2) {
220
- ctx2.onClose?.();
221
- },
222
- toggleVisibility(ctx2, _evt, { send }) {
223
- send({ type: ctx2.open ? "OPEN" : "CLOSE", src: "controlled" });
224
- }
225
- }
226
- }
227
- );
228
- }
229
-
230
- export {
231
- machine
232
- };
@@ -1,113 +0,0 @@
1
- import {
2
- parts
3
- } from "./chunk-KTOPDXGV.mjs";
4
- import {
5
- dom
6
- } from "./chunk-UA4OMIJI.mjs";
7
-
8
- // src/popover.connect.ts
9
- import { dataAttr } from "@zag-js/dom-query";
10
- import { getPlacementStyles } from "@zag-js/popper";
11
- function connect(state, send, normalize) {
12
- const isOpen = state.matches("open");
13
- const currentPlacement = state.context.currentPlacement;
14
- const portalled = state.context.currentPortalled;
15
- const rendered = state.context.renderedElements;
16
- const popperStyles = getPlacementStyles({
17
- placement: currentPlacement
18
- });
19
- return {
20
- /**
21
- * Whether the popover is portalled
22
- */
23
- portalled,
24
- /**
25
- * Whether the popover is open
26
- */
27
- isOpen,
28
- /**
29
- * Function to open the popover
30
- */
31
- open() {
32
- send("OPEN");
33
- },
34
- /**
35
- * Function to close the popover
36
- */
37
- close() {
38
- send("CLOSE");
39
- },
40
- /**
41
- * Function to reposition the popover
42
- */
43
- setPositioning(options = {}) {
44
- send({ type: "SET_POSITIONING", options });
45
- },
46
- arrowProps: normalize.element({
47
- id: dom.getArrowId(state.context),
48
- ...parts.arrow.attrs,
49
- style: popperStyles.arrow
50
- }),
51
- arrowTipProps: normalize.element({
52
- ...parts.arrowTip.attrs,
53
- style: popperStyles.arrowTip
54
- }),
55
- anchorProps: normalize.element({
56
- ...parts.anchor.attrs,
57
- id: dom.getAnchorId(state.context)
58
- }),
59
- triggerProps: normalize.button({
60
- ...parts.trigger.attrs,
61
- type: "button",
62
- "data-placement": currentPlacement,
63
- id: dom.getTriggerId(state.context),
64
- "aria-haspopup": "dialog",
65
- "aria-expanded": isOpen,
66
- "data-expanded": dataAttr(isOpen),
67
- "aria-controls": dom.getContentId(state.context),
68
- onClick() {
69
- send("TOGGLE");
70
- },
71
- onBlur(event) {
72
- send({ type: "TRIGGER_BLUR", target: event.relatedTarget });
73
- }
74
- }),
75
- positionerProps: normalize.element({
76
- id: dom.getPositionerId(state.context),
77
- ...parts.positioner.attrs,
78
- style: popperStyles.floating
79
- }),
80
- contentProps: normalize.element({
81
- ...parts.content.attrs,
82
- id: dom.getContentId(state.context),
83
- tabIndex: -1,
84
- role: "dialog",
85
- hidden: !isOpen,
86
- "data-expanded": dataAttr(isOpen),
87
- "aria-labelledby": rendered.title ? dom.getTitleId(state.context) : void 0,
88
- "aria-describedby": rendered.description ? dom.getDescriptionId(state.context) : void 0,
89
- "data-placement": currentPlacement
90
- }),
91
- titleProps: normalize.element({
92
- ...parts.title.attrs,
93
- id: dom.getTitleId(state.context)
94
- }),
95
- descriptionProps: normalize.element({
96
- ...parts.description.attrs,
97
- id: dom.getDescriptionId(state.context)
98
- }),
99
- closeTriggerProps: normalize.button({
100
- ...parts.closeTrigger.attrs,
101
- id: dom.getCloseTriggerId(state.context),
102
- type: "button",
103
- "aria-label": "close",
104
- onClick() {
105
- send("REQUEST_CLOSE");
106
- }
107
- })
108
- };
109
- }
110
-
111
- export {
112
- connect
113
- };
@@ -1,19 +0,0 @@
1
- // src/popover.anatomy.ts
2
- import { createAnatomy } from "@zag-js/anatomy";
3
- var anatomy = createAnatomy("popover").parts(
4
- "arrow",
5
- "arrowTip",
6
- "anchor",
7
- "trigger",
8
- "positioner",
9
- "content",
10
- "title",
11
- "description",
12
- "closeTrigger"
13
- );
14
- var parts = anatomy.build();
15
-
16
- export {
17
- anatomy,
18
- parts
19
- };
@@ -1,39 +0,0 @@
1
- // src/popover.dom.ts
2
- import { createScope } from "@zag-js/dom-query";
3
- import { getFirstTabbable, getFocusables, getLastTabbable, getTabbables } from "@zag-js/tabbable";
4
- import { runIfFn } from "@zag-js/utils";
5
- var dom = createScope({
6
- getActiveEl: (ctx) => dom.getDoc(ctx).activeElement,
7
- getAnchorId: (ctx) => ctx.ids?.anchor ?? `popover:${ctx.id}:anchor`,
8
- getTriggerId: (ctx) => ctx.ids?.trigger ?? `popover:${ctx.id}:trigger`,
9
- getContentId: (ctx) => ctx.ids?.content ?? `popover:${ctx.id}:content`,
10
- getPositionerId: (ctx) => ctx.ids?.positioner ?? `popover:${ctx.id}:popper`,
11
- getArrowId: (ctx) => ctx.ids?.arrow ?? `popover:${ctx.id}:arrow`,
12
- getTitleId: (ctx) => ctx.ids?.title ?? `popover:${ctx.id}:title`,
13
- getDescriptionId: (ctx) => ctx.ids?.description ?? `popover:${ctx.id}:desc`,
14
- getCloseTriggerId: (ctx) => ctx.ids?.closeTrigger ?? `popover:${ctx.id}:close`,
15
- getAnchorEl: (ctx) => dom.getById(ctx, dom.getAnchorId(ctx)),
16
- getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
17
- getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
18
- getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
19
- getTitleEl: (ctx) => dom.getById(ctx, dom.getTitleId(ctx)),
20
- getDescriptionEl: (ctx) => dom.getById(ctx, dom.getDescriptionId(ctx)),
21
- getFocusableEls: (ctx) => getFocusables(dom.getContentEl(ctx)),
22
- getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0],
23
- getDocTabbableEls: (ctx) => getTabbables(dom.getDoc(ctx).body),
24
- getTabbableEls: (ctx) => getTabbables(dom.getContentEl(ctx), "if-empty"),
25
- getFirstTabbableEl: (ctx) => getFirstTabbable(dom.getContentEl(ctx), "if-empty"),
26
- getLastTabbableEl: (ctx) => getLastTabbable(dom.getContentEl(ctx), "if-empty"),
27
- getInitialFocusEl: (ctx) => {
28
- let el = runIfFn(ctx.initialFocusEl);
29
- if (!el && ctx.autoFocus)
30
- el = dom.getFirstFocusableEl(ctx);
31
- if (!el)
32
- el = dom.getContentEl(ctx);
33
- return el;
34
- }
35
- });
36
-
37
- export {
38
- dom
39
- };
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
-
16
- // src/popover.types.ts
17
- var popover_types_exports = {};
18
- module.exports = __toCommonJS(popover_types_exports);
File without changes