@zag-js/tabs 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,234 +1,256 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/tabs.connect.ts
31
- var tabs_connect_exports = {};
32
- __export(tabs_connect_exports, {
33
- connect: () => connect
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
34
4
  });
35
- module.exports = __toCommonJS(tabs_connect_exports);
36
- var import_dom_query = require("@zag-js/dom-query");
37
- var import_utils = require("@zag-js/utils");
38
- var import_tabs = require("./tabs.anatomy.js");
39
- var dom = __toESM(require("./tabs.dom.js"));
5
+ const require_tabs_anatomy = require("./tabs.anatomy.js");
6
+ const require_tabs_dom = require("./tabs.dom.js");
7
+ let _zag_js_dom_query = require("@zag-js/dom-query");
8
+ let _zag_js_utils = require("@zag-js/utils");
9
+ // src/tabs.connect.ts
40
10
  function connect(service, normalize) {
41
- const { state, send, context, prop, scope } = service;
42
- const translations = prop("translations");
43
- const focused = state.matches("focused");
44
- const isVertical = prop("orientation") === "vertical";
45
- const isHorizontal = prop("orientation") === "horizontal";
46
- const virtualFocus = prop("virtualFocus");
47
- function getTriggerState(props) {
48
- return {
49
- selected: context.get("value") === props.value,
50
- focused: context.get("focusedValue") === props.value,
51
- disabled: !!props.disabled
52
- };
53
- }
54
- function getContentState(props) {
55
- return { selected: context.get("value") === props.value };
56
- }
57
- return {
58
- value: context.get("value"),
59
- focusedValue: context.get("focusedValue"),
60
- setValue(value) {
61
- send({ type: "SET_VALUE", value });
62
- },
63
- clearValue() {
64
- send({ type: "CLEAR_VALUE" });
65
- },
66
- setIndicatorRect(value) {
67
- const id = dom.getTriggerId(scope, value);
68
- send({ type: "SET_INDICATOR_RECT", id });
69
- },
70
- syncTabIndex() {
71
- send({ type: "SYNC_TAB_INDEX" });
72
- },
73
- selectNext(fromValue) {
74
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectNext" });
75
- send({ type: "ARROW_NEXT", src: "selectNext" });
76
- },
77
- selectPrev(fromValue) {
78
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectPrev" });
79
- send({ type: "ARROW_PREV", src: "selectPrev" });
80
- },
81
- focus() {
82
- const value = context.get("value");
83
- if (!value) return;
84
- dom.getTriggerEl(scope, value)?.focus();
85
- },
86
- getRootProps() {
87
- return normalize.element({
88
- ...import_tabs.parts.root.attrs(scope.id),
89
- "data-orientation": prop("orientation"),
90
- "data-focus": (0, import_dom_query.dataAttr)(focused),
91
- dir: prop("dir")
92
- });
93
- },
94
- getListProps() {
95
- return normalize.element({
96
- ...import_tabs.parts.list.attrs(scope.id),
97
- role: "tablist",
98
- dir: prop("dir"),
99
- "data-focus": (0, import_dom_query.dataAttr)(focused),
100
- "aria-orientation": prop("orientation"),
101
- "data-orientation": prop("orientation"),
102
- "aria-label": translations?.listLabel,
103
- onKeyDown(event) {
104
- if (event.defaultPrevented) return;
105
- if ((0, import_dom_query.isComposingEvent)(event)) return;
106
- if (!(0, import_dom_query.contains)(event.currentTarget, (0, import_dom_query.getEventTarget)(event))) return;
107
- const keyMap = {
108
- ArrowDown() {
109
- if (isHorizontal) return;
110
- send({ type: "ARROW_NEXT", key: "ArrowDown" });
111
- },
112
- ArrowUp() {
113
- if (isHorizontal) return;
114
- send({ type: "ARROW_PREV", key: "ArrowUp" });
115
- },
116
- ArrowLeft() {
117
- if (isVertical) return;
118
- send({ type: "ARROW_PREV", key: "ArrowLeft" });
119
- },
120
- ArrowRight() {
121
- if (isVertical) return;
122
- send({ type: "ARROW_NEXT", key: "ArrowRight" });
123
- },
124
- Home() {
125
- send({ type: "HOME" });
126
- },
127
- End() {
128
- send({ type: "END" });
129
- }
130
- };
131
- let key = (0, import_dom_query.getEventKey)(event, {
132
- dir: prop("dir"),
133
- orientation: prop("orientation")
134
- });
135
- const exec = keyMap[key];
136
- if (exec) {
137
- event.preventDefault();
138
- exec(event);
139
- return;
140
- }
141
- }
142
- });
143
- },
144
- getTriggerState,
145
- getTriggerProps(props) {
146
- const { value, disabled } = props;
147
- const triggerState = getTriggerState(props);
148
- return normalize.button({
149
- ...import_tabs.parts.trigger.attrs(scope.id),
150
- role: "tab",
151
- type: "button",
152
- disabled,
153
- dir: prop("dir"),
154
- "data-orientation": prop("orientation"),
155
- "data-disabled": (0, import_dom_query.dataAttr)(disabled),
156
- "aria-disabled": disabled,
157
- "data-value": value,
158
- "aria-selected": triggerState.selected,
159
- "data-selected": (0, import_dom_query.dataAttr)(triggerState.selected),
160
- "data-focus": (0, import_dom_query.dataAttr)(triggerState.focused),
161
- "aria-controls": triggerState.selected ? dom.getContentId(scope, value) : void 0,
162
- "data-ssr": (0, import_dom_query.dataAttr)(context.get("ssr")),
163
- id: dom.getTriggerId(scope, value),
164
- tabIndex: triggerState.selected && !virtualFocus ? 0 : -1,
165
- onFocus() {
166
- send({ type: "TAB_FOCUS", value });
167
- },
168
- onBlur(event) {
169
- const target = event.relatedTarget;
170
- if (target?.getAttribute("role") !== "tab") {
171
- send({ type: "TAB_BLUR" });
172
- }
173
- },
174
- onClick(event) {
175
- if (event.defaultPrevented) return;
176
- if ((0, import_dom_query.isOpeningInNewTab)(event)) return;
177
- if (disabled) return;
178
- if ((0, import_dom_query.isSafari)()) {
179
- event.currentTarget.focus();
180
- }
181
- send({ type: "TAB_CLICK", value });
182
- }
183
- });
184
- },
185
- getContentState,
186
- getContentProps(props) {
187
- const { value } = props;
188
- const contentState = getContentState(props);
189
- return normalize.element({
190
- ...import_tabs.parts.content.attrs(scope.id),
191
- dir: prop("dir"),
192
- id: dom.getContentId(scope, value),
193
- tabIndex: !virtualFocus ? 0 : -1,
194
- "aria-labelledby": dom.getTriggerId(scope, value),
195
- role: "tabpanel",
196
- "data-selected": (0, import_dom_query.dataAttr)(contentState.selected),
197
- "data-orientation": prop("orientation"),
198
- hidden: !contentState.selected
199
- });
200
- },
201
- getIndicatorProps() {
202
- const rect = context.get("indicatorRect");
203
- const animateIndicator = context.get("animateIndicator");
204
- return normalize.element({
205
- ...import_tabs.parts.indicator.attrs(scope.id),
206
- dir: prop("dir"),
207
- "data-orientation": prop("orientation"),
208
- hidden: isRectEmpty(rect),
209
- onTransitionEnd(event) {
210
- if ((0, import_dom_query.getEventTarget)(event) !== event.currentTarget) return;
211
- send({ type: "INDICATOR_TRANSITION_END" });
212
- },
213
- style: {
214
- "--transition-property": "left, right, top, bottom, width, height",
215
- "--left": (0, import_utils.toPx)(rect?.x),
216
- "--top": (0, import_utils.toPx)(rect?.y),
217
- "--width": (0, import_utils.toPx)(rect?.width),
218
- "--height": (0, import_utils.toPx)(rect?.height),
219
- position: "absolute",
220
- willChange: animateIndicator ? "var(--transition-property)" : "auto",
221
- transitionProperty: animateIndicator ? "var(--transition-property)" : "none",
222
- transitionDuration: animateIndicator ? "var(--transition-duration, 150ms)" : "0ms",
223
- transitionTimingFunction: "var(--transition-timing-function)",
224
- [isHorizontal ? "left" : "top"]: isHorizontal ? "var(--left)" : "var(--top)"
225
- }
226
- });
227
- }
228
- };
11
+ const { state, send, context, prop, scope, computed } = service;
12
+ const translations = prop("translations");
13
+ const focused = state.matches("focused");
14
+ const isVertical = prop("orientation") === "vertical";
15
+ const isHorizontal = prop("orientation") === "horizontal";
16
+ const virtualFocus = prop("virtualFocus");
17
+ const tabStopValue = context.get("tabStopValue") ?? context.get("value");
18
+ const activationDirection = computed("activationDirection");
19
+ const indicatorRect = context.get("indicatorRect");
20
+ const indicatorPending = context.get("ssr") || indicatorRect != null && isRectEmpty(indicatorRect);
21
+ function moveFocus(to) {
22
+ send({
23
+ type: "FOCUS.MOVE",
24
+ to,
25
+ src: "keyboard"
26
+ });
27
+ }
28
+ function getRootState() {
29
+ return {
30
+ focused,
31
+ orientation: prop("orientation"),
32
+ activationDirection
33
+ };
34
+ }
35
+ function getTriggerState(props) {
36
+ return {
37
+ selected: context.get("value") === props.value,
38
+ focused: context.get("focusedValue") === props.value,
39
+ disabled: !!props.disabled
40
+ };
41
+ }
42
+ function getContentState(props) {
43
+ return {
44
+ ...getRootState(),
45
+ selected: context.get("value") === props.value
46
+ };
47
+ }
48
+ function getIndicatorState() {
49
+ const rect = context.get("indicatorRect");
50
+ return {
51
+ ...getRootState(),
52
+ visible: !isRectEmpty(rect),
53
+ instant: context.get("indicatorInstant")
54
+ };
55
+ }
56
+ return {
57
+ value: context.get("value"),
58
+ focusedValue: context.get("focusedValue"),
59
+ activationDirection,
60
+ setValue(value, reason = "script") {
61
+ send({
62
+ type: "VALUE.SET",
63
+ value,
64
+ src: reason
65
+ });
66
+ },
67
+ clearValue() {
68
+ send({
69
+ type: "VALUE.SET",
70
+ value: null,
71
+ src: "script"
72
+ });
73
+ },
74
+ setIndicatorRect(value) {
75
+ send({
76
+ type: "INDICATOR.SET",
77
+ value
78
+ });
79
+ },
80
+ syncTabIndex() {
81
+ send({ type: "CONTENT.SYNC" });
82
+ },
83
+ selectNext(fromValue) {
84
+ send({
85
+ type: "VALUE.STEP",
86
+ step: 1,
87
+ from: fromValue,
88
+ src: "script"
89
+ });
90
+ },
91
+ selectPrevious(fromValue) {
92
+ send({
93
+ type: "VALUE.STEP",
94
+ step: -1,
95
+ from: fromValue,
96
+ src: "script"
97
+ });
98
+ },
99
+ focus() {
100
+ const value = context.get("value");
101
+ if (!value) return;
102
+ require_tabs_dom.getTriggerEl(scope, value)?.focus();
103
+ },
104
+ getRootState,
105
+ getRootProps() {
106
+ const rootState = getRootState();
107
+ return normalize.element({
108
+ ...require_tabs_anatomy.parts.root.attrs(scope.id),
109
+ "data-orientation": rootState.orientation,
110
+ "data-focus": (0, _zag_js_dom_query.dataAttr)(rootState.focused),
111
+ "data-activation-direction": rootState.activationDirection,
112
+ dir: prop("dir")
113
+ });
114
+ },
115
+ getListProps() {
116
+ const rootState = getRootState();
117
+ return normalize.element({
118
+ ...require_tabs_anatomy.parts.list.attrs(scope.id),
119
+ role: "tablist",
120
+ dir: prop("dir"),
121
+ "data-focus": (0, _zag_js_dom_query.dataAttr)(rootState.focused),
122
+ "aria-orientation": rootState.orientation,
123
+ "data-orientation": rootState.orientation,
124
+ "data-activation-direction": rootState.activationDirection,
125
+ "aria-label": translations?.listLabel,
126
+ onKeyDown(event) {
127
+ if (event.defaultPrevented) return;
128
+ if ((0, _zag_js_dom_query.isComposingEvent)(event)) return;
129
+ if ((0, _zag_js_dom_query.isModifierKey)(event) || event.shiftKey) return;
130
+ if (!(0, _zag_js_dom_query.contains)(event.currentTarget, (0, _zag_js_dom_query.getEventTarget)(event))) return;
131
+ const exec = {
132
+ ArrowDown() {
133
+ if (isHorizontal) return;
134
+ moveFocus("next");
135
+ },
136
+ ArrowUp() {
137
+ if (isHorizontal) return;
138
+ moveFocus("prev");
139
+ },
140
+ ArrowLeft() {
141
+ if (isVertical) return;
142
+ moveFocus("prev");
143
+ },
144
+ ArrowRight() {
145
+ if (isVertical) return;
146
+ moveFocus("next");
147
+ },
148
+ Home() {
149
+ moveFocus("first");
150
+ },
151
+ End() {
152
+ moveFocus("last");
153
+ }
154
+ }[(0, _zag_js_dom_query.getEventKey)(event, {
155
+ dir: prop("dir"),
156
+ orientation: prop("orientation")
157
+ })];
158
+ if (exec) {
159
+ event.preventDefault();
160
+ exec(event);
161
+ return;
162
+ }
163
+ }
164
+ });
165
+ },
166
+ getTriggerState,
167
+ getTriggerProps(props) {
168
+ const { value, disabled } = props;
169
+ const triggerState = getTriggerState(props);
170
+ return normalize.button({
171
+ ...require_tabs_anatomy.parts.trigger.attrs(scope.id),
172
+ role: "tab",
173
+ type: "button",
174
+ disabled,
175
+ dir: prop("dir"),
176
+ "data-orientation": prop("orientation"),
177
+ "data-disabled": (0, _zag_js_dom_query.dataAttr)(disabled),
178
+ "aria-disabled": disabled,
179
+ "data-value": value,
180
+ "aria-selected": triggerState.selected,
181
+ "data-selected": (0, _zag_js_dom_query.dataAttr)(triggerState.selected),
182
+ "data-focus": (0, _zag_js_dom_query.dataAttr)(triggerState.focused),
183
+ "aria-controls": triggerState.selected ? require_tabs_dom.getContentId(scope, value) : void 0,
184
+ "data-indicator-pending": (0, _zag_js_dom_query.dataAttr)(indicatorPending),
185
+ "data-activation-direction": activationDirection,
186
+ id: require_tabs_dom.getTriggerId(scope, value),
187
+ tabIndex: !virtualFocus && tabStopValue === value ? 0 : -1,
188
+ onFocus() {
189
+ send({
190
+ type: "TRIGGER.FOCUS",
191
+ value
192
+ });
193
+ },
194
+ onBlur(event) {
195
+ if ((0, _zag_js_dom_query.contains)(require_tabs_dom.getListEl(scope), event.relatedTarget)) return;
196
+ send({ type: "TRIGGER.BLUR" });
197
+ },
198
+ onClick(event) {
199
+ if (event.defaultPrevented) return;
200
+ if ((0, _zag_js_dom_query.isDownloadingEvent)(event)) return;
201
+ if ((0, _zag_js_dom_query.isOpeningInNewTab)(event)) return;
202
+ if (event.shiftKey && (0, _zag_js_dom_query.isAnchorElement)(event.currentTarget)) return;
203
+ if (disabled) return;
204
+ if ((0, _zag_js_dom_query.isSafari)()) event.currentTarget.focus();
205
+ const node = event.currentTarget;
206
+ if (prop("navigate") != null && (0, _zag_js_dom_query.isAnchorElement)(node)) event.preventDefault();
207
+ send({
208
+ type: "TRIGGER.CLICK",
209
+ value,
210
+ src: "trigger-click"
211
+ });
212
+ }
213
+ });
214
+ },
215
+ getContentState,
216
+ getContentProps(props) {
217
+ const { value } = props;
218
+ const contentState = getContentState(props);
219
+ return normalize.element({
220
+ ...require_tabs_anatomy.parts.content.attrs(scope.id),
221
+ dir: prop("dir"),
222
+ id: require_tabs_dom.getContentId(scope, value),
223
+ tabIndex: virtualFocus ? -1 : contentState.selected && context.get("hasFocusableContent") ? void 0 : 0,
224
+ "aria-labelledby": require_tabs_dom.getTriggerId(scope, value),
225
+ role: "tabpanel",
226
+ "data-selected": (0, _zag_js_dom_query.dataAttr)(contentState.selected),
227
+ "data-orientation": contentState.orientation,
228
+ "data-activation-direction": contentState.activationDirection,
229
+ hidden: !contentState.selected
230
+ });
231
+ },
232
+ getIndicatorState,
233
+ getIndicatorProps() {
234
+ const indicatorState = getIndicatorState();
235
+ const rect = context.get("indicatorRect");
236
+ return normalize.element({
237
+ ...require_tabs_anatomy.parts.indicator.attrs(scope.id),
238
+ dir: prop("dir"),
239
+ "data-orientation": indicatorState.orientation,
240
+ "data-instant": (0, _zag_js_dom_query.dataAttr)(indicatorState.instant),
241
+ "data-activation-direction": indicatorState.activationDirection,
242
+ hidden: !indicatorState.visible,
243
+ style: {
244
+ "--left": (0, _zag_js_utils.toPx)(rect?.x),
245
+ "--top": (0, _zag_js_utils.toPx)(rect?.y),
246
+ "--width": (0, _zag_js_utils.toPx)(rect?.width),
247
+ "--height": (0, _zag_js_utils.toPx)(rect?.height),
248
+ position: "absolute",
249
+ [isHorizontal ? "left" : "top"]: isHorizontal ? "var(--left)" : "var(--top)"
250
+ }
251
+ });
252
+ }
253
+ };
229
254
  }
230
- var isRectEmpty = (rect) => rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
231
- // Annotate the CommonJS export names for ESM import in node:
232
- 0 && (module.exports = {
233
- connect
234
- });
255
+ const isRectEmpty = (rect) => rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
256
+ exports.connect = connect;