@yamada-ui/tabs
Version:
Yamada UI tabs component
374 lines (366 loc) • 12.9 kB
JavaScript
"use client"
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/tabs.tsx
var tabs_exports = {};
__export(tabs_exports, {
Tabs: () => Tabs
});
module.exports = __toCommonJS(tabs_exports);
var import_core5 = require("@yamada-ui/core");
var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
var import_utils6 = require("@yamada-ui/utils");
var import_react5 = require("react");
// src/tab.tsx
var import_core = require("@yamada-ui/core");
var import_ripple = require("@yamada-ui/ripple");
var import_use_clickable = require("@yamada-ui/use-clickable");
var import_utils2 = require("@yamada-ui/utils");
var import_react = require("react");
// src/tabs-context.tsx
var import_use_descendant = require("@yamada-ui/use-descendant");
var import_utils = require("@yamada-ui/utils");
var {
DescendantsContextProvider: TabDescendantsContextProvider,
useDescendant: useTabDescendant,
useDescendants: useTabDescendants,
useDescendantsContext: useTabDescendantsContext
} = (0, import_use_descendant.createDescendant)();
var {
DescendantsContextProvider: TabPanelDescendantsContextProvider,
useDescendant: useTabPanelDescendant,
useDescendants: useTabPanelDescendants,
useDescendantsContext: useTabPanelDescendantsContext
} = (0, import_use_descendant.createDescendant)();
var [TabsProvider, useTabsContext] = (0, import_utils.createContext)({
name: "TabsContext",
errorMessage: `useTabsContext returned is 'undefined'. Seems you forgot to wrap the components in "<Tabs />"`
});
var [TabPanelProvider, useTabPanelContext] = (0, import_utils.createContext)({});
// src/tab.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var Tab = (0, import_core.forwardRef)(
({
id,
className,
children,
clickOnEnter,
clickOnSpace,
isDisabled,
disabled = isDisabled,
isFocusable,
focusable = isFocusable,
...rest
}, ref) => {
var _a;
const uuid = (0, import_react.useId)();
const {
disableRipple,
manual,
orientation,
selectedIndex,
setFocusedIndex,
setSelectedIndex,
styles
} = useTabsContext();
const { index, register } = useTabDescendant({
disabled: disabled && !focusable
});
const { descendants } = useTabPanelDescendant();
const tabpanelId = (_a = descendants.value(index)) == null ? void 0 : _a.node.id;
const isSelected = index === selectedIndex;
const css = {
alignItems: "center",
display: "flex",
justifyContent: "center",
outline: "0",
overflow: "hidden",
position: "relative",
...styles.tab
};
id != null ? id : id = uuid;
const onFocus = () => {
setFocusedIndex(index);
if (!manual && !(disabled && focusable)) setSelectedIndex(index);
};
const clickableProps = (0, import_use_clickable.useClickable)({
id,
"aria-controls": tabpanelId,
"aria-selected": isSelected,
role: "tab",
...rest,
ref: (0, import_utils2.mergeRefs)(register, ref),
clickOnEnter,
clickOnSpace,
disabled,
focusable,
focusOnClick: false,
onClick: (0, import_utils2.handlerAll)(rest.onClick, () => setSelectedIndex(index)),
onFocus: disabled ? void 0 : (0, import_utils2.handlerAll)(rest.onFocus, onFocus)
});
const { onPointerDown, ...rippleProps } = (0, import_ripple.useRipple)({
...clickableProps,
disabled: disableRipple || disabled
});
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
import_core.ui.button,
{
className: (0, import_utils2.cx)("ui-tabs__tab", className),
__css: css,
...clickableProps,
type: "button",
"data-orientation": orientation,
tabIndex: isSelected ? 0 : -1,
onPointerDown,
children: [
children,
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ripple.Ripple, { ...rippleProps })
]
}
);
}
);
Tab.displayName = "Tab";
Tab.__ui__ = "Tab";
// src/tab-list.tsx
var import_core2 = require("@yamada-ui/core");
var import_utils3 = require("@yamada-ui/utils");
var import_react2 = require("react");
var import_jsx_runtime2 = require("react/jsx-runtime");
var TabList = (0, import_core2.forwardRef)(
({ className, ...rest }, ref) => {
const { focusedIndex, orientation, styles, tabListProps } = useTabsContext();
const descendants = useTabDescendantsContext();
const isVertical = orientation === "vertical";
const onNext = (0, import_react2.useCallback)(() => {
const next = descendants.enabledNextValue(focusedIndex);
if (next) next.node.focus();
}, [descendants, focusedIndex]);
const onPrev = (0, import_react2.useCallback)(() => {
const prev = descendants.enabledPrevValue(focusedIndex);
if (prev) prev.node.focus();
}, [descendants, focusedIndex]);
const onFirst = (0, import_react2.useCallback)(() => {
const first = descendants.enabledFirstValue();
if (first) first.node.focus();
}, [descendants]);
const onLast = (0, import_react2.useCallback)(() => {
const last = descendants.enabledLastValue();
if (last) last.node.focus();
}, [descendants]);
const onKeyDown = (0, import_react2.useCallback)(
(ev) => {
const actions = {
ArrowDown: () => isVertical ? onNext() : {},
ArrowLeft: () => !isVertical ? onPrev() : {},
ArrowRight: () => !isVertical ? onNext() : {},
ArrowUp: () => isVertical ? onPrev() : {},
End: onLast,
Home: onFirst
};
const action = actions[ev.key];
if (!action) return;
ev.preventDefault();
action(ev);
},
[onFirst, onLast, isVertical, onPrev, onNext]
);
const css = { display: "flex", ...styles.tabList };
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
import_core2.ui.div,
{
ref,
className: (0, import_utils3.cx)("ui-tabs__list", className),
"aria-orientation": orientation,
role: "tablist",
__css: css,
...tabListProps,
...rest,
onKeyDown: (0, import_utils3.handlerAll)(rest.onKeyDown, onKeyDown)
}
);
}
);
TabList.displayName = "TabList";
TabList.__ui__ = "TabList";
// src/tab-panel.tsx
var import_core3 = require("@yamada-ui/core");
var import_use_disclosure = require("@yamada-ui/use-disclosure");
var import_utils4 = require("@yamada-ui/utils");
var import_react3 = require("react");
var import_jsx_runtime3 = require("react/jsx-runtime");
var TabPanel = (0, import_core3.forwardRef)(
({ id, className, children, ...rest }, ref) => {
var _a;
const uuid = (0, import_react3.useId)();
const { lazy: enabled, lazyBehavior: mode, styles } = useTabsContext();
const { index, selected } = useTabPanelContext();
const { register } = useTabPanelDescendant();
const { descendants } = useTabDescendant();
const hasBeenSelected = (0, import_react3.useRef)(false);
const tabId = (_a = descendants.value(index)) == null ? void 0 : _a.node.id;
if (selected) hasBeenSelected.current = true;
const shouldRenderChildren = (0, import_use_disclosure.useLazyDisclosure)({
enabled,
isSelected: selected,
mode,
wasSelected: hasBeenSelected.current
});
const css = { ...styles.tabPanel };
id != null ? id : id = uuid;
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
import_core3.ui.div,
{
id,
ref: (0, import_utils4.mergeRefs)(register, ref),
className: (0, import_utils4.cx)("ui-tabs__panel", className),
"aria-labelledby": tabId,
role: "tabpanel",
__css: css,
...rest,
hidden: !selected,
children: shouldRenderChildren ? children : null
}
);
}
);
TabPanel.displayName = "TabPanel";
TabPanel.__ui__ = "TabPanel";
// src/tab-panels.tsx
var import_core4 = require("@yamada-ui/core");
var import_utils5 = require("@yamada-ui/utils");
var import_react4 = require("react");
var import_jsx_runtime4 = require("react/jsx-runtime");
var TabPanels = (0, import_core4.forwardRef)(
({ className, children, ...rest }, ref) => {
const { selectedIndex, styles, tabPanelsProps } = useTabsContext();
const validChildren = (0, import_utils5.getValidChildren)(children);
const cloneChildren = validChildren.map((child, index) => {
const selected = index === selectedIndex;
return (0, import_react4.createElement)(
TabPanelProvider,
{ key: index, value: { index, selected, selectedIndex } },
child
);
});
const css = {
w: "100%",
...styles.tabPanels
};
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_core4.ui.div,
{
ref,
className: (0, import_utils5.cx)("ui-tabs__panels", className),
__css: css,
...tabPanelsProps,
...rest,
children: cloneChildren
}
);
}
);
TabPanels.displayName = "TabPanels";
TabPanels.__ui__ = "TabPanels";
// src/tabs.tsx
var import_jsx_runtime5 = require("react/jsx-runtime");
var Tabs = (0, import_core5.forwardRef)(
({ align = "start", isFitted, fitted = isFitted, ...props }, ref) => {
const [styles, mergedProps] = (0, import_core5.useComponentMultiStyle)("Tabs", {
align,
fitted,
...props
});
const {
className,
children,
defaultIndex = 0,
disableRipple = false,
index,
isLazy = true,
isManual,
lazy = isLazy,
lazyBehavior = "keepMounted",
manual = isManual,
orientation = "horizontal",
tabListProps,
tabPanelsProps,
onChange,
...rest
} = (0, import_core5.omitThemeProps)(mergedProps);
const [focusedIndex, setFocusedIndex] = (0, import_react5.useState)(defaultIndex);
const [selectedIndex, setSelectedIndex] = (0, import_use_controllable_state.useControllableState)({
defaultValue: defaultIndex,
value: index,
onChange
});
const tabDescendants = useTabDescendants();
const tabPanelDescendants = useTabPanelDescendants();
const validChildren = (0, import_utils6.getValidChildren)(children);
const customTabList = (0, import_utils6.findChild)(validChildren, TabList);
const customTabPanels = (0, import_utils6.findChild)(validChildren, TabPanels);
const cloneTabs = (0, import_utils6.pickChildren)(validChildren, Tab);
const cloneTabPanels = (0, import_utils6.pickChildren)(validChildren, TabPanel);
const css = { w: "100%", ...styles.container };
(0, import_react5.useEffect)(() => {
if (index != null) setFocusedIndex(index);
}, [index]);
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(TabDescendantsContextProvider, { value: tabDescendants, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(TabPanelDescendantsContextProvider, { value: tabPanelDescendants, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
TabsProvider,
{
value: {
align,
disableRipple,
fitted,
focusedIndex,
lazy,
lazyBehavior,
manual,
orientation,
selectedIndex,
setFocusedIndex,
setSelectedIndex,
styles,
tabListProps,
tabPanelsProps
},
children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
import_core5.ui.div,
{
ref,
className: (0, import_utils6.cx)("ui-tabs", className),
__css: css,
...rest,
children: [
customTabList != null ? customTabList : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(TabList, { children: cloneTabs }),
customTabPanels != null ? customTabPanels : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(TabPanels, { children: cloneTabPanels })
]
}
)
}
) }) });
}
);
Tabs.displayName = "Tabs";
Tabs.__ui__ = "Tabs";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Tabs
});
//# sourceMappingURL=tabs.js.map