@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
132 lines • 7.4 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/**
*
* 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 { FloatingFocusManager, FloatingPortal, useMergeRefs, } from '@floating-ui/react';
import classNames from 'classnames';
import { cloneElement, forwardRef, isValidElement, } from 'react';
import { Button } from '../button';
import { ConditionalWrap } from '../conditional-wrap';
import { useIsInsideDialog } from '../dialog';
import { IconButton } from '../icon-button';
import { XMarkIconOutline } from '../icons';
import { useNeedleTheme } from '../theme';
import { Typography } from '../typography';
import { TooltipContext, useTooltip, useTooltipContext } from './use-tooltip';
export function Tooltip({ children, isDisabled = false, type, isInitialOpen, placement, isOpen, onOpenChange, isPortaled = true, strategy: strategyProp, hoverDelay, }) {
const isInsideDialog = useIsInsideDialog();
const strategy = isInsideDialog ? 'fixed' : 'absolute';
const tooltip = useTooltip({
isInitialOpen,
// if isDisabled is true tooltip will not open otherwise either controlled or uncontrolled depending on if isOpen is passed
isOpen: isDisabled === true ? false : isOpen,
isPortaled,
onOpenChange,
placement,
strategy: strategyProp !== null && strategyProp !== void 0 ? strategyProp : strategy,
type,
hoverDelay,
});
return (_jsx(TooltipContext.Provider, { value: tooltip, children: children }));
}
const TooltipTrigger = forwardRef(function TooltipTrigger({ children, hasButtonWrapper = false, htmlAttributes, className, }, ref) {
const context = useTooltipContext();
// Example from floating-ui
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const childrenRef = children.ref;
const mergedRefs = useMergeRefs([
context.refs.setReference,
ref,
childrenRef,
]);
const triggerClasses = classNames({
'ndl-closed': !context.isOpen,
'ndl-open': context.isOpen,
}, 'ndl-tooltip-trigger', className);
// `hasButtonWrapper=true` allows the user to pass any element as the anchor
if (hasButtonWrapper && isValidElement(children)) {
return cloneElement(children, context.getReferenceProps(Object.assign(Object.assign({ className: triggerClasses, ref: mergedRefs }, htmlAttributes), children.props)));
}
return (_jsx("button", Object.assign({ className: triggerClasses, ref: mergedRefs }, context.getReferenceProps(htmlAttributes), { children: children })));
});
const TooltipContent = forwardRef(function TooltipContent({ children, style, htmlAttributes, className }, propsRef) {
const context = useTooltipContext();
const ref = useMergeRefs([context.refs.setFloating, propsRef]);
const { themeClassName } = useNeedleTheme();
if (!context.isOpen)
return null;
const classes = classNames('ndl-tooltip-content', themeClassName, className, {
'ndl-tooltip-content-rich': context.type === 'rich',
'ndl-tooltip-content-simple': context.type === 'simple',
});
if (context.type === 'simple') {
return (_jsx(ConditionalWrap, { shouldWrap: context.isPortaled, wrap: (wrapChildren) => (_jsx(FloatingPortal, { children: wrapChildren })), children: _jsx("div", Object.assign({ ref: ref, className: classes, style: Object.assign(Object.assign({}, context.floatingStyles), style) }, context.getFloatingProps(htmlAttributes), { children: _jsx(Typography, { variant: "body-medium", children: children }) })) }));
}
return (_jsx(ConditionalWrap, { shouldWrap: context.isPortaled, wrap: (wrapChildren) => _jsx(FloatingPortal, { children: wrapChildren }), children: _jsx(FloatingFocusManager, { context: context.context, returnFocus: true, modal: false, initialFocus: -1, closeOnFocusOut: true, children: _jsx("div", Object.assign({ ref: ref, className: classes, style: Object.assign(Object.assign({}, context.floatingStyles), style) }, context.getFloatingProps(htmlAttributes), { children: children })) }) }));
});
const TooltipHeader = ({ children, className, passThroughProps, }) => {
const context = useTooltipContext();
const classes = classNames('ndl-tooltip-header', className);
if (!context.isOpen)
return null;
return (_jsx(Typography, Object.assign({ variant: "subheading-medium", className: classes }, passThroughProps, { children: children })));
};
const TooltipBody = ({ children, className, passThroughProps, }) => {
const context = useTooltipContext();
const classes = classNames('ndl-tooltip-body', className);
if (!context.isOpen)
return null;
return (_jsx(Typography, Object.assign({ variant: "body-medium", className: classes }, passThroughProps, { children: children })));
};
const Actions = forwardRef(function Actions({ htmlAttributes, className, children }, ref) {
const context = useTooltipContext();
const mergedRefs = useMergeRefs([context.refs.setFloating, ref]);
if (!context.isOpen)
return null;
const classes = classNames('ndl-tooltip-actions', className);
return (_jsx("div", Object.assign({ className: classes, ref: mergedRefs }, htmlAttributes, { children: children })));
});
const Action = ({ children, className, onClick, htmlAttributes, variant = 'underlined', }) => {
const context = useTooltipContext();
if (!context.isOpen)
return null;
if (variant === 'underlined') {
const classes = classNames('ndl-tooltip-action', className);
return (_jsx(Typography, Object.assign({ as: "button", onClick: onClick, variant: "label", className: classes }, htmlAttributes, { children: children })));
}
return (_jsx(Button, { fill: variant, size: "small", onClick: onClick, className: className, htmlAttributes: htmlAttributes, children: children }));
};
const ActionClose = forwardRef(function ActionClose({ className, htmlAttributes }, ref) {
const context = useTooltipContext();
if (!context.isOpen)
return null;
const classes = classNames('ndl-tooltip-action-close', className);
return (_jsx(IconButton, Object.assign({ as: "button", isClean: true, ariaLabel: "close", size: "small", onClick: () => context.setOpen(false), ref: ref, className: classes }, htmlAttributes, { children: _jsx(XMarkIconOutline, {}) })));
});
Tooltip.Content = TooltipContent;
Tooltip.Trigger = TooltipTrigger;
Tooltip.Header = TooltipHeader;
Tooltip.Body = TooltipBody;
Tooltip.Actions = Actions;
Tooltip.Action = Action;
// TODO v4: remove the action close button
/** @deprecated clicking outside the tooltip closes it */
Tooltip.ActionClose = ActionClose;
//# sourceMappingURL=Tooltip.js.map