@zag-js/tabs 1.46.0 → 1.48.0

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,215 +1,228 @@
1
- // src/tabs.connect.ts
2
- import {
3
- contains,
4
- dataAttr,
5
- getEventKey,
6
- getEventTarget,
7
- isAnchorElement,
8
- isComposingEvent,
9
- isOpeningInNewTab,
10
- isSafari
11
- } from "@zag-js/dom-query";
12
- import { toPx } from "@zag-js/utils";
13
1
  import { parts } from "./tabs.anatomy.mjs";
14
- import * as dom from "./tabs.dom.mjs";
2
+ import { getContentId, getIndicatorId, getListId, getRootId, getTriggerEl, getTriggerId } from "./tabs.dom.mjs";
3
+ import { contains, dataAttr, getEventKey, getEventTarget, isAnchorElement, isComposingEvent, isDownloadingEvent, isOpeningInNewTab, isSafari } from "@zag-js/dom-query";
4
+ import { toPx } from "@zag-js/utils";
5
+ // src/tabs.connect.ts
15
6
  function connect(service, normalize) {
16
- const { state, send, context, prop, scope } = service;
17
- const translations = prop("translations");
18
- const focused = state.matches("focused");
19
- const isVertical = prop("orientation") === "vertical";
20
- const isHorizontal = prop("orientation") === "horizontal";
21
- const composite = prop("composite");
22
- function sendKeyboardEvent(type, key) {
23
- send({ type, key, src: "keyboard" });
24
- }
25
- function getTriggerState(props) {
26
- return {
27
- selected: context.get("value") === props.value,
28
- focused: context.get("focusedValue") === props.value,
29
- disabled: !!props.disabled
30
- };
31
- }
32
- return {
33
- value: context.get("value"),
34
- focusedValue: context.get("focusedValue"),
35
- setValue(value) {
36
- send({ type: "SET_VALUE", value });
37
- },
38
- clearValue() {
39
- send({ type: "CLEAR_VALUE" });
40
- },
41
- setIndicatorRect(value) {
42
- const id = dom.getTriggerId(scope, value);
43
- send({ type: "SET_INDICATOR_RECT", id });
44
- },
45
- syncTabIndex() {
46
- send({ type: "SYNC_TAB_INDEX" });
47
- },
48
- selectNext(fromValue) {
49
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectNext" });
50
- send({ type: "ARROW_NEXT", src: "selectNext" });
51
- },
52
- selectPrev(fromValue) {
53
- send({ type: "TAB_FOCUS", value: fromValue, src: "selectPrev" });
54
- send({ type: "ARROW_PREV", src: "selectPrev" });
55
- },
56
- focus() {
57
- const value = context.get("value");
58
- if (!value) return;
59
- dom.getTriggerEl(scope, value)?.focus();
60
- },
61
- getRootProps() {
62
- return normalize.element({
63
- ...parts.root.attrs,
64
- id: dom.getRootId(scope),
65
- "data-orientation": prop("orientation"),
66
- "data-focus": dataAttr(focused),
67
- dir: prop("dir")
68
- });
69
- },
70
- getListProps() {
71
- return normalize.element({
72
- ...parts.list.attrs,
73
- id: dom.getListId(scope),
74
- role: "tablist",
75
- dir: prop("dir"),
76
- "data-focus": dataAttr(focused),
77
- "aria-orientation": prop("orientation"),
78
- "data-orientation": prop("orientation"),
79
- "aria-label": translations?.listLabel,
80
- onKeyDown(event) {
81
- if (event.defaultPrevented) return;
82
- if (isComposingEvent(event)) return;
83
- if (!contains(event.currentTarget, getEventTarget(event))) return;
84
- const keyMap = {
85
- ArrowDown() {
86
- if (isHorizontal) return;
87
- sendKeyboardEvent("ARROW_NEXT", "ArrowDown");
88
- },
89
- ArrowUp() {
90
- if (isHorizontal) return;
91
- sendKeyboardEvent("ARROW_PREV", "ArrowUp");
92
- },
93
- ArrowLeft() {
94
- if (isVertical) return;
95
- sendKeyboardEvent("ARROW_PREV", "ArrowLeft");
96
- },
97
- ArrowRight() {
98
- if (isVertical) return;
99
- sendKeyboardEvent("ARROW_NEXT", "ArrowRight");
100
- },
101
- Home() {
102
- sendKeyboardEvent("HOME");
103
- },
104
- End() {
105
- sendKeyboardEvent("END");
106
- }
107
- };
108
- let key = getEventKey(event, {
109
- dir: prop("dir"),
110
- orientation: prop("orientation")
111
- });
112
- const exec = keyMap[key];
113
- if (exec) {
114
- event.preventDefault();
115
- exec(event);
116
- return;
117
- }
118
- }
119
- });
120
- },
121
- getTriggerState,
122
- getTriggerProps(props) {
123
- const { value, disabled } = props;
124
- const triggerState = getTriggerState(props);
125
- return normalize.button({
126
- ...parts.trigger.attrs,
127
- role: "tab",
128
- type: "button",
129
- disabled,
130
- dir: prop("dir"),
131
- "data-orientation": prop("orientation"),
132
- "data-disabled": dataAttr(disabled),
133
- "aria-disabled": disabled,
134
- "data-value": value,
135
- "aria-selected": triggerState.selected,
136
- "data-selected": dataAttr(triggerState.selected),
137
- "data-focus": dataAttr(triggerState.focused),
138
- "aria-controls": triggerState.selected ? dom.getContentId(scope, value) : void 0,
139
- "data-ownedby": dom.getListId(scope),
140
- "data-ssr": dataAttr(context.get("ssr")),
141
- id: dom.getTriggerId(scope, value),
142
- tabIndex: triggerState.selected && composite ? 0 : -1,
143
- onFocus() {
144
- send({ type: "TAB_FOCUS", value });
145
- },
146
- onBlur(event) {
147
- const target = event.relatedTarget;
148
- if (target?.getAttribute("role") !== "tab") {
149
- send({ type: "TAB_BLUR" });
150
- }
151
- },
152
- onClick(event) {
153
- if (event.defaultPrevented) return;
154
- if (isOpeningInNewTab(event)) return;
155
- if (disabled) return;
156
- if (isSafari()) {
157
- event.currentTarget.focus();
158
- }
159
- const node = event.currentTarget;
160
- const shouldNavigate = prop("navigate") != null && isAnchorElement(node);
161
- if (shouldNavigate) event.preventDefault();
162
- send({ type: "TAB_CLICK", value });
163
- }
164
- });
165
- },
166
- getContentProps(props) {
167
- const { value } = props;
168
- const selected = context.get("value") === value;
169
- return normalize.element({
170
- ...parts.content.attrs,
171
- dir: prop("dir"),
172
- id: dom.getContentId(scope, value),
173
- tabIndex: composite ? 0 : -1,
174
- "aria-labelledby": dom.getTriggerId(scope, value),
175
- role: "tabpanel",
176
- "data-ownedby": dom.getListId(scope),
177
- "data-selected": dataAttr(selected),
178
- "data-orientation": prop("orientation"),
179
- hidden: !selected
180
- });
181
- },
182
- getIndicatorProps() {
183
- const rect = context.get("indicatorRect");
184
- const animateIndicator = context.get("animateIndicator");
185
- return normalize.element({
186
- id: dom.getIndicatorId(scope),
187
- ...parts.indicator.attrs,
188
- dir: prop("dir"),
189
- "data-orientation": prop("orientation"),
190
- hidden: isRectEmpty(rect),
191
- onTransitionEnd(event) {
192
- if (getEventTarget(event) !== event.currentTarget) return;
193
- send({ type: "INDICATOR_TRANSITION_END" });
194
- },
195
- style: {
196
- "--transition-property": "left, right, top, bottom, width, height",
197
- "--left": toPx(rect?.x),
198
- "--top": toPx(rect?.y),
199
- "--width": toPx(rect?.width),
200
- "--height": toPx(rect?.height),
201
- position: "absolute",
202
- willChange: animateIndicator ? "var(--transition-property)" : "auto",
203
- transitionProperty: animateIndicator ? "var(--transition-property)" : "none",
204
- transitionDuration: animateIndicator ? "var(--transition-duration, 150ms)" : "0ms",
205
- transitionTimingFunction: "var(--transition-timing-function)",
206
- [isHorizontal ? "left" : "top"]: isHorizontal ? "var(--left)" : "var(--top)"
207
- }
208
- });
209
- }
210
- };
7
+ const { state, send, context, prop, scope } = service;
8
+ const translations = prop("translations");
9
+ const focused = state.matches("focused");
10
+ const isVertical = prop("orientation") === "vertical";
11
+ const isHorizontal = prop("orientation") === "horizontal";
12
+ const composite = prop("composite");
13
+ function sendKeyboardEvent(type, key) {
14
+ send({
15
+ type,
16
+ key,
17
+ src: "keyboard"
18
+ });
19
+ }
20
+ function getTriggerState(props) {
21
+ return {
22
+ selected: context.get("value") === props.value,
23
+ focused: context.get("focusedValue") === props.value,
24
+ disabled: !!props.disabled
25
+ };
26
+ }
27
+ return {
28
+ value: context.get("value"),
29
+ focusedValue: context.get("focusedValue"),
30
+ setValue(value) {
31
+ send({
32
+ type: "SET_VALUE",
33
+ value
34
+ });
35
+ },
36
+ clearValue() {
37
+ send({ type: "CLEAR_VALUE" });
38
+ },
39
+ setIndicatorRect(value) {
40
+ const id = getTriggerId(scope, value);
41
+ send({
42
+ type: "SET_INDICATOR_RECT",
43
+ id
44
+ });
45
+ },
46
+ syncTabIndex() {
47
+ send({ type: "SYNC_TAB_INDEX" });
48
+ },
49
+ selectNext(fromValue) {
50
+ send({
51
+ type: "TAB_FOCUS",
52
+ value: fromValue,
53
+ src: "selectNext"
54
+ });
55
+ send({
56
+ type: "ARROW_NEXT",
57
+ src: "selectNext"
58
+ });
59
+ },
60
+ selectPrev(fromValue) {
61
+ send({
62
+ type: "TAB_FOCUS",
63
+ value: fromValue,
64
+ src: "selectPrev"
65
+ });
66
+ send({
67
+ type: "ARROW_PREV",
68
+ src: "selectPrev"
69
+ });
70
+ },
71
+ focus() {
72
+ const value = context.get("value");
73
+ if (!value) return;
74
+ getTriggerEl(scope, value)?.focus();
75
+ },
76
+ getRootProps() {
77
+ return normalize.element({
78
+ ...parts.root.attrs,
79
+ id: getRootId(scope),
80
+ "data-orientation": prop("orientation"),
81
+ "data-focus": dataAttr(focused),
82
+ dir: prop("dir")
83
+ });
84
+ },
85
+ getListProps() {
86
+ return normalize.element({
87
+ ...parts.list.attrs,
88
+ id: getListId(scope),
89
+ role: "tablist",
90
+ dir: prop("dir"),
91
+ "data-focus": dataAttr(focused),
92
+ "aria-orientation": prop("orientation"),
93
+ "data-orientation": prop("orientation"),
94
+ "aria-label": translations?.listLabel,
95
+ onKeyDown(event) {
96
+ if (event.defaultPrevented) return;
97
+ if (isComposingEvent(event)) return;
98
+ if (!contains(event.currentTarget, getEventTarget(event))) return;
99
+ const exec = {
100
+ ArrowDown() {
101
+ if (isHorizontal) return;
102
+ sendKeyboardEvent("ARROW_NEXT", "ArrowDown");
103
+ },
104
+ ArrowUp() {
105
+ if (isHorizontal) return;
106
+ sendKeyboardEvent("ARROW_PREV", "ArrowUp");
107
+ },
108
+ ArrowLeft() {
109
+ if (isVertical) return;
110
+ sendKeyboardEvent("ARROW_PREV", "ArrowLeft");
111
+ },
112
+ ArrowRight() {
113
+ if (isVertical) return;
114
+ sendKeyboardEvent("ARROW_NEXT", "ArrowRight");
115
+ },
116
+ Home() {
117
+ sendKeyboardEvent("HOME");
118
+ },
119
+ End() {
120
+ sendKeyboardEvent("END");
121
+ }
122
+ }[getEventKey(event, {
123
+ dir: prop("dir"),
124
+ orientation: prop("orientation")
125
+ })];
126
+ if (exec) {
127
+ event.preventDefault();
128
+ exec(event);
129
+ return;
130
+ }
131
+ }
132
+ });
133
+ },
134
+ getTriggerState,
135
+ getTriggerProps(props) {
136
+ const { value, disabled } = props;
137
+ const triggerState = getTriggerState(props);
138
+ return normalize.button({
139
+ ...parts.trigger.attrs,
140
+ role: "tab",
141
+ type: "button",
142
+ disabled,
143
+ dir: prop("dir"),
144
+ "data-orientation": prop("orientation"),
145
+ "data-disabled": dataAttr(disabled),
146
+ "aria-disabled": disabled,
147
+ "data-value": value,
148
+ "aria-selected": triggerState.selected,
149
+ "data-selected": dataAttr(triggerState.selected),
150
+ "data-focus": dataAttr(triggerState.focused),
151
+ "aria-controls": triggerState.selected ? getContentId(scope, value) : void 0,
152
+ "data-ownedby": getListId(scope),
153
+ "data-ssr": dataAttr(context.get("ssr")),
154
+ id: getTriggerId(scope, value),
155
+ tabIndex: triggerState.selected && composite ? 0 : -1,
156
+ onFocus() {
157
+ send({
158
+ type: "TAB_FOCUS",
159
+ value
160
+ });
161
+ },
162
+ onBlur(event) {
163
+ if (event.relatedTarget?.getAttribute("role") !== "tab") send({ type: "TAB_BLUR" });
164
+ },
165
+ onClick(event) {
166
+ if (event.defaultPrevented) return;
167
+ if (isDownloadingEvent(event)) return;
168
+ if (isOpeningInNewTab(event)) return;
169
+ if (event.shiftKey && isAnchorElement(event.currentTarget)) return;
170
+ if (disabled) return;
171
+ if (isSafari()) event.currentTarget.focus();
172
+ const node = event.currentTarget;
173
+ if (prop("navigate") != null && isAnchorElement(node)) event.preventDefault();
174
+ send({
175
+ type: "TAB_CLICK",
176
+ value
177
+ });
178
+ }
179
+ });
180
+ },
181
+ getContentProps(props) {
182
+ const { value } = props;
183
+ const selected = context.get("value") === value;
184
+ return normalize.element({
185
+ ...parts.content.attrs,
186
+ dir: prop("dir"),
187
+ id: getContentId(scope, value),
188
+ tabIndex: composite ? 0 : -1,
189
+ "aria-labelledby": getTriggerId(scope, value),
190
+ role: "tabpanel",
191
+ "data-ownedby": getListId(scope),
192
+ "data-selected": dataAttr(selected),
193
+ "data-orientation": prop("orientation"),
194
+ hidden: !selected
195
+ });
196
+ },
197
+ getIndicatorProps() {
198
+ const rect = context.get("indicatorRect");
199
+ const animateIndicator = context.get("animateIndicator");
200
+ return normalize.element({
201
+ id: getIndicatorId(scope),
202
+ ...parts.indicator.attrs,
203
+ dir: prop("dir"),
204
+ "data-orientation": prop("orientation"),
205
+ hidden: isRectEmpty(rect),
206
+ onTransitionEnd(event) {
207
+ if (getEventTarget(event) !== event.currentTarget) return;
208
+ send({ type: "INDICATOR_TRANSITION_END" });
209
+ },
210
+ style: {
211
+ "--transition-property": "left, right, top, bottom, width, height",
212
+ "--left": toPx(rect?.x),
213
+ "--top": toPx(rect?.y),
214
+ "--width": toPx(rect?.width),
215
+ "--height": toPx(rect?.height),
216
+ position: "absolute",
217
+ willChange: animateIndicator ? "var(--transition-property)" : "auto",
218
+ transitionProperty: animateIndicator ? "var(--transition-property)" : "none",
219
+ transitionDuration: animateIndicator ? "var(--transition-duration, 150ms)" : "0ms",
220
+ transitionTimingFunction: "var(--transition-timing-function)",
221
+ [isHorizontal ? "left" : "top"]: isHorizontal ? "var(--left)" : "var(--top)"
222
+ }
223
+ });
224
+ }
225
+ };
211
226
  }
212
- var isRectEmpty = (rect) => rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
213
- export {
214
- connect
215
- };
227
+ const isRectEmpty = (rect) => rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
228
+ export { connect };
@@ -1,36 +1,33 @@
1
- import { Scope } from '@zag-js/core';
2
-
3
- declare const getRootId: (ctx: Scope) => any;
4
- declare const getListId: (ctx: Scope) => any;
5
- declare const getContentId: (ctx: Scope, value: string) => any;
6
- declare const getTriggerId: (ctx: Scope, value: string) => any;
7
- declare const getIndicatorId: (ctx: Scope) => any;
8
- declare const getListEl: (ctx: Scope) => HTMLElement | null;
9
- declare const getContentEl: (ctx: Scope, value: string) => HTMLElement | null;
10
- declare const getTriggerEl: (ctx: Scope, value: string | null) => HTMLElement | null;
11
- declare const getIndicatorEl: (ctx: Scope) => HTMLElement | null;
12
- declare const getElements: (ctx: Scope) => HTMLElement[];
13
- declare const getFirstTriggerEl: (ctx: Scope) => HTMLElement | undefined;
14
- declare const getLastTriggerEl: (ctx: Scope) => HTMLElement | undefined;
15
- declare const getNextTriggerEl: (ctx: Scope, opts: {
16
- value: string;
17
- loopFocus?: boolean | undefined;
1
+ import { Scope } from "@zag-js/core";
2
+ export declare const getRootId: (ctx: Scope) => any;
3
+ export declare const getListId: (ctx: Scope) => any;
4
+ export declare const getContentId: (ctx: Scope, value: string) => any;
5
+ export declare const getTriggerId: (ctx: Scope, value: string) => any;
6
+ export declare const getIndicatorId: (ctx: Scope) => any;
7
+ export declare const getListEl: (ctx: Scope) => HTMLElement | null;
8
+ export declare const getContentEl: (ctx: Scope, value: string) => HTMLElement | null;
9
+ export declare const getTriggerEl: (ctx: Scope, value: string | null) => HTMLElement | null;
10
+ export declare const getIndicatorEl: (ctx: Scope) => HTMLElement | null;
11
+ export declare const getElements: (ctx: Scope) => HTMLElement[];
12
+ export declare const getFirstTriggerEl: (ctx: Scope) => HTMLElement | undefined;
13
+ export declare const getLastTriggerEl: (ctx: Scope) => HTMLElement | undefined;
14
+ export declare const getNextTriggerEl: (ctx: Scope, opts: {
15
+ value: string;
16
+ loopFocus?: boolean | undefined;
18
17
  }) => HTMLElement;
19
- declare const getPrevTriggerEl: (ctx: Scope, opts: {
20
- value: string;
21
- loopFocus?: boolean | undefined;
18
+ export declare const getPrevTriggerEl: (ctx: Scope, opts: {
19
+ value: string;
20
+ loopFocus?: boolean | undefined;
22
21
  }) => HTMLElement | null;
23
- declare const getOffsetRect: (el: HTMLElement | undefined) => {
24
- x: number;
25
- y: number;
26
- width: number;
27
- height: number;
22
+ export declare const getOffsetRect: (el: HTMLElement | undefined) => {
23
+ x: number;
24
+ y: number;
25
+ width: number;
26
+ height: number;
28
27
  };
29
- declare const getRectByValue: (ctx: Scope, value: string) => {
30
- x: number;
31
- y: number;
32
- width: number;
33
- height: number;
34
- };
35
-
36
- export { getContentEl, getContentId, getElements, getFirstTriggerEl, getIndicatorEl, getIndicatorId, getLastTriggerEl, getListEl, getListId, getNextTriggerEl, getOffsetRect, getPrevTriggerEl, getRectByValue, getRootId, getTriggerEl, getTriggerId };
28
+ export declare const getRectByValue: (ctx: Scope, value: string) => {
29
+ x: number;
30
+ y: number;
31
+ width: number;
32
+ height: number;
33
+ };
@@ -1,36 +1,33 @@
1
- import { Scope } from '@zag-js/core';
2
-
3
- declare const getRootId: (ctx: Scope) => any;
4
- declare const getListId: (ctx: Scope) => any;
5
- declare const getContentId: (ctx: Scope, value: string) => any;
6
- declare const getTriggerId: (ctx: Scope, value: string) => any;
7
- declare const getIndicatorId: (ctx: Scope) => any;
8
- declare const getListEl: (ctx: Scope) => HTMLElement | null;
9
- declare const getContentEl: (ctx: Scope, value: string) => HTMLElement | null;
10
- declare const getTriggerEl: (ctx: Scope, value: string | null) => HTMLElement | null;
11
- declare const getIndicatorEl: (ctx: Scope) => HTMLElement | null;
12
- declare const getElements: (ctx: Scope) => HTMLElement[];
13
- declare const getFirstTriggerEl: (ctx: Scope) => HTMLElement | undefined;
14
- declare const getLastTriggerEl: (ctx: Scope) => HTMLElement | undefined;
15
- declare const getNextTriggerEl: (ctx: Scope, opts: {
16
- value: string;
17
- loopFocus?: boolean | undefined;
1
+ import { Scope } from "@zag-js/core";
2
+ export declare const getRootId: (ctx: Scope) => any;
3
+ export declare const getListId: (ctx: Scope) => any;
4
+ export declare const getContentId: (ctx: Scope, value: string) => any;
5
+ export declare const getTriggerId: (ctx: Scope, value: string) => any;
6
+ export declare const getIndicatorId: (ctx: Scope) => any;
7
+ export declare const getListEl: (ctx: Scope) => HTMLElement | null;
8
+ export declare const getContentEl: (ctx: Scope, value: string) => HTMLElement | null;
9
+ export declare const getTriggerEl: (ctx: Scope, value: string | null) => HTMLElement | null;
10
+ export declare const getIndicatorEl: (ctx: Scope) => HTMLElement | null;
11
+ export declare const getElements: (ctx: Scope) => HTMLElement[];
12
+ export declare const getFirstTriggerEl: (ctx: Scope) => HTMLElement | undefined;
13
+ export declare const getLastTriggerEl: (ctx: Scope) => HTMLElement | undefined;
14
+ export declare const getNextTriggerEl: (ctx: Scope, opts: {
15
+ value: string;
16
+ loopFocus?: boolean | undefined;
18
17
  }) => HTMLElement;
19
- declare const getPrevTriggerEl: (ctx: Scope, opts: {
20
- value: string;
21
- loopFocus?: boolean | undefined;
18
+ export declare const getPrevTriggerEl: (ctx: Scope, opts: {
19
+ value: string;
20
+ loopFocus?: boolean | undefined;
22
21
  }) => HTMLElement | null;
23
- declare const getOffsetRect: (el: HTMLElement | undefined) => {
24
- x: number;
25
- y: number;
26
- width: number;
27
- height: number;
22
+ export declare const getOffsetRect: (el: HTMLElement | undefined) => {
23
+ x: number;
24
+ y: number;
25
+ width: number;
26
+ height: number;
28
27
  };
29
- declare const getRectByValue: (ctx: Scope, value: string) => {
30
- x: number;
31
- y: number;
32
- width: number;
33
- height: number;
34
- };
35
-
36
- export { getContentEl, getContentId, getElements, getFirstTriggerEl, getIndicatorEl, getIndicatorId, getLastTriggerEl, getListEl, getListId, getNextTriggerEl, getOffsetRect, getPrevTriggerEl, getRectByValue, getRootId, getTriggerEl, getTriggerId };
28
+ export declare const getRectByValue: (ctx: Scope, value: string) => {
29
+ x: number;
30
+ y: number;
31
+ width: number;
32
+ height: number;
33
+ };