@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
106 lines • 6.92 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 classNames from 'classnames';
import { createContext, useContext, useRef, useState } from 'react';
import { ChevronDownIconOutline } from '../icons/generated/heroIcons/ChevronDownIconOutline';
import { Tooltip } from '../tooltip';
const BreadcrumbsComponent = (_a) => {
var { children, className, htmlAttributes, style, ref, shouldWrap = true } = _a, restProps = __rest(_a, ["children", "className", "htmlAttributes", "style", "ref", "shouldWrap"]);
const classes = classNames('ndl-breadcrumbs', className);
const listClasses = classNames('ndl-breadcrumbs-list', {
'ndl-breadcrumbs-wrap': shouldWrap,
});
return (_jsx("nav", Object.assign({ "aria-label": "Breadcrumb", ref: ref, className: classes, style: style }, restProps, htmlAttributes, { children: _jsx("ol", { className: listClasses, children: children }) })));
};
const BreadcrumbsItemContext = createContext(null);
export const useBreadcrumbsItemContext = () => {
const context = useContext(BreadcrumbsItemContext);
if (!context) {
throw new Error('This component must be used within a <Breadcrumbs.Item>');
}
return context;
};
const BreadcrumbsItem = (_a) => {
var { children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["children", "className", "style", "htmlAttributes", "ref"]);
const classes = classNames('ndl-breadcrumbs-item', className);
return (_jsx(BreadcrumbsItemContext.Provider, { value: { isInBreadcrumbsItem: true }, children: _jsx("li", Object.assign({ ref: ref, className: classes, style: style }, htmlAttributes, restProps, { children: _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"]);
useBreadcrumbsItemContext();
const [isTooltipOpen, setIsTooltipOpen] = useState(false);
const contentRef = useRef(null);
const Component = as !== null && as !== void 0 ? as : 'button';
const classes = classNames('ndl-breadcrumbs-button ndl-breadcrumbs-sibling-hover', className);
return (_jsxs(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: [_jsx(Tooltip.Trigger, { hasButtonWrapper: true, children: _jsxs(Component, Object.assign({ ref: ref, className: classes, style: style }, htmlAttributes, restProps, { children: [Boolean(leadingElement) && (_jsx("div", { className: "ndl-breadcrumbs-button-leading-element", children: leadingElement })), _jsx("div", { className: "ndl-breadcrumbs-button-content", ref: contentRef, children: children }), Boolean(trailingElement) && (_jsx("div", { className: "ndl-breadcrumbs-button-trailing-element", children: trailingElement }))] })) }), _jsx(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"]);
useBreadcrumbsItemContext();
const classes = classNames('ndl-breadcrumbs-button ndl-breadcrumbs-sibling-hover ndl-breadcrumbs-select', className, {
'ndl-active': Boolean(isActive),
});
return (_jsxs("button", Object.assign({ type: "button", ref: ref, className: classes, style: style, onClick: onClick, "aria-label": ariaLabel, "aria-pressed": isActive }, htmlAttributes, restProps, { children: [_jsx("div", { className: "ndl-breadcrumbs-button-leading-element", children: _jsx(ChevronDownIconOutline, {}) }), _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"]);
useBreadcrumbsItemContext();
const Component = as !== null && as !== void 0 ? as : 'a';
const classes = classNames('ndl-breadcrumbs-link', {
'ndl-breadcrumbs-link-current': Boolean(ariaCurrent),
}, className);
return (_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,
});
export { Breadcrumbs };
//# sourceMappingURL=Breadcrumbs.js.map