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.

2.4 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 { CloseIcon } from '@univerjs/icons';
import { useContext } from 'react';
import { focusClassName, resetButtonClassName } from '../../helper/class-utilities';
import { clsx } from '../../helper/clsx';
import { ConfigContext } from '../config-provider/ConfigProvider';

export interface IBadgeProps {
    className?: string;
    children: ReactNode;
    closable?: boolean;
    onClose?: () => void;
}

export function Badge(props: IBadgeProps) {
    const { className, children, closable = false, onClose } = props;
    const { locale } = useContext(ConfigContext);

    return (
        <span
            data-u-comp="badge"
            className={clsx(
                `
                  univer-box-border univer-inline-flex univer-items-center univer-gap-1 univer-truncate
                  univer-rounded-md univer-border univer-border-solid univer-border-gray-100 univer-bg-gray-100
                  univer-px-2.5 univer-py-0.5 univer-text-xs univer-font-medium univer-text-gray-900
                  dark:!univer-border-gray-500 dark:!univer-bg-gray-700 dark:!univer-text-gray-300
                `,
                className
            )}
        >
            <span className="univer-flex-1 univer-truncate">
                {children}
            </span>

            {closable && (
                <button
                    className={clsx(resetButtonClassName, focusClassName, `
                      univer-rounded-sm univer-text-current univer-transition-opacity
                      hover:univer-opacity-70
                    `)}
                    type="button"
                    aria-label={locale?.Accessibility.closeBadge}
                    onClick={onClose}
                >
                    <CloseIcon className="univer-text-current" />
                </button>
            )}
        </span>
    );
}