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.

6.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 { ComponentPropsWithoutRef, ComponentRef, HTMLAttributes } from 'react';
import { Close, Content, Description, Overlay, Portal, Root, Title, Trigger } from '@radix-ui/react-dialog';
import { CloseIcon } from '@univerjs/icons';
import { forwardRef, useContext } from 'react';
import { borderClassName } from '../../helper/class-utilities';
import { clsx } from '../../helper/clsx';
import { ConfigContext } from '../config-provider/ConfigProvider';

const Dialog = Root;

const DialogTrigger = Trigger;

const DialogPortal = Portal;

const DialogClose = Close;

const DialogOverlay = forwardRef<
    ComponentRef<typeof Overlay>,
    ComponentPropsWithoutRef<typeof Overlay>
>(({ className, ...props }, ref) => (
    <Overlay
        ref={ref}
        className={clsx(
            `
              data-[state=open]:univer-animate-in data-[state=open]:univer-fade-in-0
              data-[state=closed]:univer-animate-out data-[state=closed]:univer-fade-out-0
              univer-fixed univer-inset-0 univer-z-[1080] univer-bg-[rgba(0,0,0,0.8)]
            `,
            className
        )}
        {...props}
    />
));
DialogOverlay.displayName = Overlay.displayName;

export interface IDialogContentProps {
    closable?: boolean;
    onClickClose?: () => void;
    mountContainer?: HTMLElement | null;
    overlayClassName?: string;
}
const DialogContent = forwardRef<
    ComponentRef<typeof Content>,
    ComponentPropsWithoutRef<typeof Content> & IDialogContentProps
>(({ className, children, closable = true, onClickClose, mountContainer, overlayClassName, ...props }, ref) => {
    const { locale } = useContext(ConfigContext);

    return (
        <DialogPortal container={mountContainer ?? undefined}>
            <DialogOverlay className={overlayClassName} />
            <Content
                ref={ref}
                className={clsx(
                    `
                      data-[state=open]:univer-animate-in data-[state=open]:univer-fade-in-0
                      data-[state=open]:univer-zoom-in-95 data-[state=open]:univer-slide-in-from-left-1/2
                      data-[state=open]:univer-slide-in-from-top-[48%]
                      data-[state=closed]:univer-animate-out data-[state=closed]:univer-fade-out-0
                      data-[state=closed]:univer-zoom-out-95 data-[state=closed]:univer-slide-out-to-left-1/2
                      data-[state=closed]:univer-slide-out-to-top-[48%]
                      univer-fixed univer-left-1/2 univer-top-1/2 univer-z-[1080] univer-box-border univer-grid
                      univer-w-full univer-max-w-lg -univer-translate-x-1/2 -univer-translate-y-1/2 univer-gap-4
                      univer-bg-gray-0 univer-p-4 univer-text-gray-500 univer-shadow-md univer-duration-200
                      sm:!univer-rounded-lg
                      dark:!univer-bg-gray-700 dark:!univer-text-gray-400
                    `,
                    borderClassName,
                    className
                )}
                {...props}
            >
                {children}
                {closable && (
                    <Close
                        data-slot="close"
                        className={`
                          univer-absolute univer-right-4 univer-top-4 univer-size-6 univer-cursor-pointer
                          univer-rounded-sm univer-border-none univer-bg-transparent univer-p-0
                          univer-transition-opacity
                          hover:univer-opacity-100
                          disabled:univer-pointer-events-none
                          rtl:univer-left-4 rtl:univer-right-auto
                        `}
                        onClick={(event) => {
                            if (onClickClose) {
                                // The custom callback owns dismissal; skip Radix's second close.
                                event.preventDefault();
                                onClickClose();
                            }
                        }}
                    >
                        <CloseIcon className="univer-size-4 univer-text-gray-400" />
                        <span className="univer-sr-only">{locale?.Accessibility.close}</span>
                    </Close>
                )}
            </Content>
        </DialogPortal>
    );
});
DialogContent.displayName = Content.displayName;

const DialogHeader = ({
    className,
    ...props
}: HTMLAttributes<HTMLDivElement>) => (
    <div
        data-slot="dialog-header"
        className={clsx(
            `
              univer-flex univer-flex-col univer-space-y-1.5 univer-text-center
              sm:!univer-text-left
              sm:rtl:!univer-text-right
            `,
            className
        )}
        {...props}
    />
);
DialogHeader.displayName = 'DialogHeader';

const DialogFooter = ({
    className,
    ...props
}: HTMLAttributes<HTMLDivElement>) => (
    <div
        data-slot="dialog-footer"
        className={clsx(
            `
              univer-flex univer-flex-col-reverse
              sm:!univer-flex-row sm:!univer-justify-end sm:!univer-space-x-2
            `,
            className
        )}
        {...props}
    />
);
DialogFooter.displayName = 'DialogFooter';

const DialogTitle = forwardRef<
    ComponentRef<typeof Title>,
    ComponentPropsWithoutRef<typeof Title>
>(({ className, ...props }, ref) => (
    <Title
        ref={ref}
        className={clsx(
            `
              univer-my-0 univer-text-lg univer-font-semibold univer-leading-none univer-tracking-tight
              univer-text-gray-900
              dark:!univer-text-gray-0
            `,
            className
        )}
        {...props}
    />
));
DialogTitle.displayName = Title.displayName;

const DialogDescription = forwardRef<
    ComponentRef<typeof Description>,
    ComponentPropsWithoutRef<typeof Description>
>(({ className, ...props }, ref) => (
    <Description
        ref={ref}
        className={clsx(`
          univer-text-sm univer-text-gray-500
          dark:!univer-text-gray-300
        `, className)}
        {...props}
    />
));
DialogDescription.displayName = Description.displayName;

export {
    Dialog,
    DialogClose,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogOverlay,
    DialogPortal,
    DialogTitle,
    DialogTrigger,
};