@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
142 lines • 8.64 kB
JavaScript
"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tooltip = void 0;
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 conditional_wrap_1 = require("../conditional-wrap");
const dialog_context_1 = require("../dialog/dialog-context");
const theme_1 = require("../theme");
const typography_1 = require("../typography");
const use_tooltip_1 = require("./use-tooltip");
const TooltipComponent = ({ children, isDisabled = false, type, isInitialOpen, placement, isOpen, onOpenChange, isPortaled: isPortaledProp, floatingStrategy: strategyProp, hoverDelay, shouldCloseOnReferenceClick, autoUpdateOptions, }) => {
const isInsideDialog = (0, dialog_context_1.useIsInsideDialog)();
const strategy = isInsideDialog ? 'fixed' : 'absolute';
const isPortaled = !isInsideDialog;
const tooltip = (0, use_tooltip_1.useTooltip)({
autoUpdateOptions,
hoverDelay,
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 ((0, jsx_runtime_1.jsx)(use_tooltip_1.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 = (0, use_tooltip_1.useTooltipContext)();
// Example from floating-ui
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const childrenProps = children.props;
const mergedRefs = (0, react_1.useMergeRefs)([
context.refs.setReference,
ref,
childrenProps === null || childrenProps === void 0 ? void 0 : childrenProps.ref,
]);
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)) {
const props = Object.assign(Object.assign(Object.assign({ className: triggerClasses }, htmlAttributes), childrenProps), { ref: mergedRefs });
return (0, react_2.cloneElement)(children, context.getReferenceProps(props));
}
return ((0, jsx_runtime_1.jsx)("button", Object.assign({ className: triggerClasses, style: style, ref: mergedRefs }, context.getReferenceProps(htmlAttributes), restProps, { children: children })));
};
const TooltipContent = (_a) => {
var { children, style, htmlAttributes, className, ref } = _a, restProps = __rest(_a, ["children", "style", "htmlAttributes", "className", "ref"]);
const context = (0, use_tooltip_1.useTooltipContext)();
const mergedRef = (0, react_1.useMergeRefs)([context.refs.setFloating, ref]);
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: mergedRef, className: classes, style: Object.assign(Object.assign({}, context.floatingStyles), style) }, restProps, 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: mergedRef, className: classes, style: Object.assign(Object.assign({}, context.floatingStyles), style) }, restProps, context.getFloatingProps(htmlAttributes), { 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 = (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({ ref: ref, variant: typographyVariant, className: classes, style: style, htmlAttributes: htmlAttributes }, 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 = (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({ 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 = (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, style: style }, restProps, htmlAttributes, { children: children })));
};
const Tooltip = Object.assign(TooltipComponent, {
Actions: Actions,
Body: TooltipBody,
Content: TooltipContent,
Header: TooltipHeader,
Trigger: TooltipTrigger,
});
exports.Tooltip = Tooltip;
//# sourceMappingURL=Tooltip.js.map