UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

151 lines 9.4 kB
"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.Spotlight = 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 popover_1 = require("../popover"); const status_label_1 = require("../status-label"); const theme_1 = require("../theme"); const typography_1 = require("../typography"); const SpotlightProvider_1 = require("./SpotlightProvider"); const use_spotlight_1 = require("./use-spotlight"); const SpotlightHeader = (_a) => { var { children, className, as, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "as", "style", "htmlAttributes", "ref"]); (0, SpotlightProvider_1.useSpotlightContext)(); const classes = (0, classnames_1.default)('ndl-spotlight-header', className); return ((0, jsx_runtime_1.jsx)(typography_1.Typography, Object.assign({ variant: "title-4", ref: ref, as: as !== null && as !== void 0 ? as : 'div', className: classes, style: style }, restProps, htmlAttributes, { children: children }))); }; SpotlightHeader.displayName = 'Spotlight.Header'; const SpotlightBody = (_a) => { var { children, className, as, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "as", "style", "htmlAttributes", "ref"]); (0, SpotlightProvider_1.useSpotlightContext)(); const classes = (0, classnames_1.default)('ndl-spotlight-body', className); return ((0, jsx_runtime_1.jsx)(typography_1.Typography, Object.assign({ variant: "body-medium", className: classes, as: as !== null && as !== void 0 ? as : 'div', style: style, ref: ref }, restProps, htmlAttributes, { children: children }))); }; SpotlightBody.displayName = 'Spotlight.Body'; const SpotlightLabel = (_a) => { var { className, children, hasIcon, as, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["className", "children", "hasIcon", "as", "style", "htmlAttributes", "ref"]); (0, SpotlightProvider_1.useSpotlightContext)(); const classes = (0, classnames_1.default)('ndl-spotlight-label', className); return ((0, jsx_runtime_1.jsx)(status_label_1.StatusLabel, Object.assign({ fill: "semi-filled", variant: "discovery", hasIcon: hasIcon, ref: ref, as: as, className: classes, style: style }, restProps, htmlAttributes, { children: children }))); }; SpotlightLabel.displayName = 'Spotlight.Label'; const SpotlightImage = (_a) => { var { src, alt, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["src", "alt", "className", "style", "htmlAttributes", "ref"]); (0, SpotlightProvider_1.useSpotlightContext)(); const classes = (0, classnames_1.default)('ndl-spotlight-image n-body-medium', className); return ((0, jsx_runtime_1.jsx)("img", Object.assign({ src: src, alt: alt, ref: ref, className: classes, style: style }, restProps, htmlAttributes))); }; SpotlightImage.displayName = 'Spotlight.Image'; const SpotlightIconWrapper = (_a) => { var { children, className, as, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "as", "style", "htmlAttributes", "ref"]); (0, SpotlightProvider_1.useSpotlightContext)(); const classes = (0, classnames_1.default)('ndl-spotlight-icon-wrapper', className); const Component = as !== null && as !== void 0 ? as : 'div'; return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ ref: ref, className: classes, style: style }, restProps, htmlAttributes, { children: children }))); }; SpotlightIconWrapper.displayName = 'Spotlight.IconWrapper'; const SpotlightComponent = (_a) => { var { target, beforeActions, actions, initialFocus, onClose, placement, children, className, style, htmlAttributes, closeOnClickOutside = false, onOpen, ref } = _a, restProps = __rest(_a, ["target", "beforeActions", "actions", "initialFocus", "onClose", "placement", "children", "className", "style", "htmlAttributes", "closeOnClickOutside", "onOpen", "ref"]); const [targetElement, setTargetElement] = (0, react_2.useState)(null); const actionsRef = (0, react_2.useRef)(null); const { isActiveSpotlight, isOpen, setActiveSpotlight, setIsOpen, registeredTargets, hasOverlay, } = (0, SpotlightProvider_1.useSpotlightContext)(); const onOpenChange = (isOpen, event) => { if (!isOpen) { if (event instanceof PointerEvent) { onClose === null || onClose === void 0 ? void 0 : onClose('clickOutside'); } else if (event instanceof KeyboardEvent) { onClose === null || onClose === void 0 ? void 0 : onClose('escapeKeyDown'); } setActiveSpotlight(null); setIsOpen(false); } }; const spotlight = (0, use_spotlight_1.useSpotlight)({ anchorElement: targetElement, closeOnClickOutside, initialFocus, isOpen: isActiveSpotlight(target) && isOpen && !!targetElement, onOpenChange, placement: placement ? popover_1.placementTranslation[placement] : undefined, }); const mergedRef = (0, react_1.useMergeRefs)([spotlight.refs.setFloating, ref]); const { themeClassName } = (0, theme_1.useNeedleTheme)(); const classes = (0, classnames_1.default)('ndl-spotlight', themeClassName, className); (0, react_2.useEffect)(() => { var _a; setTargetElement((_a = registeredTargets[target]) === null || _a === void 0 ? void 0 : _a.current); }, [target, registeredTargets]); (0, react_2.useEffect)(() => { if (isOpen && isActiveSpotlight(target)) { onOpen === null || onOpen === void 0 ? void 0 : onOpen(); } }, [isOpen, onOpen, isActiveSpotlight, target]); const findInitialFocus = () => { if (spotlight.initialFocus !== undefined) { return spotlight.initialFocus; } if (!actionsRef.current) { return undefined; } const focusableEls = actionsRef.current.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'); return focusableEls.length - 1; }; if (!spotlight.context.open) { return null; } return ((0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { children: (0, jsx_runtime_1.jsx)(react_1.FloatingFocusManager, { context: spotlight.context, modal: true, initialFocus: findInitialFocus(), children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [hasOverlay === true && ((0, jsx_runtime_1.jsx)(react_1.FloatingOverlay, { className: "ndl-spotlight-overlay ndl-spotlight-overlay-top", onPointerDown: (e) => { e.stopPropagation(); e.preventDefault(); }, onTouchStart: (e) => { e.stopPropagation(); e.preventDefault(); } })), (0, jsx_runtime_1.jsxs)("div", Object.assign({ ref: mergedRef, className: classes, style: Object.assign(Object.assign(Object.assign({}, spotlight.floatingStyles), spotlight.transitionStyles), style) }, spotlight.getFloatingProps(Object.assign({}, htmlAttributes)), restProps, { children: [(0, jsx_runtime_1.jsx)("div", { children: children }), (Boolean(beforeActions) || Boolean(actions)) && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-spotlight-footer", children: [(0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: "body-medium", children: beforeActions }), Boolean(actions) && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-spotlight-actions", ref: actionsRef, children: actions }))] }))] }))] }) }) })); }; SpotlightComponent.displayName = 'Spotlight'; const Spotlight = Object.assign(SpotlightComponent, { Body: SpotlightBody, Header: SpotlightHeader, IconWrapper: SpotlightIconWrapper, Image: SpotlightImage, Label: SpotlightLabel, }); exports.Spotlight = Spotlight; //# sourceMappingURL=Spotlight.js.map