UNPKG

@onesy/ui-react

Version:
414 lines (413 loc) 22 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@onesy/utils"); const style_react_1 = require("@onesy/style-react"); const IconMaterialCloseW100_1 = __importDefault(require("@onesy/icons-material-rounded-react/IconMaterialCloseW100")); const Zoom_1 = __importDefault(require("../Zoom")); const Fade_1 = __importDefault(require("../Fade")); const Fab_1 = __importDefault(require("../Fab")); const Tooltip_1 = __importDefault(require("../Tooltip")); const Line_1 = __importDefault(require("../Line")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { position: 'fixed', zIndex: theme.z_index.modal }, position_top: { top: '24px' }, position_bottom: { bottom: '24px' }, alignment_left: { left: '24px', right: 'auto' }, alignment_start: { insetInlineStart: '24px', insetInlineEnd: 'auto' }, alignment_center: { left: '50%', right: 'auto', transform: 'translateX(-50%)' }, alignment_right: { right: '24px', left: 'auto' }, alignment_end: { insetInlineEnd: '24px', insetInlineStart: 'auto' }, items: { pointerEvents: 'none' }, items_position_row: { paddingInline: theme.methods.space.value(2, 'px') }, 'items_position_row-reverse': { paddingInline: theme.methods.space.value(2, 'px') }, items_position_column: { paddingBlock: theme.methods.space.value(2, 'px') }, 'items_position_column-reverse': { paddingBlock: theme.methods.space.value(2, 'px') }, items_open: { pointerEvents: 'auto' }, fab: { '&.onesy-Fab-root': { boxShadow: theme.shadows.values.default[6], transition: theme.methods.transitions.make('box-shadow') } }, fab_open: { '&.onesy-Fab-root': { boxShadow: theme.shadows.values.default[8] } }, iconWrapper: { position: 'relative', lineHeight: '0', transform: 'rotate(-45deg)', transition: theme.methods.transitions.make('transform') }, iconWrapper_noRotate: { transform: 'rotate(0deg)' }, iconWrapper_open: { transform: 'rotate(0deg)' }, icon_absolute: { position: 'absolute', inset: '0' } }), { name: 'onesy-SpeedDial' }); const IconWrapper = (props) => { return ((0, jsx_runtime_1.jsx)("span", Object.assign({}, props, { children: react_1.default.Children.toArray(props.children).map((item) => (react_1.default.cloneElement(item, { style: Object.assign(Object.assign({}, item.props.style), props.style) }))) }))); }; const SpeedDial = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useOnesyTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.onesySpeedDial) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const Zoom = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Zoom) || Zoom_1.default; }, [theme]); const Fade = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Fade) || Fade_1.default; }, [theme]); const Fab = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Fab) || Fab_1.default; }, [theme]); const Tooltip = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Tooltip) || Tooltip_1.default; }, [theme]); const { tonal = true, color = 'primary', version = 'filled', open: open_, openDefault, onOpen: onOpen_, onClose: onClose_, in: inProp = true, position = 'bottom', alignment = 'end', direction: direction_ = 'top', closeOnClick, tooltipOpen, start, end, noRotate, tooltipLabel, onKeyDown: onKeyDown_, disabled, IconOpen, Icon: Icon_ = IconMaterialCloseW100_1.default, FabTransitionComponent = Zoom, SpeeDialItemTransitionComponent: SpeeDialItemTransitionComponent_ = Zoom, IconProps, TooltipProps = { alignment: 'center', interactive: false }, FabProps = { elevation: false }, FabWrapperProps, FabTransitionComponentProps, SpeeDialItemTransitionComponentProps, Component = 'div', className, children } = props, other = __rest(props, ["tonal", "color", "version", "open", "openDefault", "onOpen", "onClose", "in", "position", "alignment", "direction", "closeOnClick", "tooltipOpen", "start", "end", "noRotate", "tooltipLabel", "onKeyDown", "disabled", "IconOpen", "Icon", "FabTransitionComponent", "SpeeDialItemTransitionComponent", "IconProps", "TooltipProps", "FabProps", "FabWrapperProps", "FabTransitionComponentProps", "SpeeDialItemTransitionComponentProps", "Component", "className", "children"]); const [focus, setFocus] = react_1.default.useState(false); const [hover, setHover] = react_1.default.useState(false); const [open, setOpen] = react_1.default.useState(openDefault !== undefined ? openDefault : open_); const { classes } = useStyle(); const refs = { root: react_1.default.useRef(undefined), fab: react_1.default.useRef(undefined), line: react_1.default.useRef(undefined) }; const direction = direction_; let SpeeDialItemTransitionComponent = SpeeDialItemTransitionComponent_; if (tooltipOpen) SpeeDialItemTransitionComponent = Fade; const onOpen = () => { // Update inner or controlled if (!props.hasOwnProperty('open')) setOpen(true); if ((0, utils_1.is)('function', onOpen_)) onOpen_(); }; const onClose = () => { // Update inner or controlled if (!props.hasOwnProperty('open')) setOpen(false); setHover(false); setFocus(false); if ((0, utils_1.is)('function', onClose_)) onClose_(); }; react_1.default.useEffect(() => { if (hover) { if (!open) onOpen(); } else { if (open) onClose(); } }, [hover]); react_1.default.useEffect(() => { if (focus) { if (!open) onOpen(); } else { if (open && !hover) onClose(); } }, [focus]); react_1.default.useEffect(() => { if (open_ !== open) setOpen(open_); }, [open_]); const onClick = () => { !open ? onOpen() : onClose(); }; const onMouseEnter = react_1.default.useCallback((event) => { if (!disabled && (event.target.contains(refs.fab.current) || event.target.contains(refs.line.current))) { setHover(true); } }, [disabled]); const onMouseLeave = react_1.default.useCallback((event) => { if (!disabled) { setHover(false); } }, [disabled]); const onFocus = react_1.default.useCallback((event) => { if (!disabled) { setFocus(true); } }, [disabled]); const onBlur = react_1.default.useCallback((event) => { if (!disabled) { setFocus(false); } }, [disabled]); let lineDirection = 'row'; // Safari bug *-reverse using gap // let lineItemsDirection = 'row-reverse'; let lineItemsDirection = !(0, utils_1.isBrowser)('safari') ? 'row-reverse' : 'row'; let directionToUse = direction; if (position === 'top') { // Safari bug *-reverse using gap // lineDirection = 'column-reverse'; if (!(0, utils_1.isBrowser)('safari')) lineDirection = 'column-reverse'; else lineDirection = 'column'; lineItemsDirection = 'column'; if (alignment === 'left' || (alignment === 'start' && theme.direction === 'ltr') || (alignment === 'end' && theme.direction === 'rtl')) { if (['top', 'bottom'].includes(direction)) directionToUse = 'bottom'; if (['left', 'right'].includes(direction)) directionToUse = (0, utils_1.isBrowser)('safari') ? 'bottom' : 'right'; if (directionToUse === 'bottom') { TooltipProps.position = 'right'; } if (directionToUse === 'right') { TooltipProps.position = 'bottom'; // Safari bug *-reverse using gap // lineDirection = 'row-reverse'; lineDirection = theme.direction === 'ltr' ? 'row-reverse' : 'row'; lineItemsDirection = theme.direction === 'ltr' ? 'row' : 'row-reverse'; } } if (alignment === 'center') { if (!['bottom'].includes(direction)) directionToUse = 'bottom'; if (directionToUse === 'bottom') { TooltipProps.position = 'left'; } } if (alignment === 'right' || (alignment === 'end' && theme.direction === 'ltr') || (alignment === 'start' && theme.direction === 'rtl')) { if (['top', 'bottom'].includes(direction)) directionToUse = 'bottom'; if (['left', 'right'].includes(direction)) directionToUse = (0, utils_1.isBrowser)('safari') ? 'bottom' : 'left'; if (directionToUse === 'bottom') { TooltipProps.position = 'left'; } if (directionToUse === 'left') { TooltipProps.position = 'bottom'; lineDirection = theme.direction === 'ltr' ? 'row' : 'row-reverse'; // Safari bug *-reverse using gap // lineItemsDirection = 'row-reverse'; lineItemsDirection = theme.direction === 'ltr' ? 'row-reverse' : 'row'; } } } if (position === 'bottom') { lineDirection = 'column'; // Safari bug *-reverse using gap // lineItemsDirection = 'column-reverse'; if (!(0, utils_1.isBrowser)('safari')) lineItemsDirection = 'column-reverse'; else lineItemsDirection = 'column'; if (alignment === 'left' || (alignment === 'start' && theme.direction === 'ltr') || (alignment === 'end' && theme.direction === 'rtl')) { if (['top', 'bottom'].includes(direction)) directionToUse = 'top'; if (['left', 'right'].includes(direction)) directionToUse = (0, utils_1.isBrowser)('safari') ? 'top' : 'right'; if (directionToUse === 'top') { TooltipProps.position = 'right'; } if (directionToUse === 'right') { TooltipProps.position = 'top'; // Safari bug *-reverse using gap // lineDirection = 'row-reverse'; lineDirection = theme.direction === 'ltr' ? 'row-reverse' : 'row'; lineItemsDirection = theme.direction === 'ltr' ? 'row' : 'row-reverse'; } } if (alignment === 'center') { if (!['top'].includes(direction)) directionToUse = 'top'; if (directionToUse === 'top') { TooltipProps.position = 'left'; } } if (alignment === 'right' || (alignment === 'end' && theme.direction === 'ltr') || (alignment === 'start' && theme.direction === 'rtl')) { if (['top', 'bottom'].includes(direction)) directionToUse = 'top'; if (['left', 'right'].includes(direction)) directionToUse = (0, utils_1.isBrowser)('safari') ? 'top' : 'left'; if (directionToUse === 'top') { TooltipProps.position = 'left'; } if (directionToUse === 'left') { TooltipProps.position = 'top'; lineDirection = theme.direction === 'ltr' ? 'row' : 'row-reverse'; // Safari bug *-reverse using gap // lineItemsDirection = 'row-reverse'; lineItemsDirection = theme.direction === 'ltr' ? 'row-reverse' : 'row'; } } } let linePosition = 'row'; if (['top', 'bottom'].includes(directionToUse)) linePosition = 'column'; if (!tooltipLabel) TooltipProps.open = false; const onKeyDown = (event) => { var _a, _b; const rootDocument = (0, utils_1.isEnvironment)('browser') ? (((_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window.document) : undefined; let allElements = []; if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key)) { allElements = [...Array.from((_b = refs.line.current) === null || _b === void 0 ? void 0 : _b.children).map((item) => item.children[0]).filter((item) => { const method = (0, utils_1.is)('element', item) && (item.matches || item['webkitMatchesSelector'] || item['mozMatchesSelector'] || item['oMatchesSelector'] || item['msMatchesSelector']); const query = `a[href]:not([tabindex='-1']), area[href]:not([tabindex='-1']), input:not([disabled]):not([tabindex='-1']), select:not([disabled]):not([tabindex='-1']), textarea:not([disabled]):not([tabindex='-1']), button:not([disabled]):not([tabindex='-1']), iframe:not([tabindex='-1']), [tabindex]:not([tabindex='-1']), [contentEditable=true]:not([tabindex='-1'])`; return method.bind(item)(query) || item.tabIndex > -1; })]; if (!lineItemsDirection.includes('reverse')) { allElements.reverse(); allElements.push(refs.fab.current); } else allElements.unshift(refs.fab.current); } switch (event.key) { case 'Escape': return onClose(); case 'ArrowUp': case 'ArrowDown': if (lineItemsDirection.includes('column')) { let index = (0, utils_1.clamp)(allElements.findIndex(item => item === rootDocument.activeElement), 0); event.key === 'ArrowUp' ? index++ : index--; allElements[(0, utils_1.clamp)(index, 0, allElements.length - 1)].focus(); event.preventDefault(); } return; case 'ArrowLeft': case 'ArrowRight': if (lineItemsDirection.includes('row')) { let index = (0, utils_1.clamp)(allElements.findIndex(item => item === rootDocument.activeElement), 0); event.key === 'ArrowLeft' ? index++ : index--; allElements[(0, utils_1.clamp)(index, 0, allElements.length - 1)].focus(); event.preventDefault(); } return; default: break; } }; const reverse = lineDirection.includes('reverse') || lineItemsDirection.includes('reverse'); const elementsChildren = react_1.default.Children.toArray(children); return ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, gap: 0, direction: lineDirection, align: 'center', justify: 'center', onKeyDown: onKeyDown, Component: Component, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('SpeedDial', theme) && [ `onesy-SpeedDial-root`, open && `onesy-SpeedDial-open`, hover && `onesy-SpeedDial-hover`, focus && `onesy-SpeedDial-focus`, disabled && `onesy-SpeedDial-disabled` ], className, classes.root, classes[`position_${position}`], classes[`alignment_${alignment}`], disabled && classes.disabled ]) }, other, { children: [!!elementsChildren.length && ((0, jsx_runtime_1.jsx)(Line, Object.assign({ ref: refs.line, gap: 1, direction: lineItemsDirection, align: 'center', justify: 'center', onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('SpeedDial', theme) && [ `onesy-SpeedDial-items` ], classes.items, classes[`items_position_${linePosition}`], open && classes.items_open ]) }, { children: elementsChildren.map((item, index) => ((0, jsx_runtime_1.jsx)(SpeeDialItemTransitionComponent, Object.assign({ in: open, delay: (((open && !reverse) || (!open && reverse)) ? children.length - 1 - index : index) * 30, append: true, add: true, removeOnExited: true, addTransition: theme.methods.transitions.make('box-shadow') }, SpeeDialItemTransitionComponentProps, { children: react_1.default.cloneElement(item, { open, tonal, color, version, tooltipOpen, onClick: (event) => { if (item.props.closeOnClick !== undefined ? item.props.closeOnClick : closeOnClick) onClose(); if ((0, utils_1.is)('function', item.props.onClick)) item.props.onClick(event); }, onBlur, onFocus, TooltipProps: Object.assign({}, TooltipProps) }) }), index))) }))), (0, jsx_runtime_1.jsx)(FabTransitionComponent, Object.assign({ in: inProp }, FabTransitionComponentProps, { children: (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'column' }, FabWrapperProps, { children: [start, (0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ name: tooltipLabel }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(Fab, Object.assign({ ref: refs.fab, tonal: tonal, color: color, version: version, onClick: onClick, onBlur: onBlur, onFocus: onFocus, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('SpeedDial', theme) && [ `onesy-SpeedDial-fab` ], classes.fab, open && classes.fab_open ]) }, FabProps, { disabled: disabled }, { children: (0, jsx_runtime_1.jsx)(IconWrapper, Object.assign({ className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('SpeedDial', theme) && [ `onesy-SpeedDial-icon-wrapper` ], classes.iconWrapper, noRotate && classes.iconWrapper_noRotate, open && classes.iconWrapper_open ]) }, { children: !IconOpen ? (0, jsx_runtime_1.jsx)(Icon_, Object.assign({}, IconProps)) : (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: !open }, { children: (0, jsx_runtime_1.jsx)(Icon_, Object.assign({}, IconProps, { className: (0, style_react_1.classNames)([ IconProps === null || IconProps === void 0 ? void 0 : IconProps.className, classes.icon_absolute ]) })) })), (0, jsx_runtime_1.jsx)(Fade, Object.assign({ in: open }, { children: (0, jsx_runtime_1.jsx)(IconOpen, Object.assign({}, IconProps)) }))] }) })) })) })), end] })) }))] }))); }); SpeedDial.displayName = 'onesy-SpeedDial'; exports.default = SpeedDial;