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.

4.0 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 { BooleanNumber } from '@univerjs/core';
import type { CSSProperties, KeyboardEventHandler, MouseEventHandler, ReactNode } from 'react';
import { ColorKit, ThemeService } from '@univerjs/core';
import { clsx } from '@univerjs/design';
import { useDependency } from '@univerjs/ui';

export interface IBaseSheetBarProps {
    label?: ReactNode;
    children?: unknown[];
    index?: number;
    color?: string;
    sheetId?: string;
    style?: CSSProperties;
    hidden?: BooleanNumber;
    selected?: boolean;
    menuOverlay?: ReactNode;
    className?: string;
    onKeyDown?: KeyboardEventHandler<HTMLDivElement>;
    onClick?: MouseEventHandler<HTMLDivElement>;
    tabIndex?: number;
}

export function SheetBarItem(props: IBaseSheetBarProps) {
    const { sheetId, label, color, selected, className, onKeyDown, onClick, tabIndex } = props;

    const themeService = useDependency(ThemeService);

    const getTextColor = (color: string) => {
        const darkTextColor = themeService.getColorFromTheme('gray.900');
        const lightTextColor = themeService.getColorFromTheme('gray.0');
        return new ColorKit(color).isDark() ? lightTextColor : darkTextColor;
    };

    const currentSelected = Boolean(selected);
    const textColor = color ? getTextColor(color) : '';

    return (
        <div
            data-u-comp="slide-tab-item"
            key={sheetId}
            data-id={sheetId}
            role="tab"
            aria-selected={currentSelected}
            tabIndex={tabIndex ?? (currentSelected ? 0 : -1)}
            onKeyDown={onKeyDown}
            onClick={onClick}
            className={clsx(`
              univer-relative univer-mx-1 univer-box-border univer-flex univer-flex-grow univer-cursor-pointer
              univer-select-none univer-flex-row univer-items-center univer-justify-center univer-rounded univer-text-xs
              univer-font-medium univer-transition-[colors,box-shadow]
              focus-visible:univer-ring-2 focus-visible:univer-ring-primary-500
            `, {
                'dark:!univer-text-gray-0': !color || (color && !textColor),
                'univer-bg-gray-0 univer-text-primary-700 univer-shadow': currentSelected,
                'dark:!univer-bg-gray-700': currentSelected && !color,
                'univer-text-gray-900 hover:univer-bg-gray-100': !currentSelected,
                'dark:hover:!univer-bg-gray-700': !currentSelected && !color,
            }, className)}
            style={{
                backgroundColor: !currentSelected && color ? color : '',
                color: !currentSelected && color ? textColor : '',
                boxShadow: currentSelected && color ? '0px 0px 8px rgba(0, 0, 0, 0.08)' : '',
            }}
        >
            <div
                className={`
                  univer-box-border univer-flex univer-max-w-lg univer-items-center univer-gap-1 univer-overflow-hidden
                  univer-whitespace-nowrap univer-rounded univer-border-2 univer-border-solid univer-border-transparent
                  univer-px-1.5 univer-py-1
                `}
            >
                {label}
            </div>
            {currentSelected && color && (
                <div
                    aria-hidden
                    className="univer-pointer-events-none univer-absolute univer-inset-x-2 univer-bottom-0 univer-h-0.5"
                    style={{ backgroundColor: color }}
                />
            )}
        </div>
    );
}