@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
196 lines • 11.6 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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 = __importStar(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, followCursor, }) => {
const isInsideDialog = (0, dialog_context_1.useIsInsideDialog)();
const strategy = isInsideDialog ? 'fixed' : 'absolute';
const isPortaled = !isInsideDialog;
const tooltip = (0, use_tooltip_1.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 ((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
// oxlint-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 && react_2.default.isValidElement(children)) {
const props = Object.assign(Object.assign(Object.assign({ className: triggerClasses }, htmlAttributes), childrenProps), { ref: mergedRefs });
return react_2.default.cloneElement(children, context.getReferenceProps(props));
}
return ((0, jsx_runtime_1.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 = (0, use_tooltip_1.useTooltipContext)();
const mergedRef = (0, react_1.useMergeRefs)([context.refs.setFloating, ref]);
const { themeClassName } = (0, theme_1.useNeedleTheme)();
const hasHeaderChild = react_2.default.useMemo(() => {
if (context.type !== 'rich') {
return false;
}
const childrenArray = react_2.default.Children.toArray(children);
return childrenArray.some((child) => react_2.default.isValidElement(child) && child.type === TooltipHeader);
}, [children, context.type]);
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 }) })) }));
}
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 ((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: 0, 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(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 = (0, use_tooltip_1.useTooltipContext)();
const classes = (0, classnames_1.default)('ndl-tooltip-header', className);
(0, react_2.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 ((0, jsx_runtime_1.jsx)(typography_1.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 = (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 })));
};
Actions.displayName = 'Tooltip.Actions';
const Tooltip = Object.assign(TooltipComponent, {
Actions: Actions,
Body: TooltipBody,
Content: TooltipContent,
Header: TooltipHeader,
Trigger: TooltipTrigger,
});
exports.Tooltip = Tooltip;
//# sourceMappingURL=Tooltip.js.map