UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

157 lines 9.32 kB
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; }; 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 React, { useEffect } from 'react'; import { ConditionalWrap } from '../conditional-wrap'; import { useIsInsideDialog } from '../dialog/dialog-context'; import { useNeedleTheme } from '../theme'; import { Typography } from '../typography'; import { TooltipContext, useTooltip, useTooltipContext } from './use-tooltip'; const TooltipComponent = ({ children, isDisabled = false, type, isInitialOpen, placement, isOpen, onOpenChange, isPortaled: isPortaledProp, floatingStrategy: strategyProp, hoverDelay, shouldCloseOnReferenceClick, autoUpdateOptions, followCursor, }) => { const isInsideDialog = useIsInsideDialog(); const strategy = isInsideDialog ? 'fixed' : 'absolute'; const isPortaled = !isInsideDialog; const tooltip = useTooltip({ autoUpdateOptions, followCursor, hoverDelay, isDisabled, 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: isPortaledProp !== null && isPortaledProp !== void 0 ? isPortaledProp : isPortaled, onOpenChange, placement, shouldCloseOnReferenceClick, strategy: strategyProp !== null && strategyProp !== void 0 ? strategyProp : strategy, type, }); return (_jsx(TooltipContext.Provider, { value: tooltip, children: children })); }; TooltipComponent.displayName = 'Tooltip'; const TooltipTrigger = (_a) => { var { children, hasButtonWrapper = false, htmlAttributes, className, style, ref } = _a, restProps = __rest(_a, ["children", "hasButtonWrapper", "htmlAttributes", "className", "style", "ref"]); const context = useTooltipContext(); // Example from floating-ui // oxlint-disable-next-line @typescript-eslint/no-explicit-any const childrenProps = children.props; const mergedRefs = useMergeRefs([ context.refs.setReference, ref, childrenProps === null || childrenProps === void 0 ? void 0 : childrenProps.ref, ]); 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 && React.isValidElement(children)) { const props = Object.assign(Object.assign(Object.assign({ className: triggerClasses }, htmlAttributes), childrenProps), { ref: mergedRefs }); return React.cloneElement(children, context.getReferenceProps(props)); } return (_jsx("button", Object.assign({ type: "button", className: triggerClasses, style: style, ref: mergedRefs }, context.getReferenceProps(htmlAttributes), restProps, { children: children }))); }; const TooltipContent = (_a) => { var _b; var { children, style, htmlAttributes, className, ref } = _a, restProps = __rest(_a, ["children", "style", "htmlAttributes", "className", "ref"]); const context = useTooltipContext(); const mergedRef = useMergeRefs([context.refs.setFloating, ref]); const { themeClassName } = useNeedleTheme(); const hasHeaderChild = React.useMemo(() => { if (context.type !== 'rich') { return false; } const childrenArray = React.Children.toArray(children); return childrenArray.some((child) => React.isValidElement(child) && child.type === TooltipHeader); }, [children, context.type]); 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: mergedRef, className: classes, style: Object.assign(Object.assign({}, context.floatingStyles), style) }, restProps, context.getFloatingProps(htmlAttributes), { children: _jsx(Typography, { variant: "body-medium", children: children }) })) })); } const ariaLabelledBy = (_b = htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-labelledby']) !== null && _b !== void 0 ? _b : (hasHeaderChild ? context.headerId : undefined); if ((htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-label']) === undefined && !ariaLabelledBy) { console.warn('The rich Tooltip is missing aria-label and Header. Please add one of them for accessibility.'); } return (_jsx(ConditionalWrap, { shouldWrap: context.isPortaled, wrap: (wrapChildren) => _jsx(FloatingPortal, { children: wrapChildren }), children: _jsx(FloatingFocusManager, { context: context.context, returnFocus: true, modal: false, initialFocus: 0, closeOnFocusOut: true, children: _jsx("div", Object.assign({ ref: mergedRef, className: classes, style: Object.assign(Object.assign({}, context.floatingStyles), style) }, restProps, context.getFloatingProps(Object.assign(Object.assign({}, htmlAttributes), { 'aria-labelledby': ariaLabelledBy })), { children: children })) }) })); }; const TooltipHeader = (_a) => { var { children, passThroughProps, typographyVariant = 'subheading-medium', className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "passThroughProps", "typographyVariant", "className", "style", "htmlAttributes", "ref"]); const context = useTooltipContext(); const classes = classNames('ndl-tooltip-header', className); useEffect(() => { var _a; if (Boolean(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id) === true) { (_a = context.setHeaderId) === null || _a === void 0 ? void 0 : _a.call(context, htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id); } }, [context, htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id]); if (!context.isOpen) { return null; } return (_jsx(Typography, Object.assign({ ref: ref, variant: typographyVariant, className: classes, style: style, htmlAttributes: Object.assign(Object.assign({}, htmlAttributes), { id: context.headerId }) }, passThroughProps, restProps, { children: children }))); }; const TooltipBody = (_a) => { var { children, className, style, htmlAttributes, passThroughProps, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "passThroughProps", "ref"]); const context = useTooltipContext(); const classes = classNames('ndl-tooltip-body', className); if (!context.isOpen) { return null; } return (_jsx(Typography, Object.assign({ ref: ref, variant: "body-medium", className: classes, style: style, htmlAttributes: htmlAttributes }, passThroughProps, restProps, { children: children }))); }; const Actions = (_a) => { var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "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, style: style }, restProps, htmlAttributes, { children: children }))); }; Actions.displayName = 'Tooltip.Actions'; const Tooltip = Object.assign(TooltipComponent, { Actions: Actions, Body: TooltipBody, Content: TooltipContent, Header: TooltipHeader, Trigger: TooltipTrigger, }); export { Tooltip }; //# sourceMappingURL=Tooltip.js.map