UNPKG

@onesy/ui-react

Version:
413 lines (405 loc) 16.7 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["ref", "tonal", "color", "version", "value", "valueDefault", "onChange", "isActive", "activateOnFocus", "align", "justify", "orientation", "size", "initialLineUpdateTimeout", "arrows", "arrowsMobile", "fixed", "noDivider", "IconStart", "IconEnd", "IconTop", "IconBottom", "SurfaceProps", "Component", "className", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React from 'react'; import { is, wait } from '@onesy/utils'; import { style as styleMethod, classNames, useOnesyTheme } from '@onesy/style-react'; import IconMaterialExpandLess from '@onesy/icons-material-rounded-react/IconMaterialExpandLessW100'; import IconMaterialExpandMore from '@onesy/icons-material-rounded-react/IconMaterialExpandMoreW100'; import IconMaterialNavigateNext from '@onesy/icons-material-rounded-react/IconMaterialNavigateNextW100'; import IconMaterialNavigateBefore from '@onesy/icons-material-rounded-react/IconMaterialNavigateBeforeW100'; import SurfaceElement from '../Surface'; import LineElement from '../Line'; import IconButtonElement from '../IconButton'; import DividerElement from '../Divider'; import useMediaQuery from '../useMediaQuery'; import { staticClassName } from '../utils'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const useStyle = styleMethod(theme => ({ root: { position: 'relative', overflow: 'hidden', flex: '0 0 auto' }, size_small: { minHeight: '39px' }, size_regular: { minHeight: '44px' }, size_large: { minHeight: '62px' }, tabs: { position: 'relative' }, orientation_horizontal: { width: '100%' }, tabs_orientation_horizontal: { height: '100%', overflowX: 'auto' }, tabs_orientation_vertical: { width: '100%', overflowY: 'auto' }, arrow: { flex: '0 0 auto', alignSelf: 'center', justifySelf: 'center', transition: theme.methods.transitions.make(['opacity'], { duration: 'xxs' }), '&[disabled]': { opacity: '0' } }, line: { display: 'inline-block', position: 'absolute', background: 'currentColor', transition: theme.methods.transitions.make(['top', 'left', 'width']), zIndex: '1' }, line_version_primary_size_small_orientation_horizontal: { height: '2px', bottom: '0', borderRadius: `${theme.methods.shape.radius.value(40, 'px')} ${theme.methods.shape.radius.value(40, 'px')} 0 0` }, line_version_primary_size_regular_orientation_horizontal: { height: '3px', bottom: '0', borderRadius: `${theme.methods.shape.radius.value(40, 'px')} ${theme.methods.shape.radius.value(40, 'px')} 0 0` }, line_version_primary_size_large_orientation_horizontal: { height: '4px', bottom: '0', borderRadius: `${theme.methods.shape.radius.value(40, 'px')} ${theme.methods.shape.radius.value(40, 'px')} 0 0` }, line_version_primary_size_small_orientation_vertical: { width: '2px', insetInlineEnd: '0', borderRadius: `${theme.methods.shape.radius.value(40, 'px')} 0 0 ${theme.methods.shape.radius.value(40, 'px')}` }, line_version_primary_size_regular_orientation_vertical: { width: '3px', insetInlineEnd: '0', borderRadius: `${theme.methods.shape.radius.value(40, 'px')} 0 0 ${theme.methods.shape.radius.value(40, 'px')}` }, line_version_primary_size_large_orientation_vertical: { width: '4px', insetInlineEnd: '0', borderRadius: `${theme.methods.shape.radius.value(40, 'px')} 0 0 ${theme.methods.shape.radius.value(40, 'px')}` }, line_version_secondary_size_small_orientation_horizontal: { height: '1px', bottom: '0' }, line_version_secondary_size_regular_orientation_horizontal: { height: '2px', bottom: '0' }, line_version_secondary_size_large_orientation_horizontal: { height: '3px', bottom: '0' }, line_version_secondary_size_small_orientation_vertical: { width: '1px', insetInlineEnd: '0' }, line_version_secondary_size_regular_orientation_vertical: { width: '2px', insetInlineEnd: '0' }, line_version_secondary_size_large_orientation_vertical: { width: '3px', insetInlineEnd: '0' }, divider: { position: 'absolute', '&.onesy-Divider-root': { margin: '0px', background: 'currentColor', opacity: '0.14', zIndex: '1' } }, divider_orientation_horizontal: { '&.onesy-Divider-root': { left: '0', right: '0', bottom: '0' } }, divider_orientation_vertical: { '&.onesy-Divider-root': { top: '0', bottom: '0', insetInlineEnd: '0' } }, fixed: { position: 'fixed', top: '0', insetInline: '0' } }), { name: 'onesy-Tabs' }); const Tabs = props_ => { const theme = useOnesyTheme(); const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyTabs?.props?.default), props_); const Line = theme?.elements?.Line || LineElement; const Surface = theme?.elements?.Surface || SurfaceElement; const IconButton = theme?.elements?.IconButton || IconButtonElement; const Divider = theme?.elements?.Divider || DividerElement; const { ref, tonal = true, color = 'primary', version = 'primary', value: value_ = 0, valueDefault, onChange: onChange_, isActive, activateOnFocus, align = 'flex-start', justify = 'flex-start', orientation = 'horizontal', size = 'regular', initialLineUpdateTimeout = 0, arrows, arrowsMobile, fixed, noDivider, IconStart = IconMaterialNavigateBefore, IconEnd = IconMaterialNavigateNext, IconTop = IconMaterialExpandLess, IconBottom = IconMaterialExpandMore, SurfaceProps, Component = 'div', className, children } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const setMoveValue = React.useState({})[1]; const [init, setInit] = React.useState(false); const [lineValues, setLineValues] = React.useState({}); const [value, setValue] = React.useState(valueDefault !== undefined ? valueDefault : value_); const refs = { root: React.useRef(undefined), tabsRoot: React.useRef(undefined), value: React.useRef(undefined), props: React.useRef(undefined), mobile: React.useRef(undefined), version: React.useRef(undefined), isActive: React.useRef(undefined), initialLineUpdateTimeout: React.useRef(undefined) }; const mobile = useMediaQuery('(pointer: coarse)', { element: refs.root.current }); refs.value.current = value; refs.props.current = props; refs.mobile.current = mobile; refs.version.current = version; refs.isActive.current = isActive; refs.initialLineUpdateTimeout.current = initialLineUpdateTimeout; React.useEffect(() => { // Update lineValues value // with the first tab // For elements within a modal // being transitioned, minor bug fix setTimeout(updateLine, refs.initialLineUpdateTimeout.current); const observerMethod = () => updateLine(); // Mutation observer const observerMutation = new ResizeObserver(observerMethod); observerMutation.observe(refs.tabsRoot.current); // Update theme subscription method const method = () => updateLine(); theme.subscriptions.update.subscribe(method); setInit(true); return () => { // Unsubscribe theme.subscriptions.update.unsubscribe(method); observerMutation.disconnect(); }; }, []); const onScroll = event => { const useArrows_ = refs.props.current.arrows && (!refs.mobile.current || refs.props.current.arrowsMobile); if (useArrows_) { setMoveValue({ left: refs.tabsRoot.current.scrollLeft, top: refs.tabsRoot.current.scrollTop }); } }; React.useEffect(() => { if (init) { if (value_ !== refs.value.current) { setValue(value_); // Update lineValues value updateLine(); } } }, [value_]); const onChange = (valueItem, index) => { // Update inner or controlled if (!props.hasOwnProperty('value')) { setValue(valueItem); // Update line updateLine(); } if (is('function', onChange_)) onChange_(valueItem); }; const move = (forward_ = true) => { const forward = theme.direction === 'ltr' || orientation === 'vertical' ? forward_ : !forward_; const rect = refs.tabsRoot.current.getBoundingClientRect(); let moveValue_; if (orientation === 'horizontal') moveValue_ = { left: refs.tabsRoot.current.scrollLeft + (forward ? 1 : -1) * rect.width, behavior: 'smooth' };else moveValue_ = { top: refs.tabsRoot.current.scrollTop + (forward ? 1 : -1) * rect.height, behavior: 'smooth' }; refs.tabsRoot.current.scrollTo(moveValue_); }; const updateLine = async () => { await wait(40); const valueProp = refs.value.current; const tabs = Array.from(refs.tabsRoot.current?.querySelectorAll(`[data-onesy-tab-value]`) || []); const tab = tabs.find(item => is('function', refs.isActive.current) ? refs.isActive.current(valueProp, item.dataset.onesyTabValue) : String(item.dataset.onesyTabValue) === String(valueProp)); if (tab) { const rect_0 = { parent: refs.tabsRoot.current?.getBoundingClientRect(), tab: tab.getBoundingClientRect(), line: (refs.version.current === 'primary' ? tab.children[1] : tab)?.getBoundingClientRect() }; // Update if (rect_0.parent && rect_0.line && rect_0.tab) { // Update left scroll if (orientation === 'horizontal') { if (rect_0.tab.x < rect_0.parent.x || rect_0.parent.x + rect_0.parent.width < rect_0.tab.x + rect_0.tab.width) { let left = refs.tabsRoot.current.scrollLeft; if (rect_0.tab.x < rect_0.parent.x) left += rect_0.tab.x - rect_0.parent.x;else left += rect_0.tab.x + rect_0.tab.width - (rect_0.parent.x + rect_0.parent.width); // Update refs.tabsRoot.current.scrollTo({ left, behavior: 'smooth' }); } } else { if (rect_0.tab.y < rect_0.parent.y || rect_0.parent.y + rect_0.parent.height < rect_0.tab.y + rect_0.tab.height) { let top = refs.tabsRoot.current.scrollTop; if (rect_0.tab.y < rect_0.parent.y) top += rect_0.tab.y - rect_0.parent.y;else top += rect_0.tab.y + rect_0.tab.height - (rect_0.parent.y + rect_0.parent.height); // Update refs.tabsRoot.current.scrollTo({ top, behavior: 'smooth' }); } } // Update lineValues value setLineValues({ x: rect_0.line.x - rect_0.parent.x + refs.tabsRoot.current.scrollLeft, y: rect_0.line.y - rect_0.parent.y + refs.tabsRoot.current.scrollTop, width: rect_0.line.width, height: rect_0.line.height }); } } }; const direction = orientation === 'horizontal' ? 'row' : 'column'; const propPosition = orientation === 'horizontal' ? 'left' : 'top'; const propMain = orientation === 'horizontal' ? 'width' : 'height'; const useArrows = arrows && (!mobile || arrowsMobile); const ArrowPre = /*#__PURE__*/_jsx(IconButton, { color: "inherit", onClick: () => move(false), className: classNames([staticClassName('Tabs', theme) && ['onesy-Tabs-arrow'], classes.arrow]), disabled: refs.tabsRoot.current?.[orientation === 'horizontal' ? 'scrollLeft' : 'scrollTop'] === 0, children: orientation === 'horizontal' ? /*#__PURE__*/_jsx(IconStart, {}) : /*#__PURE__*/_jsx(IconTop, {}) }); const ArrowPost = /*#__PURE__*/_jsx(IconButton, { color: "inherit", onClick: () => move(), className: classNames([staticClassName('Tabs', theme) && ['onesy-Tabs-arrow'], classes.arrow]), disabled: orientation === 'horizontal' ? Math.ceil(refs.tabsRoot.current?.clientWidth + refs.tabsRoot.current?.scrollLeft) === refs.tabsRoot.current?.scrollWidth : Math.ceil(refs.tabsRoot.current?.clientHeight + refs.tabsRoot.current?.scrollTop) === refs.tabsRoot.current?.scrollHeight, children: orientation === 'horizontal' ? /*#__PURE__*/_jsx(IconEnd, {}) : /*#__PURE__*/_jsx(IconBottom, {}) }); const TabElements = React.Children.toArray(children).filter(item_0 => item_0?.type?.displayName?.endsWith('Tab')); const tabActive = TabElements.find((item_1, index_0) => { const valueItem_0 = item_1.props.value !== undefined ? item_1.props.value : index_0; return is('function', refs.isActive.current) ? refs.isActive.current(value, valueItem_0) : value === valueItem_0; }); return /*#__PURE__*/_jsxs(Surface, _objectSpread(_objectSpread(_objectSpread({ ref: item_2 => { if (ref) { if (is('function', ref)) ref(item_2);else ref.current = item_2; } refs.root.current = item_2; }, tonal: tonal, color: color, gap: 0, direction: direction, align: align, justify: justify, role: "tablist", "aria-orientation": orientation, Component: Line, AdditionalProps: { Component } }, SurfaceProps), {}, { className: classNames([staticClassName('Tabs', theme) && ['onesy-Tabs-root', `onesy-Tabs-version-${version}`, `onesy-Tabs-size-${size}`], SurfaceProps?.className, className, classes.root, classes[`size_${size}`], classes[`orientation_${orientation}`], fixed && classes.fixed]) }, other), {}, { children: [useArrows && ArrowPre, !noDivider && /*#__PURE__*/_jsx(Divider, { tonal: tonal, color: color, orientation: orientation, className: classNames([staticClassName('Tabs', theme) && ['onesy-Tabs-divider'], classes.divider, classes[`divider_orientation_${orientation}`]]) }), /*#__PURE__*/_jsxs(Line, { ref: refs.tabsRoot, gap: 0, direction: direction, align: "flex-start", justify: "flex-start", onScroll: onScroll, className: classNames([staticClassName('Tabs', theme) && ['onesy-Tabs-tabs'], classes.tabs, classes[`tabs_orientation_${orientation}`]]), children: [tabActive && /*#__PURE__*/_jsx("span", { className: classNames([staticClassName('Tabs', theme) && ['onesy-Tabs-line'], classes.line, classes[`line_version_${version}_size_${size}_orientation_${orientation}`], orientation === 'vertical' && theme.direction === 'rtl' && classes[`line_version_${version}_orientation_vertical_rtl`]]), style: { [propPosition]: orientation === 'horizontal' ? lineValues.x : lineValues.y, [propMain]: lineValues[propMain] } }), TabElements.map((item_3, index_1) => { const valueItem_1 = item_3.props.value !== undefined ? item_3.props.value : index_1; return /*#__PURE__*/React.cloneElement(item_3, { tonal: item_3.props.tonal !== undefined ? item_3.props.tonal : tonal, color: item_3.props.color !== undefined ? item_3.props.color : color, version: item_3.props.version !== undefined ? item_3.props.version : version, value: item_3.props.value !== undefined ? item_3.props.value : index_1, size: item_3.props.size !== undefined ? item_3.props.size : size, index: index_1, onChange, activateOnFocus: item_3.props.activateOnFocus !== undefined ? item_3.props.activateOnFocus : activateOnFocus, active: is('function', refs.isActive.current) ? refs.isActive.current(value, valueItem_1) : value === valueItem_1, onClick: event_0 => { onChange(valueItem_1, index_1); if (is('function', item_3.props.onClick)) item_3.props.onClick(event_0); } }); })] }), useArrows && ArrowPost] })); }; Tabs.displayName = 'onesy-Tabs'; export default Tabs;