Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/ui/src/stories/Popover.stories.tsx
T

105 lines
3.2 KiB
TypeScript

import type { Meta, StoryObj } from "@storybook/react";
import { useEffect, useRef, useState } from "react";
import { DemoButton } from "./_demo-primitives.js";
const meta: Meta = {
title: "UI / Overlays / Popover",
};
export default meta;
type Story = StoryObj;
const menuButtonStyle = {
background: "transparent",
border: 0,
color: "inherit",
cursor: "pointer",
font: "inherit",
padding: 0,
textAlign: "left",
} satisfies React.CSSProperties;
function Popover({
trigger,
children,
}: {
trigger: React.ReactNode;
children: React.ReactNode;
}) {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
const handler = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) setOpen(false);
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, [open]);
return (
<div ref={ref} style={{ position: "relative", display: "inline-block" }}>
{/* trigger is always a focusable DemoButton; its keyboard activation bubbles a
click to this span, so the span needs no separate role/key handler. */}
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */}
<span onClick={() => setOpen((prev) => !prev)}>{trigger}</span>
{open ? (
<div
role="dialog"
style={{
position: "absolute",
top: "calc(100% + 6px)",
left: 0,
minWidth: 220,
background: "var(--color-card)",
color: "var(--color-card-foreground)",
border: "1px solid var(--color-border)",
borderRadius: 6,
padding: 12,
boxShadow: "0 8px 24px rgba(0,0,0,0.18)",
fontSize: 14,
zIndex: 5,
}}
>
{children}
</div>
) : null}
</div>
);
}
export const SimplePopover: Story = {
name: "Simple popover",
render: () => (
<Popover
trigger={<DemoButton variant="outline">Open menu</DemoButton>}
>
<ul style={{ margin: 0, padding: 0, listStyle: "none", display: "flex", flexDirection: "column", gap: 6 }}>
<li><button type="button" style={menuButtonStyle}>Profile</button></li>
<li><button type="button" style={menuButtonStyle}>Settings</button></li>
<li><button type="button" style={{ ...menuButtonStyle, color: "var(--color-destructive)" }}>Sign out</button></li>
</ul>
</Popover>
),
};
export const RichContent: Story = {
name: "Rich content",
render: () => (
<Popover trigger={<DemoButton>Filters</DemoButton>}>
<div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
<strong>Status</strong>
<label style={{ display: "flex", gap: 6 }}>
<input type="checkbox" defaultChecked /> Active
</label>
<label style={{ display: "flex", gap: 6 }}>
<input type="checkbox" /> Pending
</label>
<label style={{ display: "flex", gap: 6 }}>
<input type="checkbox" /> Archived
</label>
</div>
</Popover>
),
};