Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

3.9 KB
Raw Blame
import { useRef, type MouseEvent as ReactMouseEvent, type PointerEvent as ReactPointerEvent } from "react";

/** How a menu's trigger was last pressed: a mouse press, a click with no press before it (a tap, a test), or a key. */
export type MenuPress = "pointer" | "click" | "keyboard";

/** A mouse's plain primary press opens a menu at once. Touch and pen keep the tap, and a modified press is left to the
 * browser (Control-click is the context menu on a Mac). */
export function opensOnPress(event: Pick<PointerEvent, "pointerType" | "button" | "ctrlKey" | "metaKey" | "shiftKey" | "altKey">): boolean {
  return event.pointerType === "mouse" && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.altKey;
}

/** A menu trigger's press handlers: the mouse opens (or closes) the menu on the press, and the click that ends the
 * same press is spent. A click with no press before it still toggles, so a tap, Enter and Space (a click with detail 0)
 * behave as before. A press released somewhere else leaves no click behind; the next press or key clears it. */
export function useMenuPress(toggle: (how: MenuPress) => void, options: {
  /** The menu moves focus into itself when it opens: the press that opens it must not then focus the trigger (the
   * mouse's own focus change comes after the press). Needs `open`, the menu's state before the press. */
  focusesMenu?: boolean;
  open?: boolean;
} = {}) {
  const pressed = useRef(false);
  const keepFocus = useRef(false);
  const last = useRef<MenuPress>("click");
  const latest = useRef(toggle);
  latest.current = toggle;
  const settings = useRef(options);
  settings.current = options;
  return {
    /** How the menu was last opened or closed from its trigger. */
    last,
    triggerProps: {
      onPointerDown: (event: ReactPointerEvent<HTMLElement>) => {
        pressed.current = false;
        keepFocus.current = false;
        if (!opensOnPress(event) || event.currentTarget.matches(":disabled")) return;
        pressed.current = true;
        keepFocus.current = Boolean(settings.current.focusesMenu && !settings.current.open);
        last.current = "pointer";
        latest.current("pointer");
      },
      onMouseDown: (event: ReactMouseEvent<HTMLElement>) => {
        if (!keepFocus.current) return;
        keepFocus.current = false;
        event.preventDefault();
      },
      // A click called with no event (from code) counts as a plain click.
      onClick: (event?: ReactMouseEvent<HTMLElement>) => {
        const detail = event?.detail ?? 1;
        const spent = pressed.current && detail !== 0;
        pressed.current = false;
        if (spent) return;
        last.current = detail === 0 ? "keyboard" : "click";
        latest.current(last.current);
      },
    },
  };
}

/** Focus back on a menu's trigger when the menu closes. If the person used the mouse (the menu came from a press or a
 * tap and focus never went into it), the trigger is marked so the Escape that closed it does not draw the keyboard
 * ring; the mark goes at the trigger's next key or blur. */
export function returnMenuFocus(trigger: HTMLElement | null | undefined, how: MenuPress, menu?: Element | null, options?: FocusOptions): void {
  if (!trigger) return;
  const keyboard = how === "keyboard" || Boolean(menu && document.activeElement && menu.contains(document.activeElement));
  if (!keyboard) quietFocus(trigger);
  trigger.focus(options);
}

/** Keep the focus ring off `element` until its next key or blur. */
export function quietFocus(element: HTMLElement): void {
  if (element.hasAttribute("data-quiet-focus")) return;
  element.setAttribute("data-quiet-focus", "");
  const clear = () => {
    element.removeAttribute("data-quiet-focus");
    element.removeEventListener("keydown", clear);
    element.removeEventListener("blur", clear);
  };
  element.addEventListener("keydown", clear);
  element.addEventListener("blur", clear);
}