@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
111 lines • 6.29 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, Fragment as _Fragment, 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 { FloatingPortal } from '@floating-ui/react';
import { Resizable } from 're-resizable';
import { useCallback } from 'react';
import { classNames } from '../_common/defaultImports';
import { ConditionalWrap } from '../conditional-wrap';
import { forwardRef } from '../helpers';
import { IconButton } from '../icon-button';
import { XMarkIconOutline } from '../icons';
export const DrawerComponent = forwardRef(function DrawerComponent({ children, className = '', isExpanded, onExpandedChange, position = 'left', type = 'overlay', isResizeable = false, resizeableProps, isCloseable = true, isPortaled = false, portalProps = {}, htmlAttributes, }, ref) {
const setOnExpandedChange = useCallback(() => {
if (onExpandedChange)
onExpandedChange(!isExpanded);
}, [isExpanded, onExpandedChange]);
const classes = classNames('ndl-drawer', className, {
'ndl-drawer-expanded': isExpanded,
'ndl-drawer-left': position === 'left',
'ndl-drawer-right': position === 'right',
'ndl-drawer-overlay': type === 'overlay',
'ndl-drawer-push': type === 'push',
});
const resizableStylePosition = type === 'overlay' ? 'absolute' : 'relative';
const shouldRenderInPortal = isPortaled && type === 'overlay';
const Close = () => (_jsx(_Fragment, { children: isCloseable && (_jsx(IconButton, { className: "ndl-drawer-close-button", onClick: setOnExpandedChange, ariaLabel: "Close drawer", size: "medium", isClean: true, htmlAttributes: {
title: 'Close drawer',
'data-testid': 'ndl-drawer-close-button',
}, children: _jsx(XMarkIconOutline, {}) })) }));
if (isResizeable) {
return (_jsx(ConditionalWrap, { shouldWrap: shouldRenderInPortal, wrap: (wrapChildren) => (_jsx(FloatingPortal, Object.assign({ preserveTabOrder: true }, portalProps, { children: wrapChildren }))), children: _jsxs(Resizable, Object.assign({ defaultSize: {
width: 'auto',
height: '100%',
} }, resizeableProps, { className: classes, style: Object.assign({ position: resizableStylePosition }, resizeableProps === null || resizeableProps === void 0 ? void 0 : resizeableProps.style), boundsByDirection: true, bounds: "parent", handleStyles: {
/*
* adding a small offset to the handle to make it easier to click
* if the content of the drawer is scrollable, most noticeable if not using
* a mouse on mac or chrome on windows with the fluent scrollbars
*/
right: {
right: '-8px',
},
}, enable: {
top: false,
right: position === 'left',
bottom: false,
left: position === 'right',
topRight: false,
bottomRight: false,
bottomLeft: false,
topLeft: false,
}, ref: ref, children: [children, _jsx(Close, {})] })) }));
}
return (_jsx(ConditionalWrap, { shouldWrap: shouldRenderInPortal, wrap: (wrapChildren) => (_jsx(FloatingPortal, Object.assign({ preserveTabOrder: true }, portalProps, { children: wrapChildren }))), children: _jsxs("div", Object.assign({ className: classes, ref: ref }, htmlAttributes, { children: [children, _jsx(Close, {})] })) }));
});
const DrawerHeader = (_a) => {
var { children, className = '' } = _a, restProps = __rest(_a, ["children", "className"]);
const classes = classNames('ndl-drawer-header', className);
if (typeof children === 'string' || typeof children === 'number') {
return (_jsx("h5", Object.assign({}, restProps, { className: classes, children: children })));
}
return (_jsx("div", Object.assign({}, restProps, { className: classes, children: children })));
};
DrawerComponent.displayName = 'Drawer';
const DrawerActions = (_a) => {
var { children, className = '' } = _a, restProps = __rest(_a, ["children", "className"]);
const classes = classNames('ndl-drawer-actions', className);
return (_jsx("div", Object.assign({}, restProps, { className: classes, children: children })));
};
const DrawerBody = (_a) => {
var { children, className = '' } = _a, restProps = __rest(_a, ["children", "className"]);
const classes = classNames('ndl-drawer-body', className);
return (_jsx("div", { className: "ndl-drawer-body-wrapper", children: _jsx("div", Object.assign({}, restProps, { className: classes, children: children })) }));
};
// Issue with TypeScript forwardRef and subcomponents: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34757#issuecomment-894053907
const Drawer = Object.assign(DrawerComponent, {
Header: DrawerHeader,
Actions: DrawerActions,
Body: DrawerBody,
});
export { Drawer };
//# sourceMappingURL=Drawer.js.map