@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
187 lines • 11.6 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 } 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, { createContext, useContext, useEffect, useId, 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';
const SpotlightInstanceContext = createContext(null);
const useSpotlightInstance = () => {
const ctx = useContext(SpotlightInstanceContext);
if (ctx === null) {
throw new Error('Spotlight subcomponents must be used within <Spotlight />');
}
return ctx;
};
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"]);
const classes = classNames('ndl-spotlight-header', className);
const { headerId, setHeaderId } = useSpotlightInstance();
const Component = as !== null && as !== void 0 ? as : 'div';
useEffect(() => {
if (Boolean(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id) === true) {
setHeaderId === null || setHeaderId === void 0 ? void 0 : setHeaderId(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id);
}
}, [setHeaderId, htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id]);
return (_jsx(Typography, Object.assign({ as: Component, className: classes, ref: ref, style: style, variant: "title-4" }, restProps, { htmlAttributes: Object.assign(Object.assign({}, htmlAttributes), { id: headerId }), 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"]);
const classes = classNames('ndl-spotlight-body', className);
const { descriptionId, setDescriptionId } = useSpotlightInstance();
const Component = as !== null && as !== void 0 ? as : 'div';
useEffect(() => {
if (Boolean(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id) === true) {
setDescriptionId === null || setDescriptionId === void 0 ? void 0 : setDescriptionId(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id);
}
}, [setDescriptionId, htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id]);
return (_jsx(Typography, Object.assign({ as: Component, className: classes, ref: ref, style: style, variant: "body-medium" }, restProps, { htmlAttributes: Object.assign(Object.assign({}, htmlAttributes), { id: descriptionId }), 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 _b, _c;
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 generatedHeaderId = useId();
const generatedDescriptionId = useId();
const [targetElement, setTargetElement] = useState(null);
const actionsRef = useRef(null);
const { hasOverlay, isActiveSpotlight, isOpen, registeredTargets, setActiveSpotlight, setIsOpen, } = useSpotlightContext();
const [headerId, setHeaderId] = useState(generatedHeaderId);
const [descriptionId, setDescriptionId] = useState(generatedDescriptionId);
// Determine if a Header child exists
const hasHeaderChild = React.useMemo(() => {
const childrenArray = React.Children.toArray(children);
return childrenArray.some((child) => React.isValidElement(child) && child.type === SpotlightHeader);
}, [children]);
// Source ARIA from htmlAttributes
const ariaLabel = htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-label'];
const ariaLabelledBy = (_b = htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-labelledby']) !== null && _b !== void 0 ? _b : (hasHeaderChild ? headerId : undefined);
const ariaDescribedBy = (_c = htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-describedby']) !== null && _c !== void 0 ? _c : descriptionId;
if (ariaLabel === undefined && !ariaLabelledBy) {
console.warn('The Spotlight is missing aria-label and Header. Please add one of them for accessibility.');
}
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(SpotlightInstanceContext.Provider, { value: {
descriptionId,
headerId,
setDescriptionId,
setHeaderId,
}, 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(Object.assign({}, htmlAttributes), { 'aria-describedby': ariaDescribedBy, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-modal': 'true' })), 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