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.7 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 { VariantProps } from 'class-variance-authority';
import type { ButtonHTMLAttributes } from 'react';
import { Slot } from '@radix-ui/react-slot';
import { cva } from 'class-variance-authority';
import { forwardRef } from 'react';
import { focusClassName } from '../../helper/class-utilities';
import { clsx } from '../../helper/clsx';

export const buttonVariants = cva(
    `
      univer-box-border univer-inline-flex univer-cursor-pointer univer-select-none univer-items-center
      univer-justify-center univer-gap-2 univer-whitespace-nowrap univer-rounded-md univer-border univer-border-solid
      univer-text-sm univer-font-medium univer-transition-colors
      disabled:univer-pointer-events-none disabled:univer-cursor-not-allowed disabled:univer-opacity-50
      [&_svg]:univer-pointer-events-none [&_svg]:univer-size-4 [&_svg]:univer-shrink-0
    `,
    {
        variants: {
            variant: {
                default: `
                  univer-border-gray-200 univer-bg-gray-0 univer-text-gray-700
                  hover:univer-bg-gray-100
                  active:univer-bg-gray-200
                  dark:!univer-border-gray-600 dark:!univer-bg-gray-700 dark:!univer-text-gray-0
                  dark:hover:!univer-bg-gray-600
                  dark:active:!univer-bg-gray-700
                `,
                primary: `
                  univer-border-primary-600 univer-bg-primary-600 univer-text-gray-0
                  hover:univer-bg-primary-500
                  active:univer-bg-primary-700
                `,
                danger: `
                  univer-border-red-600 univer-bg-red-600 univer-text-gray-0
                  hover:univer-border-red-700 hover:univer-bg-red-700
                  active:univer-border-red-800 active:univer-bg-red-800
                `,
                text: `
                  univer-border-transparent univer-bg-transparent univer-text-gray-900
                  hover:univer-bg-gray-100
                  active:univer-bg-gray-200
                  dark:!univer-text-gray-0
                  dark:hover:!univer-bg-gray-700
                  dark:active:!univer-bg-gray-600
                `,
                link: `
                  univer-border-transparent univer-bg-transparent univer-text-primary-600 univer-decoration-current
                  univer-underline-offset-4
                  hover:univer-text-primary-700 hover:univer-underline
                  active:univer-text-primary-700
                  dark:!univer-text-primary-300
                  dark:hover:!univer-text-primary-200
                  dark:active:!univer-text-primary-100
                `,
                ghost: `
                  univer-border-transparent univer-bg-transparent univer-text-gray-900
                  hover:univer-bg-gray-100
                  active:univer-bg-gray-200
                  dark:!univer-text-gray-0
                  dark:hover:!univer-bg-gray-700
                  dark:active:!univer-bg-gray-600
                `,
            },
            size: {
                icon: 'univer-size-8 !univer-p-0',
                small: 'univer-h-6 univer-rounded-md univer-px-1.5 univer-text-xs',
                middle: 'univer-h-8 univer-rounded-md univer-px-2 univer-text-sm',
                large: 'univer-h-10 univer-rounded-md univer-px-3 univer-text-sm',
                'extra-large': 'univer-h-auto univer-min-h-12 univer-rounded-md univer-px-3 univer-text-base',
            },
        },
        defaultVariants: {
            variant: 'default',
            size: 'middle',
        },
    }
);

export interface IButtonProps
    extends ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
    asChild?: boolean;
}

export const Button = forwardRef<HTMLButtonElement, IButtonProps>(
    ({ className, variant, size, asChild = false, ...props }, ref) => {
        const Comp = asChild ? Slot : 'button';
        return (
            <Comp
                className={clsx(buttonVariants({ variant, size }), focusClassName, className)}
                ref={ref}
                data-u-comp="button"
                {...props}
            />
        );
    }
);

Button.displayName = 'Button';