Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

3.8 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { ReactNode } from 'react';
import { DownIcon } from '@univerjs/icons';
import { useState } from 'react';
import { focusClassName } from '../../helper/class-utilities';
import { clsx } from '../../helper/clsx';

interface IAccordionItem {
    id?: string;
    label: ReactNode;
    children: ReactNode;
}

export interface IAccordionProps {
    className?: string;
    items: IAccordionItem[];
    defaultOpenIndex?: number | null;
    openIndex?: number | null;
    onOpenIndexChange?: (openIndex: number | null) => void;
}

export function Accordion(props: IAccordionProps) {
    const { className, defaultOpenIndex = null, items, onOpenIndexChange } = props;
    const [innerOpenIndex, setInnerOpenIndex] = useState<number | null>(defaultOpenIndex);
    const openIndex = props.openIndex ?? innerOpenIndex;

    const toggleItem = (index: number) => {
        const nextOpenIndex = openIndex === index ? null : index;
        if (props.openIndex === undefined) {
            setInnerOpenIndex(nextOpenIndex);
        }
        onOpenIndexChange?.(nextOpenIndex);
    };

    return (
        <div
            data-u-comp="accordion"
            className={clsx(`
              univer-divide-x-0 univer-divide-y univer-divide-solid univer-divide-gray-200
              dark:!univer-divide-gray-600
            `, className)}
        >
            {items.map((item, index) => (
                <div key={item.id ?? index}>
                    <button
                        className={clsx(focusClassName, `
                          univer-box-border univer-flex univer-w-full univer-cursor-pointer univer-items-center
                          univer-gap-1.5 univer-border-none univer-bg-transparent univer-p-4 univer-text-left
                          univer-text-gray-700
                          hover:univer-text-gray-900
                          dark:!univer-text-gray-200
                          dark:hover:!univer-text-gray-0
                        `)}
                        type="button"
                        onClick={() => toggleItem(index)}
                    >
                        <DownIcon
                            aria-hidden="true"
                            className={clsx('univer-size-2.5 univer-flex-shrink-0 univer-transition-transform', {
                                '-univer-rotate-90 rtl:univer-rotate-90': openIndex !== index,
                                'univer-rotate-0': openIndex === index,
                            })}
                        />
                        <span className="univer-font-medium">{item.label}</span>
                    </button>
                    <div
                        className={clsx(`
                          univer-overflow-hidden univer-transition-[max-height,opacity] univer-duration-500
                          univer-ease-in-out
                        `, {
                            'univer-max-h-screen': openIndex === index,
                            'univer-max-h-0': openIndex !== index,
                        })}
                    >
                        <div className="univer-box-border univer-px-4 univer-py-1.5">{item.children}</div>
                    </div>
                </div>
            ))}
        </div>
    );
}