/** * React Fabric * @version 1.0.0 * @license MIT * @copyright 2024 Adarsh Pastakia * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { cn } from "@react-fabric/utilities"; import { Activity, useEffect, useLayoutEffect, useState } from "react"; import { useGlobals } from "../../context/context"; import { usePropToggle } from "../../hooks/usePropToggle"; import { useResize } from "../../hooks/useResize"; import type { ChildrenProp, CollapseProps, CssProp, HeightProps, TestProps } from "../../types"; interface BaseProps extends CssProp, ChildrenProp, TestProps, CollapseProps, HeightProps { flex?: boolean; align?: "start" | "end" | "center" | "stretch"; justify?: "start" | "end" | "center" | "between"; id?: string; hideCollapseHandle?: boolean; /** * resizable header/footer */ resizeable?: boolean; /** * Resize event */ onResize?: (height: number) => void; } type DefaultProps = { flex?: false; align?: never; justify?: never }; type FlexProps = { flex: true; }; export type HeadFootProps = BaseProps & (DefaultProps | FlexProps); const ALIGN_MAP = { start: "items-start", end: "items-end", center: "items-center", stretch: "items-stretch", }; const JUSTIFY_MAP = { start: "justify-start", end: "justify-end", center: "justify-center", between: "justify-between", }; function HeadFoot({ as: E, id, children, className, flex, height: ht, maxHeight = "50vh", minHeight = "10px", collapsed: _collapsed, collapsable, resizeable, align = "center", justify = "start", hideCollapseHandle, onCollapse, onResize, ...props }: HeadFootProps & { as: "header" | "footer" }) { const { registerArea, unregisterArea } = useGlobals(); const [height, setHeight] = useState(); const [collapsed, onToggle] = usePropToggle(_collapsed, onCollapse); const { ref, isResizing, onMouseDown } = useResize( ({ y }) => { setHeight((h = 0) => h + y); }, { isVertical: true, isReverse: E === "footer", onEnd() { const height = (ref.current?.previousElementSibling as HTMLElement)?.offsetHeight; setHeight(height); onResize?.(height ?? 0); }, }, ); useLayoutEffect(() => { const height = (ref.current?.previousElementSibling as HTMLElement)?.offsetHeight; if (resizeable && height > 0) { const tmr = setTimeout(() => setHeight(height), 50); return () => clearTimeout(tmr); } }, [ref, resizeable]); useEffect(() => { if (id) { registerArea(id, (open) => { onToggle(open); }); return () => unregisterArea(id); } // eslint-disable-next-line @eslint-react/exhaustive-deps }, [id]); return (
{children}
{collapsable && !hideCollapseHandle && (
e.stopPropagation()} onClick={() => onToggle()} >
)} ); } /** * Header component wrapping children in a header element. Delegates to HeadFoot with `as="header"`. */ export function Header(props: HeadFootProps) { return ; } /** * Footer component wrapping children in a footer element. Delegates to HeadFoot with `as="footer"`. */ export function Footer(props: HeadFootProps) { return ; }