@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
82 lines • 3.43 kB
JavaScript
/**
*
* 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/>.
*/
import { autoUpdate, flip, offset, safePolygon, shift, useClick, useDismiss, useFloating, useFocus, useHover, useInteractions, useRole, } from '@floating-ui/react';
import { createContext, useContext, useMemo, useState } from 'react';
export function useTooltip({ isInitialOpen = false, placement = 'top', isOpen: controlledOpen, onOpenChange: setControlledOpen, type = 'simple', isPortaled = true, strategy = 'absolute', hoverDelay = undefined, shouldCloseOnReferenceClick = false, autoUpdateOptions, } = {}) {
const [isUncontrolledOpen, setIsUncontrolledOpen] = useState(isInitialOpen);
const isOpen = controlledOpen !== null && controlledOpen !== void 0 ? controlledOpen : isUncontrolledOpen;
const setOpen = setControlledOpen !== null && setControlledOpen !== void 0 ? setControlledOpen : setIsUncontrolledOpen;
const data = useFloating({
placement,
open: isOpen,
onOpenChange: setOpen,
whileElementsMounted(referenceEl, floatingEl, update) {
const cleanup = autoUpdate(referenceEl, floatingEl, update, Object.assign({}, autoUpdateOptions));
return cleanup;
},
middleware: [
offset(5),
flip({
crossAxis: placement.includes('-'),
fallbackAxisSideDirection: 'start',
padding: 5,
}),
shift({ padding: 5 }),
],
strategy,
});
const context = data.context;
const hover = useHover(context, {
move: false,
delay: hoverDelay,
enabled: type === 'simple',
handleClose: safePolygon(),
});
const click = useClick(context, {
enabled: type === 'rich',
});
const focus = useFocus(context, {
visibleOnly: true,
enabled: type === 'simple',
});
const dismiss = useDismiss(context, {
outsidePress: true,
escapeKey: true,
referencePress: shouldCloseOnReferenceClick,
});
const role = useRole(context, {
role: type === 'simple' ? 'tooltip' : 'dialog',
});
const interactions = useInteractions([hover, focus, dismiss, role, click]);
return useMemo(() => (Object.assign(Object.assign({ isOpen,
setOpen,
type,
isPortaled }, interactions), data)), [isOpen, setOpen, type, isPortaled, interactions, data]);
}
export const TooltipContext = createContext(null);
export const useTooltipContext = () => {
const context = useContext(TooltipContext);
if (context === null) {
throw new Error('Tooltip components must be wrapped in <Tooltip />');
}
return context;
};
//# sourceMappingURL=use-tooltip.js.map