@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
141 lines • 8.91 kB
JavaScript
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 React, { useEffect, useState } from 'react';
import { Button } from '../button';
import { useIsInsideDialog } from '../dialog';
import { Label } from '../label';
import { placementTranslation } from '../popover';
import { useNeedleTheme } from '../theme';
import { Typography } from '../typography';
import { useSpotlightContext } from './SpotlightProvider';
import { useSpotlight } from './use-spotlight';
const SpotlightActions = React.forwardRef(function SpotlightActions(_a, ref) {
var { label, fill, as, style, className, htmlAttributes } = _a, restProps = __rest(_a, ["label", "fill", "as", "style", "className", "htmlAttributes"]);
if (fill === 'text') {
return (_jsx(Button, Object.assign({}, restProps, { fill: "text", size: "medium", as: as !== null && as !== void 0 ? as : 'button', ref: ref, style: style, className: className, htmlAttributes: htmlAttributes, children: label })));
}
else {
return (_jsx(Button, Object.assign({}, restProps, { fill: "outlined", size: "medium", as: as !== null && as !== void 0 ? as : 'button', ref: ref, style: style, className: className, htmlAttributes: htmlAttributes, children: label })));
}
});
const SpotlightHeader = React.forwardRef(function SpotlightHeader({ children, className, as, style, htmlAttributes, }, ref) {
useSpotlightContext();
const classes = classNames('ndl-spotlight-header', className);
return (_jsx(Typography, Object.assign({ variant: "h6", ref: ref, as: as !== null && as !== void 0 ? as : 'div', className: classes, style: style }, htmlAttributes, { children: children })));
});
const SpotlightBody = React.forwardRef(function SpotlightBody({ 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 }, htmlAttributes, { children: children })));
});
const SpotlightLabel = React.forwardRef(function SpotlightLabel({ className, children, hasIcon, as, style, htmlAttributes, }, ref) {
useSpotlightContext();
const classes = classNames('ndl-spotlight-label', className);
return (_jsx(Label, Object.assign({ fill: "semi-filled", color: "discovery", hasIcon: hasIcon, ref: ref, as: as, className: classes, style: style }, htmlAttributes, { children: children })));
});
const SpotlightImage = React.forwardRef(function Image({ 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 }, htmlAttributes)));
});
const SpotlightIconWrapper = React.forwardRef(function SpotlightIconWrapper({ 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 }, htmlAttributes, { children: children })));
});
const SpotlightComponent = React.forwardRef(function SpotlightComponent({ target, actions = [], beforeActions, initialFocus, onClose, placement, hasAnchorPortal: hasAnchorPortalProp, children, className, style, htmlAttributes, closeOnClickOutside = false, onOpen, strategy: strategyProp, }, ref) {
const [targetElement, setTargetElement] = useState(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 isInsideDialog = useIsInsideDialog();
const strategy = isInsideDialog ? 'fixed' : 'absolute';
const hasAnchorPortal = isInsideDialog;
const spotlight = useSpotlight({
anchorElement: targetElement,
anchorElementAsPortalAnchor: !!targetElement && (hasAnchorPortalProp !== null && hasAnchorPortalProp !== void 0 ? hasAnchorPortalProp : hasAnchorPortal),
closeOnClickOutside,
initialFocus,
isOpen: isActiveSpotlight(target) && isOpen && !!targetElement,
onOpenChange,
placement: placement ? placementTranslation[placement] : undefined,
strategy: strategyProp !== null && strategyProp !== void 0 ? strategyProp : strategy,
});
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]);
if (!spotlight.context.open)
return null;
return (_jsx(FloatingPortal, { root: spotlight.anchorElementAsPortalAnchor === true
? spotlight.refs.reference.current
: null, children: _jsx(FloatingFocusManager, { context: spotlight.context, modal: true, initialFocus: spotlight.initialFocus, children: _jsxs(_Fragment, { children: [hasOverlay === true && spotlight.context.open && (_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)), { children: [_jsx("div", { children: children }), (Boolean(beforeActions) || !!(actions === null || actions === void 0 ? void 0 : actions.length)) && (_jsxs("div", { className: "ndl-spotlight-footer", children: [_jsx(Typography, { variant: "body-medium", children: beforeActions }), !!(actions === null || actions === void 0 ? void 0 : actions.length) && (_jsx("div", { className: "ndl-spotlight-actions", children: actions.map((action, index) => (_jsx(SpotlightActions, Object.assign({}, action), index))) }))] }))] }))] }) }) }));
});
SpotlightComponent.displayName = 'Spotlight';
const Spotlight = Object.assign(SpotlightComponent, {
Actions: SpotlightActions,
Body: SpotlightBody,
Header: SpotlightHeader,
IconWrapper: SpotlightIconWrapper,
Image: SpotlightImage,
Label: SpotlightLabel,
});
export { Spotlight };
//# sourceMappingURL=Spotlight.js.map