@zag-js/popover 2.0.0-next.2 → 2.0.0-next.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,180 +1,269 @@
1
- // src/popover.connect.ts
1
+ import { parts } from "./popover.anatomy.mjs";
2
+ import { getContentId, getDescriptionId, getTitleId } from "./popover.dom.mjs";
3
+ import { getViewportSizeStyle } from "./popover.style.mjs";
2
4
  import { getDismissableLayerAttrs, getDismissableLayerStyle } from "@zag-js/dismissable";
3
5
  import { ariaAttr, dataAttr, isLeftClick, isSafari } from "@zag-js/dom-query";
4
6
  import { getPlacementSide, getPlacementStyles } from "@zag-js/popper";
5
7
  import { mergeWithDefault } from "@zag-js/utils";
6
- import { parts } from "./popover.anatomy.mjs";
7
- import * as dom from "./popover.dom.mjs";
8
- var defaultTranslations = {
9
- closeTriggerLabel: "close"
10
- };
8
+ // src/popover.connect.ts
9
+ const defaultTranslations = { closeTriggerLabel: "close" };
11
10
  function connect(service, normalize) {
12
- const { state, context, send, prop, scope } = service;
13
- const layer = context.get("layer");
14
- const translations = mergeWithDefault(defaultTranslations, prop("translations"));
15
- const open = state.matches("open");
16
- const currentPlacement = context.get("currentPlacement");
17
- const currentPlacementSide = currentPlacement ? getPlacementSide(currentPlacement) : void 0;
18
- const rendered = context.get("renderedElements");
19
- const triggerValue = context.get("triggerValue");
20
- const popperStyles = getPlacementStyles({
21
- ...prop("positioning"),
22
- placement: currentPlacement
23
- });
24
- function getTriggerState(props = {}) {
25
- const { value } = props;
26
- const current = value == null ? false : triggerValue === value;
27
- return { value, current, open: value == null ? open : open && current };
28
- }
29
- function getPositionerState() {
30
- return { nested: !!layer?.nested, hasNested: !!layer?.hasNested };
31
- }
32
- function getContentState() {
33
- return {
34
- open,
35
- modal: !!prop("modal"),
36
- nested: !!layer?.nested,
37
- hasNested: !!layer?.hasNested,
38
- placement: currentPlacement,
39
- side: currentPlacementSide
40
- };
41
- }
42
- return {
43
- open,
44
- setOpen(nextOpen) {
45
- const open2 = state.matches("open");
46
- if (open2 === nextOpen) return;
47
- send({ type: nextOpen ? "OPEN" : "CLOSE" });
48
- },
49
- triggerValue,
50
- setTriggerValue(value) {
51
- send({ type: "TRIGGER_VALUE.SET", value });
52
- },
53
- reposition(options = {}) {
54
- send({ type: "POSITIONING.SET", options });
55
- },
56
- getArrowProps() {
57
- return normalize.element({
58
- ...parts.arrow.attrs(scope.id),
59
- dir: prop("dir"),
60
- style: popperStyles.arrow
61
- });
62
- },
63
- getArrowTipProps() {
64
- return normalize.element({
65
- ...parts.arrowTip.attrs(scope.id),
66
- dir: prop("dir"),
67
- style: popperStyles.arrowTip
68
- });
69
- },
70
- getAnchorProps() {
71
- return normalize.element({
72
- ...parts.anchor.attrs(scope.id),
73
- dir: prop("dir")
74
- });
75
- },
76
- getTriggerState,
77
- getTriggerProps(props = {}) {
78
- const { value } = props;
79
- const triggerState = getTriggerState(props);
80
- const { current } = triggerState;
81
- return normalize.button({
82
- ...parts.trigger.attrs(scope.id),
83
- dir: prop("dir"),
84
- type: "button",
85
- "data-placement": currentPlacement,
86
- "data-side": currentPlacementSide,
87
- "data-value": value,
88
- "data-current": dataAttr(current),
89
- "aria-haspopup": "dialog",
90
- "aria-expanded": triggerState.open,
91
- "data-state": open ? "open" : "closed",
92
- "aria-controls": dom.getContentId(scope),
93
- onPointerDown(event) {
94
- if (!isLeftClick(event)) return;
95
- if (isSafari()) {
96
- event.currentTarget.focus();
97
- }
98
- },
99
- onClick(event) {
100
- if (event.defaultPrevented) return;
101
- const shouldSwitch = open && value != null && !current;
102
- send({ type: shouldSwitch ? "TRIGGER_VALUE.SET" : "TOGGLE", value });
103
- },
104
- onBlur(event) {
105
- send({ type: "TRIGGER_BLUR", target: event.relatedTarget });
106
- }
107
- });
108
- },
109
- getIndicatorProps() {
110
- return normalize.element({
111
- ...parts.indicator.attrs(scope.id),
112
- dir: prop("dir"),
113
- "data-state": open ? "open" : "closed"
114
- });
115
- },
116
- getPositionerState,
117
- getPositionerProps() {
118
- return normalize.element({
119
- ...parts.positioner.attrs(scope.id),
120
- dir: prop("dir"),
121
- ...getDismissableLayerAttrs(layer),
122
- style: {
123
- ...popperStyles.floating,
124
- ...getDismissableLayerStyle(layer, { zIndex: true })
125
- }
126
- });
127
- },
128
- getContentState,
129
- getContentProps() {
130
- const contentState = getContentState();
131
- return normalize.element({
132
- ...parts.content.attrs(scope.id),
133
- dir: prop("dir"),
134
- id: dom.getContentId(scope),
135
- tabIndex: -1,
136
- role: "dialog",
137
- "aria-modal": ariaAttr(contentState.modal),
138
- hidden: !contentState.open,
139
- "data-state": contentState.open ? "open" : "closed",
140
- "data-expanded": dataAttr(contentState.open),
141
- "aria-labelledby": rendered.title ? dom.getTitleId(scope) : void 0,
142
- "aria-describedby": rendered.description ? dom.getDescriptionId(scope) : void 0,
143
- "data-placement": contentState.placement,
144
- "data-side": contentState.side,
145
- ...getDismissableLayerAttrs(layer),
146
- style: getDismissableLayerStyle(layer, { pointerEvents: true })
147
- });
148
- },
149
- getTitleProps() {
150
- return normalize.element({
151
- ...parts.title.attrs(scope.id),
152
- id: dom.getTitleId(scope),
153
- dir: prop("dir")
154
- });
155
- },
156
- getDescriptionProps() {
157
- return normalize.element({
158
- ...parts.description.attrs(scope.id),
159
- id: dom.getDescriptionId(scope),
160
- dir: prop("dir")
161
- });
162
- },
163
- getCloseTriggerProps() {
164
- return normalize.button({
165
- ...parts.closeTrigger.attrs(scope.id),
166
- dir: prop("dir"),
167
- type: "button",
168
- "aria-label": translations.closeTriggerLabel,
169
- onClick(event) {
170
- if (event.defaultPrevented) return;
171
- event.stopPropagation();
172
- send({ type: "CLOSE" });
173
- }
174
- });
175
- }
176
- };
11
+ const { state, context, send, prop, scope } = service;
12
+ const layer = context.get("layer");
13
+ const translations = mergeWithDefault(defaultTranslations, prop("translations"));
14
+ const open = state.matches("open");
15
+ const currentPlacement = context.get("currentPlacement");
16
+ const currentPlacementSide = currentPlacement ? getPlacementSide(currentPlacement) : void 0;
17
+ const rendered = context.get("renderedElements");
18
+ const triggerValue = context.get("triggerValue");
19
+ const transitioning = context.get("transitioning");
20
+ const instant = context.get("instant");
21
+ const activationDirection = context.get("activationDirection");
22
+ const viewportRendered = rendered.viewport;
23
+ const popperStyles = getPlacementStyles({
24
+ ...prop("positioning"),
25
+ placement: currentPlacement
26
+ });
27
+ function getTriggerState(props = {}) {
28
+ const { value } = props;
29
+ const current = value == null ? false : triggerValue === value;
30
+ return {
31
+ value,
32
+ current,
33
+ open: value == null ? open : open && current
34
+ };
35
+ }
36
+ function getPositionerState() {
37
+ return {
38
+ nested: !!layer?.nested,
39
+ hasNested: !!layer?.hasNested,
40
+ transitioning,
41
+ instant
42
+ };
43
+ }
44
+ function getContentState(props = {}) {
45
+ const { value } = props;
46
+ return {
47
+ open: value == null ? open : triggerValue === value,
48
+ modal: !!prop("modal"),
49
+ nested: !!layer?.nested,
50
+ hasNested: !!layer?.hasNested,
51
+ placement: currentPlacement,
52
+ side: currentPlacementSide,
53
+ activationDirection,
54
+ instant
55
+ };
56
+ }
57
+ function getViewportState() {
58
+ return {
59
+ open,
60
+ instant,
61
+ side: currentPlacementSide,
62
+ activationDirection,
63
+ transitioning
64
+ };
65
+ }
66
+ return {
67
+ open,
68
+ setOpen(nextOpen, reason = "script") {
69
+ send({
70
+ type: nextOpen ? "OPEN" : "CLOSE",
71
+ src: reason,
72
+ replaces: "open"
73
+ });
74
+ },
75
+ triggerValue,
76
+ activationDirection,
77
+ getContentValues() {
78
+ const previousValue = context.get("previousTriggerValue");
79
+ const values = [];
80
+ if (previousValue != null && previousValue !== triggerValue) values.push(previousValue);
81
+ if (triggerValue != null) values.push(triggerValue);
82
+ return values;
83
+ },
84
+ setTriggerValue(value) {
85
+ send({
86
+ type: "TRIGGER_VALUE.SET",
87
+ value
88
+ });
89
+ },
90
+ reposition(options = {}) {
91
+ send({
92
+ type: "POSITIONING.SET",
93
+ options
94
+ });
95
+ },
96
+ getArrowProps() {
97
+ return normalize.element({
98
+ ...parts.arrow.attrs(scope.id),
99
+ dir: prop("dir"),
100
+ hidden: viewportRendered ? !open : void 0,
101
+ "data-state": open ? "open" : "closed",
102
+ style: popperStyles.arrow
103
+ });
104
+ },
105
+ getArrowTipProps() {
106
+ return normalize.element({
107
+ ...parts.arrowTip.attrs(scope.id),
108
+ dir: prop("dir"),
109
+ style: popperStyles.arrowTip
110
+ });
111
+ },
112
+ getAnchorProps() {
113
+ return normalize.element({
114
+ ...parts.anchor.attrs(scope.id),
115
+ dir: prop("dir")
116
+ });
117
+ },
118
+ getTriggerState,
119
+ getTriggerProps(props = {}) {
120
+ const { value } = props;
121
+ const triggerState = getTriggerState(props);
122
+ const { current } = triggerState;
123
+ return normalize.button({
124
+ ...parts.trigger.attrs(scope.id),
125
+ dir: prop("dir"),
126
+ type: "button",
127
+ "data-placement": currentPlacement,
128
+ "data-side": currentPlacementSide,
129
+ "data-value": value,
130
+ "data-current": dataAttr(current),
131
+ "aria-haspopup": "dialog",
132
+ "aria-expanded": triggerState.open,
133
+ "data-state": open ? "open" : "closed",
134
+ "aria-controls": getContentId(scope, viewportRendered ? value : null),
135
+ onPointerDown(event) {
136
+ if (!isLeftClick(event)) return;
137
+ if (isSafari()) event.currentTarget.focus();
138
+ },
139
+ onClick(event) {
140
+ if (event.defaultPrevented) return;
141
+ const shouldSwitch = open && value != null && !current;
142
+ const instant = event.detail === 0 ? "click" : null;
143
+ send({
144
+ type: shouldSwitch ? "TRIGGER_VALUE.SET" : "TOGGLE",
145
+ value,
146
+ instant,
147
+ src: "trigger-click"
148
+ });
149
+ },
150
+ onBlur(event) {
151
+ send({
152
+ type: "TRIGGER_BLUR",
153
+ target: event.relatedTarget
154
+ });
155
+ }
156
+ });
157
+ },
158
+ getIndicatorProps() {
159
+ return normalize.element({
160
+ ...parts.indicator.attrs(scope.id),
161
+ dir: prop("dir"),
162
+ "data-state": open ? "open" : "closed"
163
+ });
164
+ },
165
+ getPositionerState,
166
+ getPositionerProps() {
167
+ const positionerState = getPositionerState();
168
+ return normalize.element({
169
+ ...parts.positioner.attrs(scope.id),
170
+ dir: prop("dir"),
171
+ "data-transitioning": dataAttr(positionerState.transitioning),
172
+ "data-instant": positionerState.instant ?? void 0,
173
+ ...getDismissableLayerAttrs(layer),
174
+ style: {
175
+ ...popperStyles.floating,
176
+ ...getDismissableLayerStyle(layer),
177
+ ...getViewportSizeStyle(viewportRendered ? context.get("viewportSize") : null)
178
+ }
179
+ });
180
+ },
181
+ getContentState,
182
+ getContentProps(props = {}) {
183
+ const { value } = props;
184
+ const contentState = getContentState(props);
185
+ const contentLayer = viewportRendered ? null : layer;
186
+ return normalize.element({
187
+ ...parts.content.attrs(scope.id),
188
+ dir: prop("dir"),
189
+ id: getContentId(scope, value),
190
+ tabIndex: -1,
191
+ role: "dialog",
192
+ "aria-modal": ariaAttr(contentState.modal),
193
+ hidden: !contentState.open,
194
+ "data-value": value,
195
+ "data-state": contentState.open ? "open" : "closed",
196
+ "data-expanded": dataAttr(contentState.open),
197
+ "aria-labelledby": rendered.title ? getTitleId(scope, value) : void 0,
198
+ "aria-describedby": rendered.description ? getDescriptionId(scope, value) : void 0,
199
+ "data-placement": contentState.placement,
200
+ "data-side": contentState.side,
201
+ "data-activation-direction": contentState.activationDirection ?? void 0,
202
+ "data-instant": contentState.instant ?? void 0,
203
+ ...getDismissableLayerAttrs(contentLayer),
204
+ style: getDismissableLayerStyle(contentLayer, { pointerEvents: true })
205
+ });
206
+ },
207
+ getViewportState,
208
+ getViewportProps() {
209
+ const viewportState = getViewportState();
210
+ return normalize.element({
211
+ ...parts.viewport.attrs(scope.id),
212
+ dir: prop("dir"),
213
+ hidden: !viewportState.open,
214
+ "data-state": viewportState.open ? "open" : "closed",
215
+ "data-side": viewportState.side,
216
+ "data-instant": viewportState.instant ?? void 0,
217
+ "data-activation-direction": viewportState.activationDirection ?? void 0,
218
+ "data-transitioning": dataAttr(viewportState.transitioning),
219
+ ...getDismissableLayerAttrs(layer),
220
+ style: {
221
+ ...getDismissableLayerStyle(layer, { pointerEvents: true }),
222
+ ...getViewportStyle(viewportState.side)
223
+ }
224
+ });
225
+ },
226
+ getTitleProps(props = {}) {
227
+ return normalize.element({
228
+ ...parts.title.attrs(scope.id),
229
+ id: getTitleId(scope, props.value),
230
+ dir: prop("dir")
231
+ });
232
+ },
233
+ getDescriptionProps(props = {}) {
234
+ return normalize.element({
235
+ ...parts.description.attrs(scope.id),
236
+ id: getDescriptionId(scope, props.value),
237
+ dir: prop("dir")
238
+ });
239
+ },
240
+ getCloseTriggerProps() {
241
+ return normalize.button({
242
+ ...parts.closeTrigger.attrs(scope.id),
243
+ dir: prop("dir"),
244
+ type: "button",
245
+ "aria-label": translations.closeTriggerLabel,
246
+ onClick(event) {
247
+ if (event.defaultPrevented) return;
248
+ event.stopPropagation();
249
+ send({
250
+ type: "CLOSE",
251
+ src: "close-trigger"
252
+ });
253
+ }
254
+ });
255
+ }
256
+ };
257
+ }
258
+ function getViewportStyle(side) {
259
+ return {
260
+ position: "absolute",
261
+ width: "var(--viewport-width)",
262
+ height: "var(--viewport-height)",
263
+ top: side === "top" ? void 0 : "0px",
264
+ bottom: side === "top" ? "0px" : void 0,
265
+ left: side === "left" ? void 0 : "0px",
266
+ right: side === "left" ? "0px" : void 0
267
+ };
177
268
  }
178
- export {
179
- connect
180
- };
269
+ export { connect };
@@ -1,21 +1,19 @@
1
- import { Scope } from '@zag-js/core';
2
-
3
- declare const getAnchorId: (scope: Scope) => any;
4
- declare const getTriggerId: (scope: Scope, value?: string) => any;
5
- declare const getContentId: (scope: Scope) => any;
6
- declare const getPositionerId: (scope: Scope) => any;
7
- declare const getArrowId: (scope: Scope) => any;
8
- declare const getTitleId: (scope: Scope) => any;
9
- declare const getDescriptionId: (scope: Scope) => any;
10
- declare const getCloseTriggerId: (scope: Scope) => any;
11
- declare const getAnchorEl: (scope: Scope) => HTMLElement | null;
12
- declare const getTriggerEls: (scope: Scope) => HTMLElement[];
13
- declare const getActiveTriggerEl: (scope: Scope, value: string | null) => HTMLElement | null;
14
- declare const getContentEl: (scope: Scope) => HTMLElement | null;
15
- declare const getPositionerEl: (scope: Scope) => HTMLElement | null;
16
- declare const getTitleEl: (scope: Scope) => HTMLElement | null;
17
- declare const getDescriptionEl: (scope: Scope) => HTMLElement | null;
18
- declare const getFocusableEls: (scope: Scope) => HTMLElement[];
19
- declare const getFirstFocusableEl: (scope: Scope) => HTMLElement;
20
-
21
- export { getActiveTriggerEl, getAnchorEl, getAnchorId, getArrowId, getCloseTriggerId, getContentEl, getContentId, getDescriptionEl, getDescriptionId, getFirstFocusableEl, getFocusableEls, getPositionerEl, getPositionerId, getTitleEl, getTitleId, getTriggerEls, getTriggerId };
1
+ import { Scope } from "@zag-js/core";
2
+ export declare const getAnchorId: (scope: Scope) => any;
3
+ export declare const getTriggerId: (scope: Scope, value?: string) => any;
4
+ export declare const getContentId: (scope: Scope, value?: string | null) => any;
5
+ export declare const getPositionerId: (scope: Scope) => any;
6
+ export declare const getArrowId: (scope: Scope) => any;
7
+ export declare const getTitleId: (scope: Scope, value?: string | null) => any;
8
+ export declare const getDescriptionId: (scope: Scope, value?: string | null) => any;
9
+ export declare const getCloseTriggerId: (scope: Scope) => any;
10
+ export declare const getAnchorEl: (scope: Scope) => HTMLElement | null;
11
+ export declare const getTriggerEls: (scope: Scope) => HTMLElement[];
12
+ export declare const getTriggerEl: (scope: Scope, value?: string | null) => HTMLElement | null;
13
+ export declare const getContentEl: (scope: Scope, value?: string | null) => HTMLElement | null;
14
+ export declare const getViewportEl: (scope: Scope) => HTMLElement | null;
15
+ export declare const getLayerEl: (scope: Scope, value?: string | null) => HTMLElement | null;
16
+ export declare const getPositionerEl: (scope: Scope) => HTMLElement | null;
17
+ export declare const getArrowEl: (scope: Scope) => HTMLElement | null;
18
+ export declare const getTitleEl: (scope: Scope) => HTMLElement | null;
19
+ export declare const getDescriptionEl: (scope: Scope) => HTMLElement | null;
@@ -1,21 +1,19 @@
1
- import { Scope } from '@zag-js/core';
2
-
3
- declare const getAnchorId: (scope: Scope) => any;
4
- declare const getTriggerId: (scope: Scope, value?: string) => any;
5
- declare const getContentId: (scope: Scope) => any;
6
- declare const getPositionerId: (scope: Scope) => any;
7
- declare const getArrowId: (scope: Scope) => any;
8
- declare const getTitleId: (scope: Scope) => any;
9
- declare const getDescriptionId: (scope: Scope) => any;
10
- declare const getCloseTriggerId: (scope: Scope) => any;
11
- declare const getAnchorEl: (scope: Scope) => HTMLElement | null;
12
- declare const getTriggerEls: (scope: Scope) => HTMLElement[];
13
- declare const getActiveTriggerEl: (scope: Scope, value: string | null) => HTMLElement | null;
14
- declare const getContentEl: (scope: Scope) => HTMLElement | null;
15
- declare const getPositionerEl: (scope: Scope) => HTMLElement | null;
16
- declare const getTitleEl: (scope: Scope) => HTMLElement | null;
17
- declare const getDescriptionEl: (scope: Scope) => HTMLElement | null;
18
- declare const getFocusableEls: (scope: Scope) => HTMLElement[];
19
- declare const getFirstFocusableEl: (scope: Scope) => HTMLElement;
20
-
21
- export { getActiveTriggerEl, getAnchorEl, getAnchorId, getArrowId, getCloseTriggerId, getContentEl, getContentId, getDescriptionEl, getDescriptionId, getFirstFocusableEl, getFocusableEls, getPositionerEl, getPositionerId, getTitleEl, getTitleId, getTriggerEls, getTriggerId };
1
+ import { Scope } from "@zag-js/core";
2
+ export declare const getAnchorId: (scope: Scope) => any;
3
+ export declare const getTriggerId: (scope: Scope, value?: string) => any;
4
+ export declare const getContentId: (scope: Scope, value?: string | null) => any;
5
+ export declare const getPositionerId: (scope: Scope) => any;
6
+ export declare const getArrowId: (scope: Scope) => any;
7
+ export declare const getTitleId: (scope: Scope, value?: string | null) => any;
8
+ export declare const getDescriptionId: (scope: Scope, value?: string | null) => any;
9
+ export declare const getCloseTriggerId: (scope: Scope) => any;
10
+ export declare const getAnchorEl: (scope: Scope) => HTMLElement | null;
11
+ export declare const getTriggerEls: (scope: Scope) => HTMLElement[];
12
+ export declare const getTriggerEl: (scope: Scope, value?: string | null) => HTMLElement | null;
13
+ export declare const getContentEl: (scope: Scope, value?: string | null) => HTMLElement | null;
14
+ export declare const getViewportEl: (scope: Scope) => HTMLElement | null;
15
+ export declare const getLayerEl: (scope: Scope, value?: string | null) => HTMLElement | null;
16
+ export declare const getPositionerEl: (scope: Scope) => HTMLElement | null;
17
+ export declare const getArrowEl: (scope: Scope) => HTMLElement | null;
18
+ export declare const getTitleEl: (scope: Scope) => HTMLElement | null;
19
+ export declare const getDescriptionEl: (scope: Scope) => HTMLElement | null;