e9cae918a7
* feat: Sidebar tabs support [wip] * tab trigger styling tweaks * add `:hover` & `:active` states * replace `@dwelle/tunnel-rat` with `tunnel-rat` * make stuff more explicit - remove `Sidebar.Header` fallback (host apps need to render manually), and stop tunneling it (render in place) - make `docked` state explicit - stop tunneling `Sidebar.TabTriggers` (render in place) * redesign sidebar / library as per latest spec * support no label on `Sidebar.Trigger` * add Sidebar `props.onStateChange` * style fixes * make `appState.isSidebarDocked` into a soft user preference * px -> rem & refactor * remove `props.renderSidebar` * update tests * remove * refactor * rename constants * tab triggers styling fixes * factor out library-related logic from generic sidebar trigger * change `props.onClose` to `onToggle` * rename `props.value` -> `props.tab` * add displayNames * allow HTMLAttributes on applicable compos * fix example App * more styling tweaks and fixes * fix not setting `dockable` * more style fixes * fix and align sidebar header button styling * make DefaultSidebar dockable on if host apps supplies `onDock` * stop `Sidebar.Trigger` hiding label on mobile this should be only the default sidebar trigger behavior, and for that we don't need to use `device` hook as we handle in CSS * fix `dockable` prop of defaultSidebar * remove extra `typescript` dep * remove `defaultTab` prop in favor of explicit `tab` value in `<Sidebar.Trigger/>` and `toggleSidebar()`, to reduce API surface area and solve inconsistency of `appState.openSidebar.tab` not reflecting actual UI value if `defaultTab` was supported (without additional syncing logic which feels like the wrong solution). * remove `onToggle` in favor of `onStateChange` reducing API surface area * fix restore * comment no longer applies * reuse `Button` component in sidebar buttons * fix tests * split Sidebar sub-components into files * remove `props.dockable` in favor of `props.onDock` only * split tests * fix sidebar showing dock button if no `props.docked` supplied & add more tests * reorder and group sidebar tests * clarify * rename classes & dedupe css * refactor tests * update changelog * update changelog --------- Co-authored-by: barnabasmolnar <barnabas@excalidraw.com>
111 lines
3.4 KiB
TypeScript
111 lines
3.4 KiB
TypeScript
import clsx from "clsx";
|
|
import React, { useEffect, useState } from "react";
|
|
import { useCallbackRefState } from "../hooks/useCallbackRefState";
|
|
import { t } from "../i18n";
|
|
import {
|
|
useExcalidrawContainer,
|
|
useDevice,
|
|
useExcalidrawSetAppState,
|
|
} from "../components/App";
|
|
import { KEYS } from "../keys";
|
|
import "./Dialog.scss";
|
|
import { back, CloseIcon } from "./icons";
|
|
import { Island } from "./Island";
|
|
import { Modal } from "./Modal";
|
|
import { AppState } from "../types";
|
|
import { queryFocusableElements } from "../utils";
|
|
import { useSetAtom } from "jotai";
|
|
import { isLibraryMenuOpenAtom } from "./LibraryMenu";
|
|
import { jotaiScope } from "../jotai";
|
|
|
|
export interface DialogProps {
|
|
children: React.ReactNode;
|
|
className?: string;
|
|
small?: boolean;
|
|
onCloseRequest(): void;
|
|
title: React.ReactNode;
|
|
autofocus?: boolean;
|
|
theme?: AppState["theme"];
|
|
closeOnClickOutside?: boolean;
|
|
}
|
|
|
|
export const Dialog = (props: DialogProps) => {
|
|
const [islandNode, setIslandNode] = useCallbackRefState<HTMLDivElement>();
|
|
const [lastActiveElement] = useState(document.activeElement);
|
|
const { id } = useExcalidrawContainer();
|
|
|
|
useEffect(() => {
|
|
if (!islandNode) {
|
|
return;
|
|
}
|
|
|
|
const focusableElements = queryFocusableElements(islandNode);
|
|
|
|
if (focusableElements.length > 0 && props.autofocus !== false) {
|
|
// If there's an element other than close, focus it.
|
|
(focusableElements[1] || focusableElements[0]).focus();
|
|
}
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
if (event.key === KEYS.TAB) {
|
|
const focusableElements = queryFocusableElements(islandNode);
|
|
const { activeElement } = document;
|
|
const currentIndex = focusableElements.findIndex(
|
|
(element) => element === activeElement,
|
|
);
|
|
|
|
if (currentIndex === 0 && event.shiftKey) {
|
|
focusableElements[focusableElements.length - 1].focus();
|
|
event.preventDefault();
|
|
} else if (
|
|
currentIndex === focusableElements.length - 1 &&
|
|
!event.shiftKey
|
|
) {
|
|
focusableElements[0].focus();
|
|
event.preventDefault();
|
|
}
|
|
}
|
|
};
|
|
|
|
islandNode.addEventListener("keydown", handleKeyDown);
|
|
|
|
return () => islandNode.removeEventListener("keydown", handleKeyDown);
|
|
}, [islandNode, props.autofocus]);
|
|
|
|
const setAppState = useExcalidrawSetAppState();
|
|
const setIsLibraryMenuOpen = useSetAtom(isLibraryMenuOpenAtom, jotaiScope);
|
|
|
|
const onClose = () => {
|
|
setAppState({ openMenu: null });
|
|
setIsLibraryMenuOpen(false);
|
|
(lastActiveElement as HTMLElement).focus();
|
|
props.onCloseRequest();
|
|
};
|
|
|
|
return (
|
|
<Modal
|
|
className={clsx("Dialog", props.className)}
|
|
labelledBy="dialog-title"
|
|
maxWidth={props.small ? 550 : 800}
|
|
onCloseRequest={onClose}
|
|
theme={props.theme}
|
|
closeOnClickOutside={props.closeOnClickOutside}
|
|
>
|
|
<Island ref={setIslandNode}>
|
|
<h2 id={`${id}-dialog-title`} className="Dialog__title">
|
|
<span className="Dialog__titleContent">{props.title}</span>
|
|
<button
|
|
className="Modal__close"
|
|
onClick={onClose}
|
|
title={t("buttons.close")}
|
|
aria-label={t("buttons.close")}
|
|
>
|
|
{useDevice().isMobile ? back : CloseIcon}
|
|
</button>
|
|
</h2>
|
|
<div className="Dialog__content">{props.children}</div>
|
|
</Island>
|
|
</Modal>
|
|
);
|
|
};
|