@zag-js/tabs 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,166 +0,0 @@
1
- import {
2
- parts
3
- } from "./chunk-S2KW2DT4.mjs";
4
- import {
5
- dom
6
- } from "./chunk-EU4R7SET.mjs";
7
-
8
- // src/tabs.connect.ts
9
- import { getEventKey } from "@zag-js/dom-event";
10
- import { dataAttr, isSafari } from "@zag-js/dom-query";
11
- function connect(state, send, normalize) {
12
- const translations = state.context.translations;
13
- const isFocused = state.matches("focused");
14
- return {
15
- /**
16
- * The current value of the tabs.
17
- */
18
- value: state.context.value,
19
- /**
20
- * The value of the tab that is currently focused.
21
- */
22
- focusedValue: state.context.focusedValue,
23
- /**
24
- * The previous values of the tabs in sequence of selection.
25
- */
26
- previousValues: Array.from(state.context.previousValues),
27
- /**
28
- * Sets the value of the tabs.
29
- */
30
- setValue(value) {
31
- send({ type: "SET_VALUE", value });
32
- },
33
- /**
34
- * Clears the value of the tabs.
35
- */
36
- clearValue() {
37
- send({ type: "CLEAR_VALUE" });
38
- },
39
- /**
40
- * Sets the indicator rect to the tab with the given id.
41
- */
42
- setIndicatorRect(id) {
43
- send({ type: "SET_INDICATOR_RECT", id });
44
- },
45
- rootProps: normalize.element({
46
- ...parts.root.attrs,
47
- id: dom.getRootId(state.context),
48
- "data-orientation": state.context.orientation,
49
- "data-focus": dataAttr(isFocused),
50
- dir: state.context.dir
51
- }),
52
- tablistProps: normalize.element({
53
- ...parts.tablist.attrs,
54
- id: dom.getTablistId(state.context),
55
- role: "tablist",
56
- "data-focus": dataAttr(isFocused),
57
- "aria-orientation": state.context.orientation,
58
- "data-orientation": state.context.orientation,
59
- "aria-label": translations.tablistLabel,
60
- onKeyDown(event) {
61
- const keyMap = {
62
- ArrowDown() {
63
- send("ARROW_DOWN");
64
- },
65
- ArrowUp() {
66
- send("ARROW_UP");
67
- },
68
- ArrowLeft() {
69
- send("ARROW_LEFT");
70
- },
71
- ArrowRight() {
72
- send("ARROW_RIGHT");
73
- },
74
- Home() {
75
- send("HOME");
76
- },
77
- End() {
78
- send("END");
79
- },
80
- Enter() {
81
- send({ type: "ENTER", value: state.context.focusedValue });
82
- }
83
- };
84
- let key = getEventKey(event, state.context);
85
- const exec = keyMap[key];
86
- if (exec) {
87
- event.preventDefault();
88
- exec(event);
89
- }
90
- }
91
- }),
92
- getTriggerProps(props) {
93
- const { value, disabled } = props;
94
- const selected = state.context.value === value;
95
- return normalize.button({
96
- ...parts.trigger.attrs,
97
- role: "tab",
98
- type: "button",
99
- disabled,
100
- "data-orientation": state.context.orientation,
101
- "data-disabled": dataAttr(disabled),
102
- "aria-disabled": disabled,
103
- "data-value": value,
104
- "aria-selected": selected,
105
- "data-selected": dataAttr(selected),
106
- "aria-controls": dom.getContentId(state.context, value),
107
- "data-ownedby": dom.getTablistId(state.context),
108
- id: dom.getTriggerId(state.context, value),
109
- tabIndex: selected ? 0 : -1,
110
- onFocus() {
111
- send({ type: "TAB_FOCUS", value });
112
- },
113
- onBlur(event) {
114
- const target = event.relatedTarget;
115
- if (target?.getAttribute("role") !== "tab") {
116
- send({ type: "TAB_BLUR" });
117
- }
118
- },
119
- onClick(event) {
120
- if (disabled)
121
- return;
122
- if (isSafari()) {
123
- event.currentTarget.focus();
124
- }
125
- send({ type: "TAB_CLICK", value });
126
- }
127
- });
128
- },
129
- contentGroupProps: normalize.element({
130
- ...parts.contentGroup.attrs,
131
- id: dom.getContentGroupId(state.context),
132
- "data-orientation": state.context.orientation
133
- }),
134
- getContentProps({ value }) {
135
- const selected = state.context.value === value;
136
- return normalize.element({
137
- ...parts.content.attrs,
138
- id: dom.getContentId(state.context, value),
139
- tabIndex: 0,
140
- "aria-labelledby": dom.getTriggerId(state.context, value),
141
- role: "tabpanel",
142
- "data-ownedby": dom.getTablistId(state.context),
143
- hidden: !selected
144
- });
145
- },
146
- indicatorProps: normalize.element({
147
- id: dom.getIndicatorId(state.context),
148
- ...parts.indicator.attrs,
149
- "data-orientation": state.context.orientation,
150
- style: {
151
- "--transition-duration": "150ms",
152
- "--transition-property": "left, right, top, bottom, width, height",
153
- position: "absolute",
154
- willChange: "var(--transition-property)",
155
- transitionProperty: "var(--transition-property)",
156
- transitionDuration: state.context.canIndicatorTransition ? "var(--transition-duration)" : "0ms",
157
- transitionTimingFunction: "var(--transition-timing-function)",
158
- ...state.context.indicatorRect
159
- }
160
- })
161
- };
162
- }
163
-
164
- export {
165
- connect
166
- };
@@ -1,252 +0,0 @@
1
- import {
2
- dom
3
- } from "./chunk-EU4R7SET.mjs";
4
-
5
- // src/tabs.machine.ts
6
- import { createMachine, guards } from "@zag-js/core";
7
- import { nextTick, raf } from "@zag-js/dom-query";
8
- import { trackElementRect } from "@zag-js/element-rect";
9
- import { getFocusables } from "@zag-js/tabbable";
10
- import { compact } from "@zag-js/utils";
11
- var { not } = guards;
12
- function machine(userContext) {
13
- const ctx = compact(userContext);
14
- return createMachine(
15
- {
16
- initial: "idle",
17
- context: {
18
- dir: "ltr",
19
- orientation: "horizontal",
20
- activationMode: "automatic",
21
- value: null,
22
- focusedValue: null,
23
- previousValues: [],
24
- indicatorRect: {
25
- left: "0px",
26
- top: "0px",
27
- width: "0px",
28
- height: "0px"
29
- },
30
- canIndicatorTransition: false,
31
- isIndicatorRendered: false,
32
- loop: true,
33
- translations: {},
34
- ...ctx
35
- },
36
- computed: {
37
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
38
- isVertical: (ctx2) => ctx2.orientation === "vertical"
39
- },
40
- created: ["setPrevSelectedTabs"],
41
- entry: ["checkRenderedElements", "syncIndicatorRect", "setContentTabIndex"],
42
- exit: ["cleanupObserver"],
43
- watch: {
44
- focusedValue: "invokeOnFocus",
45
- value: [
46
- "enableIndicatorTransition",
47
- "invokeOnChange",
48
- "setPrevSelectedTabs",
49
- "syncIndicatorRect",
50
- "setContentTabIndex"
51
- ],
52
- dir: ["syncIndicatorRect"],
53
- orientation: ["syncIndicatorRect"]
54
- },
55
- on: {
56
- SET_VALUE: {
57
- actions: "setValue"
58
- },
59
- CLEAR_VALUE: {
60
- actions: "clearValue"
61
- },
62
- SET_INDICATOR_RECT: {
63
- actions: "setIndicatorRect"
64
- }
65
- },
66
- states: {
67
- idle: {
68
- on: {
69
- TAB_FOCUS: [
70
- {
71
- guard: "selectOnFocus",
72
- target: "focused",
73
- actions: ["setFocusedValue", "setValue"]
74
- },
75
- {
76
- target: "focused",
77
- actions: "setFocusedValue"
78
- }
79
- ],
80
- TAB_CLICK: {
81
- target: "focused",
82
- actions: ["setFocusedValue", "setValue"]
83
- }
84
- }
85
- },
86
- focused: {
87
- on: {
88
- TAB_CLICK: {
89
- target: "focused",
90
- actions: ["setFocusedValue", "setValue"]
91
- },
92
- ARROW_LEFT: {
93
- guard: "isHorizontal",
94
- actions: "focusPrevTab"
95
- },
96
- ARROW_RIGHT: {
97
- guard: "isHorizontal",
98
- actions: "focusNextTab"
99
- },
100
- ARROW_UP: {
101
- guard: "isVertical",
102
- actions: "focusPrevTab"
103
- },
104
- ARROW_DOWN: {
105
- guard: "isVertical",
106
- actions: "focusNextTab"
107
- },
108
- HOME: {
109
- actions: "focusFirstTab"
110
- },
111
- END: {
112
- actions: "focusLastTab"
113
- },
114
- ENTER: {
115
- guard: not("selectOnFocus"),
116
- actions: "setValue"
117
- },
118
- TAB_FOCUS: [
119
- {
120
- guard: "selectOnFocus",
121
- actions: ["setFocusedValue", "setValue"]
122
- },
123
- { actions: "setFocusedValue" }
124
- ],
125
- TAB_BLUR: {
126
- target: "idle",
127
- actions: "clearFocusedValue"
128
- }
129
- }
130
- }
131
- }
132
- },
133
- {
134
- guards: {
135
- isVertical: (ctx2) => ctx2.isVertical,
136
- isHorizontal: (ctx2) => ctx2.isHorizontal,
137
- selectOnFocus: (ctx2) => ctx2.activationMode === "automatic"
138
- },
139
- actions: {
140
- setFocusedValue(ctx2, evt) {
141
- ctx2.focusedValue = evt.value;
142
- },
143
- clearFocusedValue(ctx2) {
144
- ctx2.focusedValue = null;
145
- },
146
- setValue(ctx2, evt) {
147
- ctx2.value = evt.value;
148
- },
149
- clearValue(ctx2) {
150
- ctx2.value = null;
151
- },
152
- focusFirstTab(ctx2) {
153
- raf(() => dom.getFirstEl(ctx2)?.focus());
154
- },
155
- focusLastTab(ctx2) {
156
- raf(() => dom.getLastEl(ctx2)?.focus());
157
- },
158
- focusNextTab(ctx2) {
159
- if (!ctx2.focusedValue)
160
- return;
161
- const next = dom.getNextEl(ctx2, ctx2.focusedValue);
162
- raf(() => next?.focus());
163
- },
164
- focusPrevTab(ctx2) {
165
- if (!ctx2.focusedValue)
166
- return;
167
- const prev = dom.getPrevEl(ctx2, ctx2.focusedValue);
168
- raf(() => prev?.focus());
169
- },
170
- checkRenderedElements(ctx2) {
171
- ctx2.isIndicatorRendered = !!dom.getIndicatorEl(ctx2);
172
- },
173
- invokeOnChange(ctx2) {
174
- ctx2.onChange?.({ value: ctx2.value });
175
- },
176
- invokeOnFocus(ctx2) {
177
- ctx2.onFocus?.({ value: ctx2.focusedValue });
178
- },
179
- setPrevSelectedTabs(ctx2) {
180
- if (ctx2.value != null) {
181
- ctx2.previousValues = pushUnique(ctx2.previousValues, ctx2.value);
182
- }
183
- },
184
- // if tab panel contains focusable elements, remove the tabindex attribute
185
- setContentTabIndex(ctx2) {
186
- raf(() => {
187
- const panel = dom.getActiveContentEl(ctx2);
188
- if (!panel)
189
- return;
190
- const focusables = getFocusables(panel);
191
- if (focusables.length > 0) {
192
- panel.removeAttribute("tabindex");
193
- } else {
194
- panel.setAttribute("tabindex", "0");
195
- }
196
- });
197
- },
198
- cleanupObserver(ctx2) {
199
- ctx2.indicatorCleanup?.();
200
- },
201
- enableIndicatorTransition(ctx2) {
202
- ctx2.canIndicatorTransition = true;
203
- },
204
- setIndicatorRect(ctx2, evt) {
205
- const value = evt.id ?? ctx2.value;
206
- if (!ctx2.isIndicatorRendered || !value)
207
- return;
208
- const tabEl = dom.getTriggerEl(ctx2, value);
209
- if (!tabEl)
210
- return;
211
- ctx2.indicatorRect = dom.getRectById(ctx2, value);
212
- nextTick(() => {
213
- ctx2.canIndicatorTransition = false;
214
- });
215
- },
216
- syncIndicatorRect(ctx2) {
217
- ctx2.indicatorCleanup?.();
218
- const value = ctx2.value;
219
- if (!ctx2.isIndicatorRendered || !value)
220
- return;
221
- const tabEl = dom.getActiveTabEl(ctx2);
222
- if (!tabEl)
223
- return;
224
- ctx2.indicatorCleanup = trackElementRect(tabEl, {
225
- getRect(el) {
226
- return dom.getOffsetRect(el);
227
- },
228
- onChange(rect) {
229
- ctx2.indicatorRect = dom.resolveRect(rect, ctx2.orientation);
230
- nextTick(() => {
231
- ctx2.canIndicatorTransition = false;
232
- });
233
- }
234
- });
235
- }
236
- }
237
- }
238
- );
239
- }
240
- function pushUnique(arr, value) {
241
- const newArr = Array.from(arr).slice();
242
- const index = newArr.indexOf(value);
243
- if (index > -1) {
244
- newArr.splice(index, 1);
245
- }
246
- newArr.push(value);
247
- return newArr;
248
- }
249
-
250
- export {
251
- machine
252
- };
@@ -1,9 +0,0 @@
1
- // src/tabs.anatomy.ts
2
- import { createAnatomy } from "@zag-js/anatomy";
3
- var anatomy = createAnatomy("tabs").parts("root", "tablist", "trigger", "contentGroup", "content", "indicator");
4
- var parts = anatomy.build();
5
-
6
- export {
7
- anatomy,
8
- parts
9
- };
@@ -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/tabs.types.ts
17
- var tabs_types_exports = {};
18
- module.exports = __toCommonJS(tabs_types_exports);
File without changes