Add expandable functionality to Panel component
This commit is contained in:
@@ -1,14 +1,31 @@
|
|||||||
import type { PropsWithChildren } from "react";
|
import { useId, useState, type PropsWithChildren } from "react";
|
||||||
|
|
||||||
interface PanelProps extends PropsWithChildren {
|
interface PanelProps extends PropsWithChildren {
|
||||||
title: string;
|
title: string;
|
||||||
|
defaultExpanded?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Panel({ title, children }: PanelProps) {
|
export function Panel({ title, children, defaultExpanded = true }: PanelProps) {
|
||||||
|
const [isExpanded, setIsExpanded] = useState(defaultExpanded);
|
||||||
|
const bodyId = useId();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="panel">
|
<section className={`panel ${isExpanded ? "" : "panel--collapsed"}`}>
|
||||||
<div className="panel__header">{title}</div>
|
<button
|
||||||
<div className="panel__body">{children}</div>
|
className="panel__header"
|
||||||
|
type="button"
|
||||||
|
aria-expanded={isExpanded}
|
||||||
|
aria-controls={bodyId}
|
||||||
|
onClick={() => setIsExpanded((expanded) => !expanded)}
|
||||||
|
>
|
||||||
|
<span className={`panel__chevron ${isExpanded ? "panel__chevron--expanded" : ""}`} aria-hidden="true" />
|
||||||
|
<span>{title}</span>
|
||||||
|
</button>
|
||||||
|
{isExpanded ? (
|
||||||
|
<div className="panel__body" id={bodyId}>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user