UNPKG

@wonderflow/react-components

Version:

UI components from Wonderflow's Wanda design system

52 lines (51 loc) 3.14 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /* * Copyright 2022-2023 Wonderflow Design Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import tkns from '@wonderflow/tokens/platforms/web/tokens.json'; import { useKeyPress } from 'ahooks'; import clsx from 'clsx'; import { domMax, LazyMotion, m } from 'framer-motion'; import { forwardRef, } from 'react'; import { AutoFocusInside, FocusOn } from 'react-focus-on'; import { Elevator, IconButton, Stack, Text, useOverlayContext, } from '../..'; import * as styles from './drawer.module.css'; export const Drawer = forwardRef(({ children, className, closeOnClickOutside = true, showHeader = true, maxWidth = '400px', side = 'right', theme = 'light', isModal = true, title, ...otherProps }, forwardedRef) => { const { titleId, onClose } = useOverlayContext(); useKeyPress('esc', () => (!isModal && onClose) && onClose()); const dynamicStyle = { '--max-w': maxWidth, }; const DrawerAnimation = { visible: { x: 0, transition: { type: 'spring', bounce: 0, duration: parseFloat(tkns.duration[500].replace('s', '')), }, }, hidden: { x: side === 'right' ? '100%' : '-100%', transition: { type: 'spring', bounce: 0, duration: parseFloat(tkns.duration[500].replace('s', '')), }, }, }; return (_jsx("div", { role: "dialog", "aria-modal": isModal, "data-theme": theme, "aria-labelledby": titleId, className: clsx(styles.Drawer, className), ref: forwardedRef, ...otherProps, children: _jsx(FocusOn, { enabled: isModal, onClickOutside: closeOnClickOutside ? onClose : undefined, onEscapeKey: onClose, children: _jsx(LazyMotion, { features: domMax, children: _jsx(m.div, { variants: DrawerAnimation, initial: "hidden", animate: "visible", exit: "hidden", className: styles.Container, "data-drawer-side": side, children: _jsx(Elevator, { resting: 4, children: _jsxs("div", { className: styles.Content, style: dynamicStyle, ref: forwardedRef, ...otherProps, children: [(showHeader && title) && (_jsxs(Stack, { vAlign: "center", hAlign: "space-between", direction: "row", className: styles.Header, columnGap: 24, children: [_jsx(Text, { preventResponsive: true, variant: "heading-6", id: titleId, children: title }), onClose && _jsx(IconButton, { onClick: onClose, className: styles.CloseButton, icon: "xmark", kind: "flat" })] })), _jsx(AutoFocusInside, { children: children })] }) }) }) }) }) })); }); Drawer.displayName = 'Drawer';