@tamagui/tabs-headless 2.0.0-1
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.
- package/dist/cjs/index.cjs +18 -0
- package/dist/cjs/index.js +15 -0
- package/dist/cjs/index.js.map +6 -0
- package/dist/cjs/index.native.js +21 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/cjs/useTabs.cjs +258 -0
- package/dist/cjs/useTabs.js +210 -0
- package/dist/cjs/useTabs.js.map +6 -0
- package/dist/cjs/useTabs.native.js +276 -0
- package/dist/cjs/useTabs.native.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +6 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +2 -0
- package/dist/esm/index.native.js.map +1 -0
- package/dist/esm/useTabs.js +190 -0
- package/dist/esm/useTabs.js.map +6 -0
- package/dist/esm/useTabs.mjs +220 -0
- package/dist/esm/useTabs.mjs.map +1 -0
- package/dist/esm/useTabs.native.js +235 -0
- package/dist/esm/useTabs.native.js.map +1 -0
- package/dist/jsx/index.js +2 -0
- package/dist/jsx/index.js.map +6 -0
- package/dist/jsx/index.mjs +2 -0
- package/dist/jsx/index.mjs.map +1 -0
- package/dist/jsx/index.native.js +21 -0
- package/dist/jsx/index.native.js.map +1 -0
- package/dist/jsx/useTabs.js +190 -0
- package/dist/jsx/useTabs.js.map +6 -0
- package/dist/jsx/useTabs.mjs +220 -0
- package/dist/jsx/useTabs.mjs.map +1 -0
- package/dist/jsx/useTabs.native.js +276 -0
- package/dist/jsx/useTabs.native.js.map +1 -0
- package/package.json +54 -0
- package/src/index.ts +1 -0
- package/src/useTabs.tsx +406 -0
- package/types/index.d.ts +2 -0
- package/types/index.d.ts.map +1 -0
- package/types/useTabs.d.ts +132 -0
- package/types/useTabs.d.ts.map +1 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
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, {
|
|
7
|
+
get: () => from[key],
|
|
8
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
9
|
+
});
|
|
10
|
+
return to;
|
|
11
|
+
},
|
|
12
|
+
__reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
13
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
14
|
+
value: !0
|
|
15
|
+
}), mod);
|
|
16
|
+
var index_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(index_exports);
|
|
18
|
+
__reExport(index_exports, require("./useTabs.cjs"), module.exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
9
|
+
return to;
|
|
10
|
+
}, __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
11
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
12
|
+
var index_exports = {};
|
|
13
|
+
module.exports = __toCommonJS(index_exports);
|
|
14
|
+
__reExport(index_exports, require("./useTabs"), module.exports);
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
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, {
|
|
9
|
+
get: () => from[key],
|
|
10
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
11
|
+
});
|
|
12
|
+
return to;
|
|
13
|
+
},
|
|
14
|
+
__reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
16
|
+
value: !0
|
|
17
|
+
}), mod);
|
|
18
|
+
var index_exports = {};
|
|
19
|
+
module.exports = __toCommonJS(index_exports);
|
|
20
|
+
__reExport(index_exports, require("./useTabs.native.js"), module.exports);
|
|
21
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +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":[]}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf,
|
|
6
|
+
__hasOwnProp = Object.prototype.hasOwnProperty;
|
|
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, {
|
|
15
|
+
get: () => from[key],
|
|
16
|
+
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", {
|
|
26
|
+
value: mod,
|
|
27
|
+
enumerable: !0
|
|
28
|
+
}) : target, mod)),
|
|
29
|
+
__toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
30
|
+
value: !0
|
|
31
|
+
}), mod);
|
|
32
|
+
var useTabs_exports = {};
|
|
33
|
+
__export(useTabs_exports, {
|
|
34
|
+
TabsProvider: () => TabsProvider,
|
|
35
|
+
useTab: () => useTab,
|
|
36
|
+
useTabContent: () => useTabContent,
|
|
37
|
+
useTabs: () => useTabs,
|
|
38
|
+
useTabsContext: () => useTabsContext
|
|
39
|
+
});
|
|
40
|
+
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");
|
|
46
|
+
function useTabs(props = {}) {
|
|
47
|
+
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;
|
|
105
|
+
}
|
|
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);
|
|
129
|
+
}
|
|
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]);
|
|
145
|
+
return {
|
|
146
|
+
value,
|
|
147
|
+
setValue,
|
|
148
|
+
direction,
|
|
149
|
+
tabsProps: {
|
|
150
|
+
"data-orientation": orientation,
|
|
151
|
+
dir: direction
|
|
152
|
+
},
|
|
153
|
+
listProps: {
|
|
154
|
+
role: "tablist",
|
|
155
|
+
"aria-orientation": orientation
|
|
156
|
+
},
|
|
157
|
+
getTabProps,
|
|
158
|
+
getContentProps,
|
|
159
|
+
contextValue: {
|
|
160
|
+
baseId,
|
|
161
|
+
value,
|
|
162
|
+
setValue,
|
|
163
|
+
orientation,
|
|
164
|
+
direction,
|
|
165
|
+
activationMode,
|
|
166
|
+
loop
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const TabsContext = React.createContext(null),
|
|
171
|
+
TabsProvider = TabsContext.Provider;
|
|
172
|
+
function useTabsContext() {
|
|
173
|
+
const context = React.useContext(TabsContext);
|
|
174
|
+
if (!context) throw new Error("useTabsContext must be used within a TabsProvider");
|
|
175
|
+
return context;
|
|
176
|
+
}
|
|
177
|
+
function useTab(props) {
|
|
178
|
+
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);
|
|
199
|
+
return {
|
|
200
|
+
isSelected,
|
|
201
|
+
tabProps: {
|
|
202
|
+
ref,
|
|
203
|
+
role: "tab",
|
|
204
|
+
id: triggerId,
|
|
205
|
+
"aria-selected": isSelected,
|
|
206
|
+
"aria-controls": contentId,
|
|
207
|
+
"data-state": isSelected ? "active" : "inactive",
|
|
208
|
+
...(disabled && {
|
|
209
|
+
"data-disabled": ""
|
|
210
|
+
}),
|
|
211
|
+
disabled,
|
|
212
|
+
tabIndex: isSelected ? 0 : -1,
|
|
213
|
+
onKeyDown: (0, import_helpers.composeEventHandlers)(onKeyDown, event => {
|
|
214
|
+
disabled || [" ", "Enter"].includes(event.key) && (setValue(tabValue), event.preventDefault());
|
|
215
|
+
}),
|
|
216
|
+
onPress: (0, import_helpers.composeEventHandlers)(onPress, () => {
|
|
217
|
+
!disabled && !isSelected && setValue(tabValue);
|
|
218
|
+
}),
|
|
219
|
+
onFocus: (0, import_helpers.composeEventHandlers)(onFocus, () => {
|
|
220
|
+
!disabled && !isSelected && activationMode === "automatic" && setValue(tabValue);
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function useTabContent(props) {
|
|
226
|
+
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);
|
|
239
|
+
return {
|
|
240
|
+
isSelected,
|
|
241
|
+
shouldMount: forceMount || isSelected,
|
|
242
|
+
contentProps: {
|
|
243
|
+
role: "tabpanel",
|
|
244
|
+
id: contentId,
|
|
245
|
+
"aria-labelledby": triggerId,
|
|
246
|
+
"data-state": isSelected ? "active" : "inactive",
|
|
247
|
+
"data-orientation": orientation,
|
|
248
|
+
hidden: !isSelected,
|
|
249
|
+
tabIndex: 0
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function makeTriggerId(baseId, value) {
|
|
254
|
+
return `${baseId}-trigger-${value}`;
|
|
255
|
+
}
|
|
256
|
+
function makeContentId(baseId, value) {
|
|
257
|
+
return `${baseId}-content-${value}`;
|
|
258
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
9
|
+
}, __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
return to;
|
|
14
|
+
};
|
|
15
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
16
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
17
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
18
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
19
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
|
|
21
|
+
mod
|
|
22
|
+
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
23
|
+
var useTabs_exports = {};
|
|
24
|
+
__export(useTabs_exports, {
|
|
25
|
+
TabsProvider: () => TabsProvider,
|
|
26
|
+
useTab: () => useTab,
|
|
27
|
+
useTabContent: () => useTabContent,
|
|
28
|
+
useTabs: () => useTabs,
|
|
29
|
+
useTabsContext: () => useTabsContext
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(useTabs_exports);
|
|
32
|
+
var import_helpers = require("@tamagui/helpers"), import_use_controllable_state = require("@tamagui/use-controllable-state"), import_use_direction = require("@tamagui/use-direction"), React = __toESM(require("react"), 1), import_react = require("react");
|
|
33
|
+
function useTabs(props = {}) {
|
|
34
|
+
const {
|
|
35
|
+
value: valueProp,
|
|
36
|
+
onValueChange,
|
|
37
|
+
defaultValue = "",
|
|
38
|
+
orientation = "horizontal",
|
|
39
|
+
dir,
|
|
40
|
+
activationMode = "automatic",
|
|
41
|
+
loop = !0
|
|
42
|
+
} = props, direction = (0, import_use_direction.useDirection)(dir), baseId = (0, import_react.useId)(), [value, setValue] = (0, import_use_controllable_state.useControllableState)({
|
|
43
|
+
prop: valueProp,
|
|
44
|
+
onChange: onValueChange,
|
|
45
|
+
defaultProp: defaultValue
|
|
46
|
+
}), tabRefs = (0, import_react.useRef)(/* @__PURE__ */ new Map()), [tabValues, setTabValues] = (0, import_react.useState)([]), registerTab = (0, import_react.useCallback)((tabValue, element) => {
|
|
47
|
+
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)));
|
|
48
|
+
}, []), focusTab = (0, import_react.useCallback)((tabValue) => {
|
|
49
|
+
tabRefs.current.get(tabValue)?.focus();
|
|
50
|
+
}, []), getNextTab = (0, import_react.useCallback)(
|
|
51
|
+
(currentValue, direction2) => {
|
|
52
|
+
const currentIndex = tabValues.indexOf(currentValue);
|
|
53
|
+
if (currentIndex === -1) return null;
|
|
54
|
+
let nextIndex = currentIndex + direction2;
|
|
55
|
+
if (loop)
|
|
56
|
+
nextIndex = (nextIndex + tabValues.length) % tabValues.length;
|
|
57
|
+
else if (nextIndex < 0 || nextIndex >= tabValues.length) return null;
|
|
58
|
+
return tabValues[nextIndex] ?? null;
|
|
59
|
+
},
|
|
60
|
+
[tabValues, loop]
|
|
61
|
+
), handleKeyDown = (0, import_react.useCallback)(
|
|
62
|
+
(tabValue) => (event) => {
|
|
63
|
+
const isHorizontal = orientation === "horizontal", isRtl = direction === "rtl";
|
|
64
|
+
let nextTab = null;
|
|
65
|
+
switch (event.key) {
|
|
66
|
+
case "ArrowRight":
|
|
67
|
+
isHorizontal && (nextTab = getNextTab(tabValue, isRtl ? -1 : 1));
|
|
68
|
+
break;
|
|
69
|
+
case "ArrowLeft":
|
|
70
|
+
isHorizontal && (nextTab = getNextTab(tabValue, isRtl ? 1 : -1));
|
|
71
|
+
break;
|
|
72
|
+
case "ArrowDown":
|
|
73
|
+
isHorizontal || (nextTab = getNextTab(tabValue, 1));
|
|
74
|
+
break;
|
|
75
|
+
case "ArrowUp":
|
|
76
|
+
isHorizontal || (nextTab = getNextTab(tabValue, -1));
|
|
77
|
+
break;
|
|
78
|
+
case "Home":
|
|
79
|
+
nextTab = tabValues[0] ?? null;
|
|
80
|
+
break;
|
|
81
|
+
case "End":
|
|
82
|
+
nextTab = tabValues[tabValues.length - 1] ?? null;
|
|
83
|
+
break;
|
|
84
|
+
case " ":
|
|
85
|
+
case "Enter":
|
|
86
|
+
activationMode === "manual" && (setValue(tabValue), event.preventDefault());
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
nextTab && (event.preventDefault(), focusTab(nextTab), activationMode === "automatic" && setValue(nextTab));
|
|
90
|
+
},
|
|
91
|
+
[orientation, direction, getNextTab, tabValues, activationMode, setValue, focusTab]
|
|
92
|
+
), getTabProps = (0, import_react.useCallback)(
|
|
93
|
+
(tabValue, disabled) => {
|
|
94
|
+
const isSelected = value === tabValue, triggerId = makeTriggerId(baseId, tabValue), contentId = makeContentId(baseId, tabValue);
|
|
95
|
+
return {
|
|
96
|
+
role: "tab",
|
|
97
|
+
id: triggerId,
|
|
98
|
+
"aria-selected": isSelected,
|
|
99
|
+
"aria-controls": contentId,
|
|
100
|
+
"data-state": isSelected ? "active" : "inactive",
|
|
101
|
+
...disabled && { "data-disabled": "" },
|
|
102
|
+
disabled,
|
|
103
|
+
tabIndex: isSelected ? 0 : -1,
|
|
104
|
+
onKeyDown: disabled ? () => {
|
|
105
|
+
} : handleKeyDown(tabValue),
|
|
106
|
+
onClick: (event) => {
|
|
107
|
+
!disabled && !isSelected && setValue(tabValue);
|
|
108
|
+
},
|
|
109
|
+
onFocus: (event) => {
|
|
110
|
+
registerTab(tabValue, event.currentTarget), !disabled && !isSelected && activationMode === "automatic" && setValue(tabValue);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
[value, baseId, handleKeyDown, setValue, activationMode, registerTab]
|
|
115
|
+
), getContentProps = (0, import_react.useCallback)(
|
|
116
|
+
(tabValue) => {
|
|
117
|
+
const isSelected = value === tabValue, triggerId = makeTriggerId(baseId, tabValue);
|
|
118
|
+
return {
|
|
119
|
+
role: "tabpanel",
|
|
120
|
+
id: makeContentId(baseId, tabValue),
|
|
121
|
+
"aria-labelledby": triggerId,
|
|
122
|
+
"data-state": isSelected ? "active" : "inactive",
|
|
123
|
+
"data-orientation": orientation,
|
|
124
|
+
hidden: !isSelected,
|
|
125
|
+
tabIndex: 0
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
[value, baseId, orientation]
|
|
129
|
+
);
|
|
130
|
+
return {
|
|
131
|
+
value,
|
|
132
|
+
setValue,
|
|
133
|
+
direction,
|
|
134
|
+
tabsProps: {
|
|
135
|
+
"data-orientation": orientation,
|
|
136
|
+
dir: direction
|
|
137
|
+
},
|
|
138
|
+
listProps: {
|
|
139
|
+
role: "tablist",
|
|
140
|
+
"aria-orientation": orientation
|
|
141
|
+
},
|
|
142
|
+
getTabProps,
|
|
143
|
+
getContentProps,
|
|
144
|
+
contextValue: {
|
|
145
|
+
baseId,
|
|
146
|
+
value,
|
|
147
|
+
setValue,
|
|
148
|
+
orientation,
|
|
149
|
+
direction,
|
|
150
|
+
activationMode,
|
|
151
|
+
loop
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const TabsContext = React.createContext(null), TabsProvider = TabsContext.Provider;
|
|
156
|
+
function useTabsContext() {
|
|
157
|
+
const context = React.useContext(TabsContext);
|
|
158
|
+
if (!context)
|
|
159
|
+
throw new Error("useTabsContext must be used within a TabsProvider");
|
|
160
|
+
return context;
|
|
161
|
+
}
|
|
162
|
+
function useTab(props) {
|
|
163
|
+
const { value: tabValue, disabled, onPress, onKeyDown, onFocus } = props, context = useTabsContext(), { value, setValue, baseId, activationMode, orientation, direction, loop } = context, isSelected = value === tabValue, triggerId = makeTriggerId(baseId, tabValue), contentId = makeContentId(baseId, tabValue), ref = (0, import_react.useRef)(null);
|
|
164
|
+
return {
|
|
165
|
+
isSelected,
|
|
166
|
+
tabProps: {
|
|
167
|
+
ref,
|
|
168
|
+
role: "tab",
|
|
169
|
+
id: triggerId,
|
|
170
|
+
"aria-selected": isSelected,
|
|
171
|
+
"aria-controls": contentId,
|
|
172
|
+
"data-state": isSelected ? "active" : "inactive",
|
|
173
|
+
...disabled && { "data-disabled": "" },
|
|
174
|
+
disabled,
|
|
175
|
+
tabIndex: isSelected ? 0 : -1,
|
|
176
|
+
onKeyDown: (0, import_helpers.composeEventHandlers)(onKeyDown, (event) => {
|
|
177
|
+
disabled || [" ", "Enter"].includes(event.key) && (setValue(tabValue), event.preventDefault());
|
|
178
|
+
}),
|
|
179
|
+
onPress: (0, import_helpers.composeEventHandlers)(onPress, () => {
|
|
180
|
+
!disabled && !isSelected && setValue(tabValue);
|
|
181
|
+
}),
|
|
182
|
+
onFocus: (0, import_helpers.composeEventHandlers)(onFocus, () => {
|
|
183
|
+
!disabled && !isSelected && activationMode === "automatic" && setValue(tabValue);
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function useTabContent(props) {
|
|
189
|
+
const { value: tabValue, forceMount } = props, context = useTabsContext(), { value, baseId, orientation } = context, isSelected = value === tabValue, triggerId = makeTriggerId(baseId, tabValue), contentId = makeContentId(baseId, tabValue);
|
|
190
|
+
return {
|
|
191
|
+
isSelected,
|
|
192
|
+
shouldMount: forceMount || isSelected,
|
|
193
|
+
contentProps: {
|
|
194
|
+
role: "tabpanel",
|
|
195
|
+
id: contentId,
|
|
196
|
+
"aria-labelledby": triggerId,
|
|
197
|
+
"data-state": isSelected ? "active" : "inactive",
|
|
198
|
+
"data-orientation": orientation,
|
|
199
|
+
hidden: !isSelected,
|
|
200
|
+
tabIndex: 0
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function makeTriggerId(baseId, value) {
|
|
205
|
+
return `${baseId}-trigger-${value}`;
|
|
206
|
+
}
|
|
207
|
+
function makeContentId(baseId, value) {
|
|
208
|
+
return `${baseId}-content-${value}`;
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=useTabs.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/useTabs.tsx"],
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAqC,6BACrC,gCAAqC,4CACrC,uBAA6B,mCAC7B,QAAuB,8BACvB,eAAqD;AAkG9C,SAAS,QAAQ,QAAsB,CAAC,GAAkB;AAC/D,QAAM;AAAA,IACJ,OAAO;AAAA,IACP;AAAA,IACA,eAAe;AAAA,IACf,cAAc;AAAA,IACd;AAAA,IACA,iBAAiB;AAAA,IACjB,OAAO;AAAA,EACT,IAAI,OAEE,gBAAY,mCAAa,GAAG,GAC5B,aAAS,oBAAM,GAEf,CAAC,OAAO,QAAQ,QAAI,oDAAqB;AAAA,IAC7C,MAAM;AAAA,IACN,UAAU;AAAA,IACV,aAAa;AAAA,EACf,CAAC,GAEK,cAAU,qBAAiC,oBAAI,IAAI,CAAC,GACpD,CAAC,WAAW,YAAY,QAAI,uBAAmB,CAAC,CAAC,GAEjD,kBAAc,0BAAY,CAAC,UAAkB,YAAgC;AACjF,IAAI,WACF,QAAQ,QAAQ,IAAI,UAAU,OAAO,GACrC,aAAa,CAAC,SAAU,KAAK,S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