Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

closing-positioner.tsx

18 lines893 Bytes

1import { useState, type ComponentProps, type CSSProperties } from "react";
2​
3const PLACEMENT_KEYS = ["position", "top", "right", "bottom", "left", "transform"] as const;
4​
5type Placement = Pick<CSSProperties, (typeof PLACEMENT_KEYS)[number]>;
6​
7function ClosingPositioner({ open, style, ...props }: ComponentProps<"div"> & { open: boolean }) {
8 const [openPlacement, setOpenPlacement] = useState<Placement>({});
9 if (open && style && PLACEMENT_KEYS.some((key) => style[key] !== openPlacement[key])) {
10 const { position, top, right, bottom, left, transform } = style;
11 setOpenPlacement({ position, top, right, bottom, left, transform });
12 }
13 return <div {...props} style={open ? style : { ...style, ...openPlacement }} />;
15​
16export function renderClosingPositioner(props: ComponentProps<"div">, state: { open: boolean }) {
17 return <ClosingPositioner {...props} open={state.open} />;