import clsx from "clsx"; import React from "react"; import { Icon, IconName } from "../Icon"; import styles from "./TreeListing.module.css"; interface Tree { size: number; } export const TreeContext = React.createContext({ size: 24 }); /** * Action (the icon on the right side of the header) for a header. */ export const TreeHeaderAction = (props: { icon: IconName; onClick: (e: React.MouseEvent) => void; }) => { const tree = React.useContext(TreeContext); return (
); }; /** * Header for a listing. */ export const TreeHeader = (props: { icon?: string; text: string; action?: React.ReactNode }) => { const tree = React.useContext(TreeContext); return ( <> {props.icon !== undefined && ( )} {props.text} {props.action && {props.action}} ); }; export const TreeLeaf = (props: { level: number; height?: number; icon?: IconName; iconTitle?: string; content: string; onClick?: (e: React.MouseEvent) => void; }) => { const stylesHeader = styles[`header${props.level}`]; const height = props.height ?? 20; const style = { "--height": `${height}px` } as React.CSSProperties; return (
{props.icon !== undefined && ( )} {props.content}
); }; /** * Tree listing for hulls, modules, and charges. */ export const TreeListing = (props: { level: number; header?: React.ReactNode; height?: number; getChildren: () => React.ReactNode; }) => { const [expanded, setExpanded] = React.useState(props.header === undefined); const stylesHeader = styles[`header${props.level}`]; const stylesContent = styles[`content${props.level}`]; const height = props.height ?? 20; const style = { "--height": `${height}px` } as React.CSSProperties; let children: React.ReactNode = <>; /* Speed up rendering by not rendering children if we are collapsed. */ if (expanded) { children = props.getChildren(); } return (
{props.header !== undefined && (
setExpanded((current) => !current)} > {props.header}
)}
{children}
); };