UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

87 lines 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTooltipContext = exports.TooltipContext = void 0; exports.useTooltip = useTooltip; /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ const react_1 = require("@floating-ui/react"); const react_2 = require("react"); function useTooltip({ isInitialOpen = false, placement = 'top', isOpen: controlledOpen, onOpenChange: setControlledOpen, type = 'simple', isPortaled = true, strategy = 'absolute', hoverDelay = undefined, shouldCloseOnReferenceClick = false, autoUpdateOptions, } = {}) { const [isUncontrolledOpen, setIsUncontrolledOpen] = (0, react_2.useState)(isInitialOpen); const isOpen = controlledOpen !== null && controlledOpen !== void 0 ? controlledOpen : isUncontrolledOpen; const setOpen = setControlledOpen !== null && setControlledOpen !== void 0 ? setControlledOpen : setIsUncontrolledOpen; const data = (0, react_1.useFloating)({ placement, open: isOpen, onOpenChange: setOpen, whileElementsMounted(referenceEl, floatingEl, update) { const cleanup = (0, react_1.autoUpdate)(referenceEl, floatingEl, update, Object.assign({}, autoUpdateOptions)); return cleanup; }, middleware: [ (0, react_1.offset)(5), (0, react_1.flip)({ crossAxis: placement.includes('-'), fallbackAxisSideDirection: 'start', padding: 5, }), (0, react_1.shift)({ padding: 5 }), ], strategy, }); const context = data.context; const hover = (0, react_1.useHover)(context, { move: false, delay: hoverDelay, enabled: type === 'simple', handleClose: (0, react_1.safePolygon)(), }); const click = (0, react_1.useClick)(context, { enabled: type === 'rich', }); const focus = (0, react_1.useFocus)(context, { visibleOnly: true, enabled: type === 'simple', }); const dismiss = (0, react_1.useDismiss)(context, { outsidePress: true, escapeKey: true, referencePress: shouldCloseOnReferenceClick, }); const role = (0, react_1.useRole)(context, { role: type === 'simple' ? 'tooltip' : 'dialog', }); const interactions = (0, react_1.useInteractions)([hover, focus, dismiss, role, click]); return (0, react_2.useMemo)(() => (Object.assign(Object.assign({ isOpen, setOpen, type, isPortaled }, interactions), data)), [isOpen, setOpen, type, isPortaled, interactions, data]); } exports.TooltipContext = (0, react_2.createContext)(null); const useTooltipContext = () => { const context = (0, react_2.useContext)(exports.TooltipContext); if (context === null) { throw new Error('Tooltip components must be wrapped in <Tooltip />'); } return context; }; exports.useTooltipContext = useTooltipContext; //# sourceMappingURL=use-tooltip.js.map