UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

138 lines 8.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Tooltip = Tooltip; const jsx_runtime_1 = require("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/>. */ const react_1 = require("@floating-ui/react"); const classnames_1 = __importDefault(require("classnames")); const react_2 = require("react"); const button_1 = require("../button"); const conditional_wrap_1 = require("../conditional-wrap"); const dialog_1 = require("../dialog"); const icon_button_1 = require("../icon-button"); const icons_1 = require("../icons"); const theme_1 = require("../theme"); const typography_1 = require("../typography"); const use_tooltip_1 = require("./use-tooltip"); function Tooltip({ children, isDisabled = false, type, isInitialOpen, placement, isOpen, onOpenChange, isPortaled = true, strategy: strategyProp, hoverDelay, }) { const isInsideDialog = (0, dialog_1.useIsInsideDialog)(); const strategy = isInsideDialog ? 'fixed' : 'absolute'; const tooltip = (0, use_tooltip_1.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 ((0, jsx_runtime_1.jsx)(use_tooltip_1.TooltipContext.Provider, { value: tooltip, children: children })); } const TooltipTrigger = (0, react_2.forwardRef)(function TooltipTrigger({ children, hasButtonWrapper = false, htmlAttributes, className, }, ref) { const context = (0, use_tooltip_1.useTooltipContext)(); // Example from floating-ui // eslint-disable-next-line @typescript-eslint/no-explicit-any const childrenRef = children.ref; const mergedRefs = (0, react_1.useMergeRefs)([ context.refs.setReference, ref, childrenRef, ]); const triggerClasses = (0, classnames_1.default)({ '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 && (0, react_2.isValidElement)(children)) { return (0, react_2.cloneElement)(children, context.getReferenceProps(Object.assign(Object.assign({ className: triggerClasses, ref: mergedRefs }, htmlAttributes), children.props))); } return ((0, jsx_runtime_1.jsx)("button", Object.assign({ className: triggerClasses, ref: mergedRefs }, context.getReferenceProps(htmlAttributes), { children: children }))); }); const TooltipContent = (0, react_2.forwardRef)(function TooltipContent({ children, style, htmlAttributes, className }, propsRef) { const context = (0, use_tooltip_1.useTooltipContext)(); const ref = (0, react_1.useMergeRefs)([context.refs.setFloating, propsRef]); const { themeClassName } = (0, theme_1.useNeedleTheme)(); if (!context.isOpen) return null; const classes = (0, classnames_1.default)('ndl-tooltip-content', themeClassName, className, { 'ndl-tooltip-content-rich': context.type === 'rich', 'ndl-tooltip-content-simple': context.type === 'simple', }); if (context.type === 'simple') { return ((0, jsx_runtime_1.jsx)(conditional_wrap_1.ConditionalWrap, { shouldWrap: context.isPortaled, wrap: (wrapChildren) => ((0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { children: wrapChildren })), children: (0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, className: classes, style: Object.assign(Object.assign({}, context.floatingStyles), style) }, context.getFloatingProps(htmlAttributes), { children: (0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: "body-medium", children: children }) })) })); } return ((0, jsx_runtime_1.jsx)(conditional_wrap_1.ConditionalWrap, { shouldWrap: context.isPortaled, wrap: (wrapChildren) => (0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { children: wrapChildren }), children: (0, jsx_runtime_1.jsx)(react_1.FloatingFocusManager, { context: context.context, returnFocus: true, modal: false, initialFocus: -1, closeOnFocusOut: true, children: (0, jsx_runtime_1.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 = (0, use_tooltip_1.useTooltipContext)(); const classes = (0, classnames_1.default)('ndl-tooltip-header', className); if (!context.isOpen) return null; return ((0, jsx_runtime_1.jsx)(typography_1.Typography, Object.assign({ variant: "subheading-medium", className: classes }, passThroughProps, { children: children }))); }; const TooltipBody = ({ children, className, passThroughProps, }) => { const context = (0, use_tooltip_1.useTooltipContext)(); const classes = (0, classnames_1.default)('ndl-tooltip-body', className); if (!context.isOpen) return null; return ((0, jsx_runtime_1.jsx)(typography_1.Typography, Object.assign({ variant: "body-medium", className: classes }, passThroughProps, { children: children }))); }; const Actions = (0, react_2.forwardRef)(function Actions({ htmlAttributes, className, children }, ref) { const context = (0, use_tooltip_1.useTooltipContext)(); const mergedRefs = (0, react_1.useMergeRefs)([context.refs.setFloating, ref]); if (!context.isOpen) return null; const classes = (0, classnames_1.default)('ndl-tooltip-actions', className); return ((0, jsx_runtime_1.jsx)("div", Object.assign({ className: classes, ref: mergedRefs }, htmlAttributes, { children: children }))); }); const Action = ({ children, className, onClick, htmlAttributes, variant = 'underlined', }) => { const context = (0, use_tooltip_1.useTooltipContext)(); if (!context.isOpen) return null; if (variant === 'underlined') { const classes = (0, classnames_1.default)('ndl-tooltip-action', className); return ((0, jsx_runtime_1.jsx)(typography_1.Typography, Object.assign({ as: "button", onClick: onClick, variant: "label", className: classes }, htmlAttributes, { children: children }))); } return ((0, jsx_runtime_1.jsx)(button_1.Button, { fill: variant, size: "small", onClick: onClick, className: className, htmlAttributes: htmlAttributes, children: children })); }; const ActionClose = (0, react_2.forwardRef)(function ActionClose({ className, htmlAttributes }, ref) { const context = (0, use_tooltip_1.useTooltipContext)(); if (!context.isOpen) return null; const classes = (0, classnames_1.default)('ndl-tooltip-action-close', className); return ((0, jsx_runtime_1.jsx)(icon_button_1.IconButton, Object.assign({ as: "button", isClean: true, ariaLabel: "close", size: "small", onClick: () => context.setOpen(false), ref: ref, className: classes }, htmlAttributes, { children: (0, jsx_runtime_1.jsx)(icons_1.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