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.6 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 { LocaleKey } from '../../../locale/types';
import type { MobilePanelLayout } from '../../mobile-workbench/MobileCanvasLayout';
import type { MobileDrawerSnap } from '../mobile-drawer/MobileDrawer';
import type { IRenderedSidebarOptions, ISidebarMethodOptions } from './Sidebar';
import { LocaleService } from '@univerjs/core';
import { MobileActionRowGroup } from '@univerjs/design';
import { CloseIcon } from '@univerjs/icons';
import { useEffect, useMemo, useRef, useState } from 'react';
import { ISidebarService } from '../../../services/sidebar/sidebar.service';
import { useDependency, useObservable } from '../../../utils/di';
import { MobileDrawer } from '../mobile-drawer/MobileDrawer';
import { renderSidebarOptions } from './Sidebar';

export interface IMobileSidebarMethodOptions extends ISidebarMethodOptions {
    layout?: MobilePanelLayout;
}

type IMobileRenderedSidebarOptions = IRenderedSidebarOptions & Pick<IMobileSidebarMethodOptions, 'layout'>;

export function MobileSidebar() {
    const localeService = useDependency(LocaleService);
    const sidebarService = useDependency(ISidebarService);
    const sidebarOptions = useObservable(sidebarService.sidebarOptions$) as IMobileSidebarMethodOptions | undefined;
    const layerRef = useRef<HTMLDivElement>(null);
    const scrollRef = useRef<HTMLDivElement>(null);
    const closeButtonRef = useRef<HTMLButtonElement>(null);
    const [snapOverride, setSnapOverride] = useState<{ id?: string; snap: MobileDrawerSnap } | null>(null);
    const options = useMemo(
        () => renderSidebarOptions(sidebarOptions) as IMobileRenderedSidebarOptions | null,
        [sidebarOptions]
    );

    useEffect(() => {
        if (options?.visible && options.layout !== 'canvas') {
            closeButtonRef.current?.focus();
        }
    }, [options?.visible, options?.layout, options?.id]);

    useEffect(() => {
        const scrollElement = scrollRef.current;
        sidebarService.setContainer(scrollElement ?? undefined);
        const handleScroll = (event: Event) => sidebarService.scrollEvent$.next(event);
        scrollElement?.addEventListener('scroll', handleScroll);
        return () => {
            scrollElement?.removeEventListener('scroll', handleScroll);
            sidebarService.setContainer(undefined);
        };
    }, [options?.visible, sidebarService]);

    if (!options?.visible) {
        return null;
    }
    const defaultSnap = options.layout === 'canvas' ? 'compact' : 'expanded';

    const close = () => sidebarService.close(sidebarOptions?.id);

    return (
        <div ref={layerRef} className="univer-pointer-events-none univer-fixed univer-inset-0 univer-z-[1100]" data-u-comp="mobile-sidebar">
            {options.layout !== 'canvas' && (
                <button
                    type="button"
                    aria-label={localeService.t<LocaleKey>('ui.sidebar.close')}
                    className="
                      univer-pointer-events-auto univer-absolute univer-inset-0 univer-m-0 univer-appearance-none
                      univer-rounded-none univer-border-0 univer-bg-black/35 univer-p-0
                    "
                    onClick={close}
                />
            )}
            <MobileDrawer
                layerRef={layerRef}
                layout={options.layout}
                componentName="mobile-sidebar-drawer"
                snap={snapOverride && snapOverride.id === options.id ? snapOverride.snap : defaultSnap}
                expandLabel={localeService.t<LocaleKey>('ui.ribbon.more')}
                collapseLabel={localeService.t<LocaleKey>('ui.ribbon.more')}
                onSnapChange={(snap) => setSnapOverride({ id: options.id, snap })}
                onClose={close}
                role="dialog"
                ariaLabel={localeService.t<LocaleKey>('ui.sidebar.panel')}
                contentRef={scrollRef}
                panelClassName="
                  univer-bg-gray-0 univer-text-gray-900
                  dark:!univer-bg-gray-900 dark:!univer-text-gray-0
                "
                contentClassName="univer-min-h-0 univer-px-4 univer-pb-4"
                header={(
                    <header
                        className="
                          univer-flex univer-h-12 univer-flex-1 univer-items-center univer-justify-between univer-px-4
                        "
                    >
                        <div className="univer-min-w-0 univer-truncate univer-text-base univer-font-semibold">{options.header}</div>
                        <button
                            ref={closeButtonRef}
                            type="button"
                            className="
                              univer-flex univer-size-10 univer-shrink-0 univer-items-center univer-justify-center
                              univer-rounded-full univer-border-0 univer-bg-transparent univer-text-xl
                              univer-text-gray-600 univer-outline-none
                              active:univer-bg-gray-100
                              dark:!univer-text-gray-300
                              dark:active:!univer-bg-gray-700
                            "
                            onClick={close}
                            aria-label={localeService.t<LocaleKey>('ui.sidebar.close')}
                        >
                            <CloseIcon />
                        </button>
                    </header>
                )}
                footer={options.footer
                    ? (
                        <footer
                            className="
                              univer-shrink-0 univer-border-0 univer-border-t univer-border-solid univer-border-gray-200
                              univer-p-4
                              dark:!univer-border-gray-700
                            "
                        >
                            <MobileActionRowGroup>{options.footer}</MobileActionRowGroup>
                        </footer>
                    )
                    : undefined}
            >
                {options.children}
            </MobileDrawer>
        </div>
    );
}