Skip to content

Build your personal knowledge base with Trilium Notes

3.5 KB
Raw Blame
import "./Collapsible.css";

import clsx from "clsx";
import { ComponentChildren, HTMLAttributes } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";

import { useElementSize, useUniqueName } from "./hooks";
import Icon from "./Icon";

interface CollapsibleProps extends Pick<HTMLAttributes<HTMLDivElement>, "className"> {
    title: string;
    children: ComponentChildren;
    initiallyExpanded?: boolean;
    /** Shown beside the title, also while collapsed; not part of the button that toggles. */
    headerActions?: ComponentChildren;
    /** Draws the expanded body on a card of its own, apart from what surrounds it. */
    card?: boolean;
}

export default function Collapsible({ initiallyExpanded, ...restProps }: CollapsibleProps) {
    const [ expanded, setExpanded ] = useState(initiallyExpanded);
    return <ExternallyControlledCollapsible {...restProps} expanded={expanded} setExpanded={setExpanded} />;
}

export function ExternallyControlledCollapsible({
    title, children, className, expanded, setExpanded, headerActions, card
}: Omit<CollapsibleProps, "initiallyExpanded"> & {
    expanded: boolean | undefined;
    setExpanded: (expanded: boolean) => void
}) {
    const bodyRef = useRef<HTMLDivElement>(null);
    const innerRef = useRef<HTMLDivElement>(null);
    const { height } = useElementSize(innerRef) ?? {};
    const contentId = useUniqueName();
    const [ transitionEnabled, setTransitionEnabled ] = useState(false);
    const [ fullyExpanded, setFullyExpanded ] = useState(false);

    useEffect(() => {
        const timeout = setTimeout(() => {
            setTransitionEnabled(true);
        }, 200);
        return () => clearTimeout(timeout);
    }, []);

    useEffect(() => {
        if (expanded) {
            if (transitionEnabled) {
                const timeout = setTimeout(() => {
                    setFullyExpanded(true);
                }, 250);
                return () => clearTimeout(timeout);
            } 
            setFullyExpanded(true);
            
        } else {
            setFullyExpanded(false);
        }
    }, [expanded, transitionEnabled]);

    const titleButton = (
        <button
            className="collapsible-title tn-low-profile"
            onClick={(e) => {
                e.preventDefault();
                setExpanded(!expanded);
            }}
            aria-expanded={expanded}
            aria-controls={contentId}
        >
            <Icon className="arrow" icon="bx bx-chevron-right" />&nbsp;
            {title}
        </button>
    );

    return (
        <div className={clsx("collapsible", className, {
            expanded,
            "with-transition": transitionEnabled,
            "collapsible-card": card
        })}>
            {headerActions ? (
                <div className="collapsible-header">
                    {titleButton}
                    <div className="collapsible-header-actions">{headerActions}</div>
                </div>
            ) : titleButton}

            <div
                id={contentId}
                ref={bodyRef}
                className={clsx("collapsible-body", {"fully-expanded": fullyExpanded})}
                style={{
                    height: expanded ? (height === undefined ? undefined : `${height}px`) : "0"
                }}
                aria-hidden={!expanded}
            >
                <div
                    ref={innerRef}
                    className="collapsible-inner-body"
                >
                    {children}
                </div>
            </div>
        </div>
    );
}