@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
226 lines • 13.8 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.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 = __importStar(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 SpotlightInstanceContext = (0, react_2.createContext)(null);
const useSpotlightInstance = () => {
const ctx = (0, react_2.useContext)(SpotlightInstanceContext);
if (ctx === null) {
throw new Error('Spotlight subcomponents must be used within <Spotlight />');
}
return ctx;
};
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"]);
const classes = (0, classnames_1.default)('ndl-spotlight-header', className);
const { headerId, setHeaderId } = useSpotlightInstance();
const Component = as !== null && as !== void 0 ? as : 'div';
(0, react_2.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 ((0, jsx_runtime_1.jsx)(typography_1.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 = (0, classnames_1.default)('ndl-spotlight-body', className);
const { descriptionId, setDescriptionId } = useSpotlightInstance();
const Component = as !== null && as !== void 0 ? as : 'div';
(0, react_2.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 ((0, jsx_runtime_1.jsx)(typography_1.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"]);
(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 _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 = (0, react_2.useId)();
const generatedDescriptionId = (0, react_2.useId)();
const [targetElement, setTargetElement] = (0, react_2.useState)(null);
const actionsRef = (0, react_2.useRef)(null);
const { hasOverlay, isActiveSpotlight, isOpen, registeredTargets, setActiveSpotlight, setIsOpen, } = (0, SpotlightProvider_1.useSpotlightContext)();
const [headerId, setHeaderId] = (0, react_2.useState)(generatedHeaderId);
const [descriptionId, setDescriptionId] = (0, react_2.useState)(generatedDescriptionId);
// Determine if a Header child exists
const hasHeaderChild = react_2.default.useMemo(() => {
const childrenArray = react_2.default.Children.toArray(children);
return childrenArray.some((child) => react_2.default.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 = (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)(SpotlightInstanceContext.Provider, { value: {
descriptionId,
headerId,
setDescriptionId,
setHeaderId,
}, 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(Object.assign({}, htmlAttributes), { 'aria-describedby': ariaDescribedBy, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-modal': 'true' })), 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