@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
170 lines • 10.9 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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__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 button_1 = require("../button");
const dialog_1 = require("../dialog");
const label_1 = require("../label");
const popover_1 = require("../popover");
const theme_1 = require("../theme");
const typography_1 = require("../typography");
const SpotlightProvider_1 = require("./SpotlightProvider");
const use_spotlight_1 = require("./use-spotlight");
const SpotlightActions = react_2.default.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 ((0, jsx_runtime_1.jsx)(button_1.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 ((0, jsx_runtime_1.jsx)(button_1.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_2.default.forwardRef(function SpotlightHeader({ 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: "h6", ref: ref, as: as !== null && as !== void 0 ? as : 'div', className: classes, style: style }, htmlAttributes, { children: children })));
});
const SpotlightBody = react_2.default.forwardRef(function SpotlightBody({ 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 }, htmlAttributes, { children: children })));
});
const SpotlightLabel = react_2.default.forwardRef(function SpotlightLabel({ 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)(label_1.Label, Object.assign({ fill: "semi-filled", color: "discovery", hasIcon: hasIcon, ref: ref, as: as, className: classes, style: style }, htmlAttributes, { children: children })));
});
const SpotlightImage = react_2.default.forwardRef(function Image({ 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 }, htmlAttributes)));
});
const SpotlightIconWrapper = react_2.default.forwardRef(function SpotlightIconWrapper({ 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 }, htmlAttributes, { children: children })));
});
const SpotlightComponent = react_2.default.forwardRef(function SpotlightComponent({ target, actions = [], beforeActions, initialFocus, onClose, placement, hasAnchorPortal: hasAnchorPortalProp, children, className, style, htmlAttributes, closeOnClickOutside = false, onOpen, strategy: strategyProp, }, ref) {
const [targetElement, setTargetElement] = (0, react_2.useState)(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 isInsideDialog = (0, dialog_1.useIsInsideDialog)();
const strategy = isInsideDialog ? 'fixed' : 'absolute';
const hasAnchorPortal = isInsideDialog;
const spotlight = (0, use_spotlight_1.useSpotlight)({
anchorElement: targetElement,
anchorElementAsPortalAnchor: !!targetElement && (hasAnchorPortalProp !== null && hasAnchorPortalProp !== void 0 ? hasAnchorPortalProp : hasAnchorPortal),
closeOnClickOutside,
initialFocus,
isOpen: isActiveSpotlight(target) && isOpen && !!targetElement,
onOpenChange,
placement: placement ? popover_1.placementTranslation[placement] : undefined,
strategy: strategyProp !== null && strategyProp !== void 0 ? strategyProp : strategy,
});
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]);
if (!spotlight.context.open)
return null;
return ((0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { root: spotlight.anchorElementAsPortalAnchor === true
? spotlight.refs.reference.current
: null, children: (0, jsx_runtime_1.jsx)(react_1.FloatingFocusManager, { context: spotlight.context, modal: true, initialFocus: spotlight.initialFocus, children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [hasOverlay === true && spotlight.context.open && ((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)), { children: [(0, jsx_runtime_1.jsx)("div", { children: children }), (Boolean(beforeActions) || !!(actions === null || actions === void 0 ? void 0 : actions.length)) && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-spotlight-footer", children: [(0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: "body-medium", children: beforeActions }), !!(actions === null || actions === void 0 ? void 0 : actions.length) && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-spotlight-actions", children: actions.map((action, index) => ((0, jsx_runtime_1.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,
});
exports.Spotlight = Spotlight;
//# sourceMappingURL=Spotlight.js.map