@tamagui/tabs-headless 2.0.0-rc.4 → 2.0.0-rc.40

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.
@@ -3,15 +3,17 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
5
  var __copyProps = (to, from, except, desc) => {
6
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
7
8
  get: () => from[key],
8
9
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
9
10
  });
10
- return to;
11
- },
12
- __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
13
15
  var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
14
- value: !0
16
+ value: true
15
17
  }), mod);
16
18
  var index_exports = {};
17
19
  module.exports = __toCommonJS(index_exports);
@@ -5,15 +5,17 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __copyProps = (to, from, except, desc) => {
8
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ if (from && typeof from === "object" || typeof from === "function") {
9
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
9
10
  get: () => from[key],
10
11
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
11
12
  });
12
- return to;
13
- },
14
- __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
13
+ }
14
+ return to;
15
+ };
16
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
17
  var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
16
- value: !0
18
+ value: true
17
19
  }), mod);
18
20
  var index_exports = {};
19
21
  module.exports = __toCommonJS(index_exports);
@@ -1 +1 @@
1
- {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","module","exports"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAP,YAAc,CAAAK,aAAA","ignoreList":[]}
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","module","exports"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAP,YAAc,CAAAK,aAAA","ignoreList":[]}
@@ -2,33 +2,35 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: !0
11
- });
12
- },
13
- __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
16
  get: () => from[key],
16
17
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
18
  });
18
- return to;
19
- };
19
+ }
20
+ return to;
21
+ };
20
22
  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", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: true
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
32
34
  var useTabs_exports = {};
33
35
  __export(useTabs_exports, {
34
36
  TabsProvider: () => TabsProvider,
@@ -38,110 +40,153 @@ __export(useTabs_exports, {
38
40
  useTabsContext: () => useTabsContext
39
41
  });
40
42
  module.exports = __toCommonJS(useTabs_exports);
41
- var import_helpers = require("@tamagui/helpers"),
42
- import_use_controllable_state = require("@tamagui/use-controllable-state"),
43
- import_use_direction = require("@tamagui/use-direction"),
44
- React = __toESM(require("react"), 1),
45
- import_react = require("react");
43
+ var import_helpers = require("@tamagui/helpers");
44
+ var import_use_controllable_state = require("@tamagui/use-controllable-state");
45
+ var import_use_direction = require("@tamagui/use-direction");
46
+ var React = __toESM(require("react"), 1);
47
+ var import_react = require("react");
46
48
  function useTabs(props = {}) {
47
49
  const {
48
- value: valueProp,
49
- onValueChange,
50
- defaultValue = "",
51
- orientation = "horizontal",
52
- dir,
53
- activationMode = "automatic",
54
- loop = !0
55
- } = props,
56
- direction = (0, import_use_direction.useDirection)(dir),
57
- baseId = (0, import_react.useId)(),
58
- [value, setValue] = (0, import_use_controllable_state.useControllableState)({
59
- prop: valueProp,
60
- onChange: onValueChange,
61
- defaultProp: defaultValue
62
- }),
63
- tabRefs = (0, import_react.useRef)(/* @__PURE__ */new Map()),
64
- [tabValues, setTabValues] = (0, import_react.useState)([]),
65
- registerTab = (0, import_react.useCallback)((tabValue, element) => {
66
- element ? (tabRefs.current.set(tabValue, element), setTabValues(prev => prev.includes(tabValue) ? prev : [...prev, tabValue])) : (tabRefs.current.delete(tabValue), setTabValues(prev => prev.filter(v => v !== tabValue)));
67
- }, []),
68
- focusTab = (0, import_react.useCallback)(tabValue => {
69
- tabRefs.current.get(tabValue)?.focus();
70
- }, []),
71
- getNextTab = (0, import_react.useCallback)((currentValue, direction2) => {
72
- const currentIndex = tabValues.indexOf(currentValue);
73
- if (currentIndex === -1) return null;
74
- let nextIndex = currentIndex + direction2;
75
- if (loop) nextIndex = (nextIndex + tabValues.length) % tabValues.length;else if (nextIndex < 0 || nextIndex >= tabValues.length) return null;
76
- return tabValues[nextIndex] ?? null;
77
- }, [tabValues, loop]),
78
- handleKeyDown = (0, import_react.useCallback)(tabValue => event => {
79
- const isHorizontal = orientation === "horizontal",
80
- isRtl = direction === "rtl";
81
- let nextTab = null;
82
- switch (event.key) {
83
- case "ArrowRight":
84
- isHorizontal && (nextTab = getNextTab(tabValue, isRtl ? -1 : 1));
85
- break;
86
- case "ArrowLeft":
87
- isHorizontal && (nextTab = getNextTab(tabValue, isRtl ? 1 : -1));
88
- break;
89
- case "ArrowDown":
90
- isHorizontal || (nextTab = getNextTab(tabValue, 1));
91
- break;
92
- case "ArrowUp":
93
- isHorizontal || (nextTab = getNextTab(tabValue, -1));
94
- break;
95
- case "Home":
96
- nextTab = tabValues[0] ?? null;
97
- break;
98
- case "End":
99
- nextTab = tabValues[tabValues.length - 1] ?? null;
100
- break;
101
- case " ":
102
- case "Enter":
103
- activationMode === "manual" && (setValue(tabValue), event.preventDefault());
104
- return;
50
+ value: valueProp,
51
+ onValueChange,
52
+ defaultValue = "",
53
+ orientation = "horizontal",
54
+ dir,
55
+ activationMode = "automatic",
56
+ loop = true
57
+ } = props;
58
+ const direction = (0, import_use_direction.useDirection)(dir);
59
+ const baseId = (0, import_react.useId)();
60
+ const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
61
+ prop: valueProp,
62
+ onChange: onValueChange,
63
+ defaultProp: defaultValue
64
+ });
65
+ const tabRefs = (0, import_react.useRef)(/* @__PURE__ */new Map());
66
+ const [tabValues, setTabValues] = (0, import_react.useState)([]);
67
+ const registerTab = (0, import_react.useCallback)((tabValue, element) => {
68
+ if (element) {
69
+ tabRefs.current.set(tabValue, element);
70
+ setTabValues(prev => prev.includes(tabValue) ? prev : [...prev, tabValue]);
71
+ } else {
72
+ tabRefs.current.delete(tabValue);
73
+ setTabValues(prev => prev.filter(v => v !== tabValue));
74
+ }
75
+ }, []);
76
+ const focusTab = (0, import_react.useCallback)(tabValue => {
77
+ const element = tabRefs.current.get(tabValue);
78
+ element?.focus();
79
+ }, []);
80
+ const getNextTab = (0, import_react.useCallback)((currentValue, direction2) => {
81
+ const currentIndex = tabValues.indexOf(currentValue);
82
+ if (currentIndex === -1) return null;
83
+ let nextIndex = currentIndex + direction2;
84
+ if (loop) {
85
+ nextIndex = (nextIndex + tabValues.length) % tabValues.length;
86
+ } else {
87
+ if (nextIndex < 0 || nextIndex >= tabValues.length) return null;
88
+ }
89
+ return tabValues[nextIndex] ?? null;
90
+ }, [tabValues, loop]);
91
+ const handleKeyDown = (0, import_react.useCallback)(tabValue => event => {
92
+ const isHorizontal = orientation === "horizontal";
93
+ const isRtl = direction === "rtl";
94
+ let nextTab = null;
95
+ switch (event.key) {
96
+ case "ArrowRight":
97
+ if (isHorizontal) {
98
+ nextTab = getNextTab(tabValue, isRtl ? -1 : 1);
99
+ }
100
+ break;
101
+ case "ArrowLeft":
102
+ if (isHorizontal) {
103
+ nextTab = getNextTab(tabValue, isRtl ? 1 : -1);
104
+ }
105
+ break;
106
+ case "ArrowDown":
107
+ if (!isHorizontal) {
108
+ nextTab = getNextTab(tabValue, 1);
109
+ }
110
+ break;
111
+ case "ArrowUp":
112
+ if (!isHorizontal) {
113
+ nextTab = getNextTab(tabValue, -1);
114
+ }
115
+ break;
116
+ case "Home":
117
+ nextTab = tabValues[0] ?? null;
118
+ break;
119
+ case "End":
120
+ nextTab = tabValues[tabValues.length - 1] ?? null;
121
+ break;
122
+ case " ":
123
+ case "Enter":
124
+ if (activationMode === "manual") {
125
+ setValue(tabValue);
126
+ event.preventDefault();
127
+ }
128
+ return;
129
+ }
130
+ if (nextTab) {
131
+ event.preventDefault();
132
+ focusTab(nextTab);
133
+ if (activationMode === "automatic") {
134
+ setValue(nextTab);
105
135
  }
106
- nextTab && (event.preventDefault(), focusTab(nextTab), activationMode === "automatic" && setValue(nextTab));
107
- }, [orientation, direction, getNextTab, tabValues, activationMode, setValue, focusTab]),
108
- getTabProps = (0, import_react.useCallback)((tabValue, disabled) => {
109
- const isSelected = value === tabValue,
110
- triggerId = makeTriggerId(baseId, tabValue),
111
- contentId = makeContentId(baseId, tabValue);
112
- return {
113
- role: "tab",
114
- id: triggerId,
115
- "aria-selected": isSelected,
116
- "aria-controls": contentId,
117
- "data-state": isSelected ? "active" : "inactive",
118
- ...(disabled && {
119
- "data-disabled": ""
120
- }),
121
- disabled,
122
- tabIndex: isSelected ? 0 : -1,
123
- onKeyDown: disabled ? () => {} : handleKeyDown(tabValue),
124
- onClick: event => {
125
- !disabled && !isSelected && setValue(tabValue);
126
- },
127
- onFocus: event => {
128
- registerTab(tabValue, event.currentTarget), !disabled && !isSelected && activationMode === "automatic" && setValue(tabValue);
136
+ }
137
+ }, [orientation, direction, getNextTab, tabValues, activationMode, setValue, focusTab]);
138
+ const getTabProps = (0, import_react.useCallback)((tabValue, disabled) => {
139
+ const isSelected = value === tabValue;
140
+ const triggerId = makeTriggerId(baseId, tabValue);
141
+ const contentId = makeContentId(baseId, tabValue);
142
+ return {
143
+ role: "tab",
144
+ id: triggerId,
145
+ "aria-selected": isSelected,
146
+ "aria-controls": contentId,
147
+ "data-state": isSelected ? "active" : "inactive",
148
+ ...(disabled && {
149
+ "data-disabled": ""
150
+ }),
151
+ disabled,
152
+ tabIndex: isSelected ? 0 : -1,
153
+ onKeyDown: disabled ? () => {} : handleKeyDown(tabValue),
154
+ onClick: event => {
155
+ if (!disabled && !isSelected) {
156
+ setValue(tabValue);
129
157
  }
130
- };
131
- }, [value, baseId, handleKeyDown, setValue, activationMode, registerTab]),
132
- getContentProps = (0, import_react.useCallback)(tabValue => {
133
- const isSelected = value === tabValue,
134
- triggerId = makeTriggerId(baseId, tabValue);
135
- return {
136
- role: "tabpanel",
137
- id: makeContentId(baseId, tabValue),
138
- "aria-labelledby": triggerId,
139
- "data-state": isSelected ? "active" : "inactive",
140
- "data-orientation": orientation,
141
- hidden: !isSelected,
142
- tabIndex: 0
143
- };
144
- }, [value, baseId, orientation]);
158
+ },
159
+ onFocus: event => {
160
+ registerTab(tabValue, event.currentTarget);
161
+ if (!disabled && !isSelected && activationMode === "automatic") {
162
+ setValue(tabValue);
163
+ }
164
+ }
165
+ };
166
+ }, [value, baseId, handleKeyDown, setValue, activationMode, registerTab]);
167
+ const getContentProps = (0, import_react.useCallback)(tabValue => {
168
+ const isSelected = value === tabValue;
169
+ const triggerId = makeTriggerId(baseId, tabValue);
170
+ const contentId = makeContentId(baseId, tabValue);
171
+ return {
172
+ role: "tabpanel",
173
+ id: contentId,
174
+ "aria-labelledby": triggerId,
175
+ "data-state": isSelected ? "active" : "inactive",
176
+ "data-orientation": orientation,
177
+ hidden: !isSelected,
178
+ tabIndex: 0
179
+ };
180
+ }, [value, baseId, orientation]);
181
+ const contextValue = {
182
+ baseId,
183
+ value,
184
+ setValue,
185
+ orientation,
186
+ direction,
187
+ activationMode,
188
+ loop
189
+ };
145
190
  return {
146
191
  value,
147
192
  setValue,
@@ -156,46 +201,40 @@ function useTabs(props = {}) {
156
201
  },
157
202
  getTabProps,
158
203
  getContentProps,
159
- contextValue: {
160
- baseId,
161
- value,
162
- setValue,
163
- orientation,
164
- direction,
165
- activationMode,
166
- loop
167
- }
204
+ contextValue
168
205
  };
169
206
  }
170
- const TabsContext = React.createContext(null),
171
- TabsProvider = TabsContext.Provider;
207
+ const TabsContext = React.createContext(null);
208
+ const TabsProvider = TabsContext.Provider;
172
209
  function useTabsContext() {
173
210
  const context = React.useContext(TabsContext);
174
- if (!context) throw new Error("useTabsContext must be used within a TabsProvider");
211
+ if (!context) {
212
+ throw new Error("useTabsContext must be used within a TabsProvider");
213
+ }
175
214
  return context;
176
215
  }
177
216
  function useTab(props) {
178
217
  const {
179
- value: tabValue,
180
- disabled,
181
- onPress,
182
- onKeyDown,
183
- onFocus
184
- } = props,
185
- context = useTabsContext(),
186
- {
187
- value,
188
- setValue,
189
- baseId,
190
- activationMode,
191
- orientation,
192
- direction,
193
- loop
194
- } = context,
195
- isSelected = value === tabValue,
196
- triggerId = makeTriggerId(baseId, tabValue),
197
- contentId = makeContentId(baseId, tabValue),
198
- ref = (0, import_react.useRef)(null);
218
+ value: tabValue,
219
+ disabled,
220
+ onPress,
221
+ onKeyDown,
222
+ onFocus
223
+ } = props;
224
+ const context = useTabsContext();
225
+ const {
226
+ value,
227
+ setValue,
228
+ baseId,
229
+ activationMode,
230
+ orientation,
231
+ direction,
232
+ loop
233
+ } = context;
234
+ const isSelected = value === tabValue;
235
+ const triggerId = makeTriggerId(baseId, tabValue);
236
+ const contentId = makeContentId(baseId, tabValue);
237
+ const ref = (0, import_react.useRef)(null);
199
238
  return {
200
239
  isSelected,
201
240
  tabProps: {
@@ -211,31 +250,39 @@ function useTab(props) {
211
250
  disabled,
212
251
  tabIndex: isSelected ? 0 : -1,
213
252
  onKeyDown: (0, import_helpers.composeEventHandlers)(onKeyDown, event => {
214
- disabled || [" ", "Enter"].includes(event.key) && (setValue(tabValue), event.preventDefault());
253
+ if (disabled) return;
254
+ if ([" ", "Enter"].includes(event.key)) {
255
+ setValue(tabValue);
256
+ event.preventDefault();
257
+ }
215
258
  }),
216
259
  onPress: (0, import_helpers.composeEventHandlers)(onPress, () => {
217
- !disabled && !isSelected && setValue(tabValue);
260
+ if (!disabled && !isSelected) {
261
+ setValue(tabValue);
262
+ }
218
263
  }),
219
264
  onFocus: (0, import_helpers.composeEventHandlers)(onFocus, () => {
220
- !disabled && !isSelected && activationMode === "automatic" && setValue(tabValue);
265
+ if (!disabled && !isSelected && activationMode === "automatic") {
266
+ setValue(tabValue);
267
+ }
221
268
  })
222
269
  }
223
270
  };
224
271
  }
225
272
  function useTabContent(props) {
226
273
  const {
227
- value: tabValue,
228
- forceMount
229
- } = props,
230
- context = useTabsContext(),
231
- {
232
- value,
233
- baseId,
234
- orientation
235
- } = context,
236
- isSelected = value === tabValue,
237
- triggerId = makeTriggerId(baseId, tabValue),
238
- contentId = makeContentId(baseId, tabValue);
274
+ value: tabValue,
275
+ forceMount
276
+ } = props;
277
+ const context = useTabsContext();
278
+ const {
279
+ value,
280
+ baseId,
281
+ orientation
282
+ } = context;
283
+ const isSelected = value === tabValue;
284
+ const triggerId = makeTriggerId(baseId, tabValue);
285
+ const contentId = makeContentId(baseId, tabValue);
239
286
  return {
240
287
  isSelected,
241
288
  shouldMount: forceMount || isSelected,