mirror of
https://github.com/outline/outline.git
synced 2026-06-13 03:14:59 +03:00
adbffc0734
* chore: clear mechanical lint warnings Drops 44 oxlint warnings (559 → 515) by fixing easy mechanical rules across the codebase: no-useless-escape, no-duplicate-type-constituents, no-redundant-type-constituents, no-unused-expressions, no-meaningless-void-operator, require-array-sort-compare, await-thenable. * chore: drop callback parameter from useCallback deps The `open` argument is a parameter of the callback, not a closed-over variable, so it doesn't belong in the deps array. * chore: promote cleared lint rules to errors Promotes the rules cleared in this PR from warn to error so future violations fail the lint: - no-unused-expressions - typescript/await-thenable - typescript/no-duplicate-type-constituents - typescript/no-meaningless-void-operator - typescript/require-array-sort-compare Removes the override that suppressed no-useless-escape on source files (the global rule is already error) and fixes the 21 escape violations that this exposed in regex character classes and template literals. * chore: address PR review feedback - usePinnedDocuments: simplify UrlId to plain string instead of the intersection trick. - PlantUML embed: move - to end of character class so it's a literal hyphen rather than a range operator. - checkboxes: type token params as Token | undefined to match the actual call sites that pass tokens[index - 2] etc.
88 lines
2.6 KiB
TypeScript
88 lines
2.6 KiB
TypeScript
import * as React from "react";
|
|
import { actionToMenuItem } from "~/actions";
|
|
import useActionContext from "~/hooks/useActionContext";
|
|
import useMobile from "~/hooks/useMobile";
|
|
import type { ActionVariant, ActionWithChildren } from "~/types";
|
|
import { preventDefault } from "~/utils/events";
|
|
import { toMenuItems } from "./transformer";
|
|
import { observer } from "mobx-react";
|
|
import { useComputed } from "~/hooks/useComputed";
|
|
import { Menu, MenuContent, MenuTrigger } from "~/components/primitives/Menu";
|
|
import { MenuProvider } from "~/components/primitives/Menu/MenuContext";
|
|
|
|
type Props = {
|
|
/** Root action with children representing the menu items */
|
|
action?: ActionWithChildren;
|
|
/** Trigger for the menu */
|
|
children: React.ReactNode;
|
|
/** ARIA label for the menu */
|
|
ariaLabel: string;
|
|
/** Callback when menu is opened */
|
|
onOpen?: () => void;
|
|
/** Callback when menu is closed */
|
|
onClose?: () => void;
|
|
};
|
|
|
|
export const ContextMenu = observer(
|
|
({ action, children, ariaLabel, onOpen, onClose }: Props) => {
|
|
const isMobile = useMobile();
|
|
const contentRef = React.useRef<React.ElementRef<typeof MenuContent>>(null);
|
|
const actionContext = useActionContext({
|
|
isMenu: true,
|
|
});
|
|
|
|
const menuItems = useComputed(
|
|
() =>
|
|
((action?.children as ActionVariant[]) ?? []).map((childAction) =>
|
|
actionToMenuItem(childAction, actionContext)
|
|
),
|
|
[action?.children, actionContext]
|
|
);
|
|
|
|
const handleOpenChange = React.useCallback(
|
|
(open: boolean) => {
|
|
if (open) {
|
|
onOpen?.();
|
|
} else {
|
|
onClose?.();
|
|
}
|
|
},
|
|
[onOpen, onClose]
|
|
);
|
|
|
|
const enablePointerEvents = React.useCallback(() => {
|
|
if (contentRef.current) {
|
|
contentRef.current.style.pointerEvents = "auto";
|
|
}
|
|
}, []);
|
|
|
|
const disablePointerEvents = React.useCallback(() => {
|
|
if (contentRef.current) {
|
|
contentRef.current.style.pointerEvents = "none";
|
|
}
|
|
}, []);
|
|
|
|
if (isMobile || !action || menuItems.length === 0) {
|
|
return <>{children}</>;
|
|
}
|
|
|
|
const content = toMenuItems(menuItems);
|
|
|
|
return (
|
|
<MenuProvider variant="context">
|
|
<Menu onOpenChange={handleOpenChange}>
|
|
<MenuTrigger aria-label={ariaLabel}>{children}</MenuTrigger>
|
|
<MenuContent
|
|
aria-label={ariaLabel}
|
|
onAnimationStart={disablePointerEvents}
|
|
onAnimationEnd={enablePointerEvents}
|
|
onCloseAutoFocus={preventDefault}
|
|
>
|
|
{content}
|
|
</MenuContent>
|
|
</Menu>
|
|
</MenuProvider>
|
|
);
|
|
}
|
|
);
|