UNPKG

@crossed/ui

Version:

A universal & performant styling library for React Native, Next.js & React

166 lines (165 loc) 5.64 kB
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); var Tab_exports = {}; __export(Tab_exports, { createTab: () => createTab }); module.exports = __toCommonJS(Tab_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_core = require("@crossed/core"); var import_react = require("react"); var import_styled = require("@crossed/styled"); var import_react_native_reanimated = require("react-native-reanimated"); var import_styles = require("./styles"); var import_Button = require("../../buttons/Button"); var import_useMedia = require("../../useMedia"); const createTab = ({ useTriggerContext, useTabsContext, TriggerProvider }) => { const TabImpl = ({ value: valueProps, children, disabled, style, ...props }) => { const { setValue, value, id, variant, listTabRef, indicator, scroll, shouldShow, widthLayout, size } = useTabsContext(); const media = (0, import_useMedia.useMedia)(); const selected = valueProps === value; const { state, props: interaction } = (0, import_styled.useInteraction)(props); const measure = () => { var _a; if (listTabRef.current) { (_a = ref.current) == null ? void 0 : _a.measureLayout( listTabRef.current, (left, _top, width) => { var _a2, _b; const offset = shouldShow ? 30 : 0; const positionLeft = import_styled.isWeb ? left + scroll.value : left; indicator.left.value = (0, import_react_native_reanimated.withTiming)(positionLeft); indicator.width.value = (0, import_react_native_reanimated.withTiming)(width); if (widthLayout.value + scroll.value < positionLeft + width + offset) { (_a2 = listTabRef.current) == null ? void 0 : _a2.scrollTo({ x: positionLeft + width - widthLayout.value + offset }); } else if (positionLeft <= offset || positionLeft < scroll.value) { (_b = listTabRef.current) == null ? void 0 : _b.scrollTo({ x: positionLeft - offset }); } } ); } }; const onPress = (0, import_react.useCallback)( (0, import_core.composeEventHandlers)(() => { setValue(valueProps); measure(); }, props.onPress), [props.onPress, setValue] ); (0, import_react.useEffect)(() => { if (shouldShow && selected) { measure(); } }, [shouldShow]); (0, import_react.useEffect)(() => { selected && measure(); }, [...Object.values(media), selected]); const onLayout = (0, import_react.useCallback)( (0, import_core.composeEventHandlers)(({ nativeEvent: { layout } }) => { if (selected) { indicator.left.value = layout.x + scroll.value; indicator.width.value = layout.width; } }, props.onLayout), [props.onLayout, setValue, selected, shouldShow] ); const ref = (0, import_react.useRef)(); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( TriggerProvider, { ...state, disabled, selected, hover: selected || state.hover, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_Button.Button, { role: "tab", ref, disabled, variant: "tertiary", "aria-selected": selected, "aria-control": `${id}-panel-${valueProps}`, id: `${id}-tab-${valueProps}`, ...props, style: (0, import_styled.composeStyles)( import_styles.triggerStyles.trigger, import_styles.triggerStyles[size], import_styles.focusStyles[variant], disabled && import_styles.triggerStyles.disabled, style ), ...interaction, onPress, onLayout, children: (e) => typeof children === "function" ? children(e) : children } ) } ); }; const Text = ({ style, ...props }) => { const { size } = useTabsContext(); const state = useTriggerContext(); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_Button.Button.Text, { style: (0, import_styled.composeStyles)( import_styles.tabTitleStyles.default, state.selected && import_styles.tabTitleStyles.active, !state.selected && !state.disabled && state.hover && import_styles.tabTitleStyles.hover, style ), size: size === "sm" ? "md" : "default", ...state, ...props } ); }; return (0, import_core.withStaticProperties)(TabImpl, { Text }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createTab }); //# sourceMappingURL=Tab.js.map