UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

113 lines 7.8 kB
"use strict"; 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.Breadcrumbs = exports.useBreadcrumbsItemContext = 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 classnames_1 = __importDefault(require("classnames")); const react_1 = require("react"); const ChevronDownIconOutline_1 = require("../icons/generated/heroIcons/ChevronDownIconOutline"); const tooltip_1 = require("../tooltip"); const BreadcrumbsComponent = (_a) => { var { children, className, htmlAttributes, style, ref, shouldWrap = true } = _a, restProps = __rest(_a, ["children", "className", "htmlAttributes", "style", "ref", "shouldWrap"]); const classes = (0, classnames_1.default)('ndl-breadcrumbs', className); const listClasses = (0, classnames_1.default)('ndl-breadcrumbs-list', { 'ndl-breadcrumbs-wrap': shouldWrap, }); return ((0, jsx_runtime_1.jsx)("nav", Object.assign({ "aria-label": "Breadcrumb", ref: ref, className: classes, style: style }, restProps, htmlAttributes, { children: (0, jsx_runtime_1.jsx)("ol", { className: listClasses, children: children }) }))); }; const BreadcrumbsItemContext = (0, react_1.createContext)(null); const useBreadcrumbsItemContext = () => { const context = (0, react_1.useContext)(BreadcrumbsItemContext); if (!context) { throw new Error('This component must be used within a <Breadcrumbs.Item>'); } return context; }; exports.useBreadcrumbsItemContext = useBreadcrumbsItemContext; const BreadcrumbsItem = (_a) => { var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]); const classes = (0, classnames_1.default)('ndl-breadcrumbs-item', className); return ((0, jsx_runtime_1.jsx)(BreadcrumbsItemContext.Provider, { value: { isInBreadcrumbsItem: true }, children: (0, jsx_runtime_1.jsx)("li", Object.assign({ ref: ref, className: classes, style: style }, htmlAttributes, restProps, { children: (0, jsx_runtime_1.jsx)("div", { className: "ndl-breadcrumbs-item-inner", children: children }) })) })); }; const BreadcrumbsButton = (_a) => { var { children, as, className, style, htmlAttributes, ref, leadingElement, trailingElement } = _a, restProps = __rest(_a, ["children", "as", "className", "style", "htmlAttributes", "ref", "leadingElement", "trailingElement"]); (0, exports.useBreadcrumbsItemContext)(); const [isTooltipOpen, setIsTooltipOpen] = (0, react_1.useState)(false); const contentRef = (0, react_1.useRef)(null); const Component = as !== null && as !== void 0 ? as : 'button'; const classes = (0, classnames_1.default)('ndl-breadcrumbs-button ndl-breadcrumbs-sibling-hover', className); return ((0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, { type: "simple", isOpen: isTooltipOpen, onOpenChange: (isOpen) => { var _a, _b, _c, _d; if (isOpen) { const isTruncated = ((_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.scrollWidth) && ((_b = contentRef.current) === null || _b === void 0 ? void 0 : _b.clientWidth) && contentRef.current.scrollWidth > contentRef.current.clientWidth; const isVisible = ((_c = contentRef.current) === null || _c === void 0 ? void 0 : _c.offsetWidth) && ((_d = contentRef.current) === null || _d === void 0 ? void 0 : _d.offsetHeight) && contentRef.current.offsetWidth > 0 && contentRef.current.offsetHeight > 0; setIsTooltipOpen(isTruncated || !isVisible); } else { setIsTooltipOpen(false); } }, children: [(0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Trigger, { hasButtonWrapper: true, children: (0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: ref, className: classes, style: style }, htmlAttributes, restProps, { children: [Boolean(leadingElement) && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-breadcrumbs-button-leading-element", children: leadingElement })), (0, jsx_runtime_1.jsx)("div", { className: "ndl-breadcrumbs-button-content", ref: contentRef, children: children }), Boolean(trailingElement) && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-breadcrumbs-button-trailing-element", children: trailingElement }))] })) }), (0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Content, { children: children })] })); }; const BreadcrumbsSelectButton = (_a) => { var { onClick, children, isActive, className, style, htmlAttributes, ariaLabel, ref } = _a, restProps = __rest(_a, ["onClick", "children", "isActive", "className", "style", "htmlAttributes", "ariaLabel", "ref"]); (0, exports.useBreadcrumbsItemContext)(); const classes = (0, classnames_1.default)('ndl-breadcrumbs-button ndl-breadcrumbs-sibling-hover ndl-breadcrumbs-select', className, { 'ndl-active': Boolean(isActive), }); return ((0, jsx_runtime_1.jsxs)("button", Object.assign({ type: "button", ref: ref, className: classes, style: style, onClick: onClick, "aria-label": ariaLabel, "aria-pressed": isActive }, htmlAttributes, restProps, { children: [(0, jsx_runtime_1.jsx)("div", { className: "ndl-breadcrumbs-button-leading-element", children: (0, jsx_runtime_1.jsx)(ChevronDownIconOutline_1.ChevronDownIconOutline, {}) }), (0, jsx_runtime_1.jsx)("div", { className: "ndl-breadcrumbs-button-content", children: children })] }))); }; const BreadcrumbsLink = (_a) => { var { children, as, className, style, htmlAttributes, ariaCurrent = false, ref } = _a, restProps = __rest(_a, ["children", "as", "className", "style", "htmlAttributes", "ariaCurrent", "ref"]); (0, exports.useBreadcrumbsItemContext)(); const Component = as !== null && as !== void 0 ? as : 'a'; const classes = (0, classnames_1.default)('ndl-breadcrumbs-link', { 'ndl-breadcrumbs-link-current': Boolean(ariaCurrent), }, className); return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ ref: ref, className: classes, style: style, "aria-current": ariaCurrent }, restProps, htmlAttributes, { children: children }))); }; const Breadcrumbs = Object.assign(BreadcrumbsComponent, { Button: BreadcrumbsButton, Item: BreadcrumbsItem, Link: BreadcrumbsLink, SelectButton: BreadcrumbsSelectButton, }); exports.Breadcrumbs = Breadcrumbs; //# sourceMappingURL=Breadcrumbs.js.map