@postenbring/hedwig-react
Version:
React components for [Hedwig Design System](https://github.com/bring/hedwig-design-system).
284 lines (276 loc) • 10.9 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/tabs/index.tsx
var tabs_exports = {};
__export(tabs_exports, {
Tabs: () => Tabs,
TabsContent: () => TabsContent,
TabsContents: () => TabsContents,
TabsList: () => TabsList,
TabsTab: () => TabsTab
});
module.exports = __toCommonJS(tabs_exports);
// src/tabs/tabs.tsx
var import_react5 = require("react");
var import_typed_classname3 = require("@postenbring/hedwig-css/typed-classname");
var import_react_slot2 = require("@radix-ui/react-slot");
// src/tabs/context.ts
var import_react = require("react");
var TabsContext = (0, import_react.createContext)(null);
function useTabsContext() {
const context = (0, import_react.useContext)(TabsContext);
if (!context) {
throw new Error(
"Tabs context required. Did you use `<Tabs.List />`, `<Tabs.Tab />`, or `<Tabs.Content />` outside of <Tabs/>?"
);
}
return context;
}
// src/tabs/tabs-content.tsx
var import_react2 = require("react");
var import_typed_classname = require("@postenbring/hedwig-css/typed-classname");
var import_react_slot = require("@radix-ui/react-slot");
var import_jsx_runtime = require("react/jsx-runtime");
var TabsContents = (0, import_react2.forwardRef)(
(_a, ref) => {
var _b = _a, { asChild, children } = _b, rest = __objRest(_b, ["asChild", "children"]);
const Component = asChild ? import_react_slot.Slot : "div";
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, __spreadProps(__spreadValues({ ref, className: (0, import_typed_classname.clsx)("hds-tabs__contents") }, rest), { children }));
}
);
TabsContents.displayName = "Tabs.Contents";
var TabsContent = (0, import_react2.forwardRef)(
(_a, ref) => {
var _b = _a, { asChild, forTabId, children } = _b, rest = __objRest(_b, ["asChild", "forTabId", "children"]);
const context = useTabsContext();
const Component = asChild ? import_react_slot.Slot : "div";
if (context.activeTabId === forTabId) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, __spreadProps(__spreadValues({}, rest), { ref, children }));
}
return null;
}
);
TabsContent.displayName = "Tabs.Content";
// src/tabs/tabs-list.tsx
var import_react4 = require("react");
var import_typed_classname2 = require("@postenbring/hedwig-css/typed-classname");
// src/utils/utils.ts
var React = __toESM(require("react"));
var import_react3 = require("react");
function useMergeRefs(refs) {
return React.useMemo(() => {
if (refs.every((ref) => ref === null)) {
return null;
}
return (value) => {
refs.forEach((ref) => {
if (typeof ref === "function") {
ref(value);
} else if (ref !== null) {
ref.current = value;
}
});
};
}, refs);
}
function useResize(ref) {
const [width, setWidth] = (0, import_react3.useState)(0);
const [height, setHeight] = (0, import_react3.useState)(0);
const handleResize = (0, import_react3.useCallback)(() => {
var _a, _b, _c, _d;
if ((ref == null ? void 0 : ref.current) !== null) {
setWidth((_b = (_a = ref == null ? void 0 : ref.current) == null ? void 0 : _a.offsetWidth) != null ? _b : 0);
setHeight((_d = (_c = ref == null ? void 0 : ref.current) == null ? void 0 : _c.offsetHeight) != null ? _d : 0);
}
}, [ref]);
(0, import_react3.useEffect)(() => {
window.addEventListener("load", handleResize);
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("load", handleResize);
window.removeEventListener("resize", handleResize);
};
}, [ref, handleResize]);
(0, import_react3.useEffect)(() => {
handleResize();
}, []);
return { width, height };
}
function subscribe() {
return () => {
};
}
function useHydrated() {
return React.useSyncExternalStore(
subscribe,
() => true,
() => false
);
}
// src/tabs/tabs-list.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var TabsList = (0, import_react4.forwardRef)(
(_a, ref) => {
var _b = _a, { children, direction = "horizontal", className } = _b, rest = __objRest(_b, ["children", "direction", "className"]);
const { activeTabId } = useTabsContext();
const tabsListRef = (0, import_react4.useRef)(null);
const mergedRef = useMergeRefs([tabsListRef, ref]);
const { width: tabsWidth } = useResize(tabsListRef);
const isClientSide = useHydrated();
const { innerWidth } = isClientSide ? window : { innerWidth: 1e3 };
const wideEnough = innerWidth >= tabsWidth;
const previousTabId = (0, import_react4.useRef)(activeTabId);
(0, import_react4.useEffect)(() => {
const tabList = tabsListRef.current;
const activeTab = tabList == null ? void 0 : tabList.querySelector(`[data-tabid="${activeTabId}"]`);
if (!activeTab || !tabList) return;
const { offsetHeight: containerHeight, offsetWidth: containerWidth } = tabList;
const { offsetHeight, offsetWidth, offsetTop, offsetLeft } = activeTab;
const height = offsetHeight / containerHeight;
const width = offsetWidth / containerWidth;
tabsListRef.current.style.setProperty("--_hds-tabs-marker-height", String(height));
tabsListRef.current.style.setProperty("--_hds-tabs-marker-width", String(width));
tabsListRef.current.style.setProperty("--_hds-tabs-marker-top", `${offsetTop}px`);
tabsListRef.current.style.setProperty("--_hds-tabs-marker-left", `${offsetLeft}px`);
if (previousTabId.current !== activeTabId) {
tabsListRef.current.style.setProperty(
"--_hds-tabs-marker-animated-color",
"var(--_hds-tabs-marker-color)"
);
tabsListRef.current.style.setProperty(
"--_hds-tabs-marker-border-fallback-color",
"transparent"
);
}
previousTabId.current = activeTabId;
}, [activeTabId, innerWidth]);
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
"div",
__spreadProps(__spreadValues({
className: (0, import_typed_classname2.clsx)(
"hds-tabs__list",
direction === "horizontal" ? {
"hds-tabs__list--horizontal": wideEnough,
"hds-tabs__list--vertical": !wideEnough
} : {
"hds-tabs__list--vertical": true
},
className
),
ref: mergedRef,
role: "tablist"
}, rest), {
children
})
);
}
);
TabsList.displayName = "Tabs.List";
var TabsTab = (0, import_react4.forwardRef)(
(_a, ref) => {
var _b = _a, { children, tabId, className, onClick } = _b, rest = __objRest(_b, ["children", "tabId", "className", "onClick"]);
const context = useTabsContext();
const toggleTab = (e) => {
e.preventDefault();
context.toggleActiveTabId(tabId);
onClick == null ? void 0 : onClick(e);
};
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
"button",
__spreadProps(__spreadValues({
className: (0, import_typed_classname2.clsx)(
"hds-tabs__tab",
{ "hds-tabs__tab--active": context.activeTabId === tabId },
className
),
"data-tabid": tabId,
onClick: toggleTab,
ref,
type: "button",
role: "tab"
}, rest), {
children
})
);
}
);
TabsTab.displayName = "Tabs.Tab";
// src/tabs/tabs.tsx
var import_jsx_runtime3 = require("react/jsx-runtime");
var Tabs = (0, import_react5.forwardRef)(
(_a, ref) => {
var _b = _a, { asChild, defaultTab, children } = _b, rest = __objRest(_b, ["asChild", "defaultTab", "children"]);
const [activeTabId, setActiveTabId] = (0, import_react5.useState)(defaultTab);
const Component = asChild ? import_react_slot2.Slot : "div";
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Component, __spreadProps(__spreadValues({ className: (0, import_typed_classname3.clsx)("hds-tabs"), ref }, rest), { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(TabsContext.Provider, { value: { activeTabId, toggleActiveTabId: setActiveTabId }, children }) }));
}
);
Tabs.displayName = "Tabs";
Tabs.List = TabsList;
Tabs.Tab = TabsTab;
Tabs.Contents = TabsContents;
Tabs.Content = TabsContent;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Tabs,
TabsContent,
TabsContents,
TabsList,
TabsTab
});
//# sourceMappingURL=index.js.map