UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

145 lines 8.55 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, jsxs as _jsxs, Fragment as _Fragment } 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, FloatingOverlay, FloatingPortal, useMergeRefs, } from '@floating-ui/react'; import classNames from 'classnames'; import { useEffect, useRef, useState } from 'react'; import { placementTranslation } from '../popover'; import { StatusLabel } from '../status-label'; import { useNeedleTheme } from '../theme'; import { Typography } from '../typography'; import { useSpotlightContext } from './SpotlightProvider'; import { useSpotlight } from './use-spotlight'; const SpotlightHeader = (_a) => { var { children, className, as, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "as", "style", "htmlAttributes", "ref"]); useSpotlightContext(); const classes = classNames('ndl-spotlight-header', className); return (_jsx(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"]); useSpotlightContext(); const classes = classNames('ndl-spotlight-body', className); return (_jsx(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"]); useSpotlightContext(); const classes = classNames('ndl-spotlight-label', className); return (_jsx(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"]); useSpotlightContext(); const classes = classNames('ndl-spotlight-image n-body-medium', className); return (_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"]); useSpotlightContext(); const classes = classNames('ndl-spotlight-icon-wrapper', className); const Component = as !== null && as !== void 0 ? as : 'div'; return (_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] = useState(null); const actionsRef = useRef(null); const { isActiveSpotlight, isOpen, setActiveSpotlight, setIsOpen, registeredTargets, hasOverlay, } = 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 = useSpotlight({ anchorElement: targetElement, closeOnClickOutside, initialFocus, isOpen: isActiveSpotlight(target) && isOpen && !!targetElement, onOpenChange, placement: placement ? placementTranslation[placement] : undefined, }); const mergedRef = useMergeRefs([spotlight.refs.setFloating, ref]); const { themeClassName } = useNeedleTheme(); const classes = classNames('ndl-spotlight', themeClassName, className); useEffect(() => { var _a; setTargetElement((_a = registeredTargets[target]) === null || _a === void 0 ? void 0 : _a.current); }, [target, registeredTargets]); 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 (_jsx(FloatingPortal, { children: _jsx(FloatingFocusManager, { context: spotlight.context, modal: true, initialFocus: findInitialFocus(), children: _jsxs(_Fragment, { children: [hasOverlay === true && (_jsx(FloatingOverlay, { className: "ndl-spotlight-overlay ndl-spotlight-overlay-top", onPointerDown: (e) => { e.stopPropagation(); e.preventDefault(); }, onTouchStart: (e) => { e.stopPropagation(); e.preventDefault(); } })), _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: [_jsx("div", { children: children }), (Boolean(beforeActions) || Boolean(actions)) && (_jsxs("div", { className: "ndl-spotlight-footer", children: [_jsx(Typography, { variant: "body-medium", children: beforeActions }), Boolean(actions) && (_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, }); export { Spotlight }; //# sourceMappingURL=Spotlight.js.map