diff --git a/apps/web/src/index.css b/apps/web/src/index.css index 15380512f4060..e55d651a41325 100644 --- a/apps/web/src/index.css +++ b/apps/web/src/index.css @@ -7,14 +7,11 @@ /* Window Controls Overlay: active when Electron exposes native titlebar control geometry. */ @custom-variant wco (&:is(.wco, .wco *)); -/* Secondary hovering pointers reveal sidebar actions on touch-primary devices. */ -@custom-variant any-hover { - @media (any-hover: hover) { - &:hover { - @slot; - } - } -} +/* Hover variants key off markers main.tsx keeps on : the media query's answer, or a real + mouse that Chromium reports as non-hovering (VDI/VM absolute pointers). A single selector keeps + one rule per utility. Without main.tsx (e.g. browser tests importing this file) hover never matches. */ +@custom-variant hover (&:where([data-hover] *):hover); +@custom-variant any-hover (&:where([data-any-hover] *):hover); /* On mobile, morph the expanded hero composer into the compact docked composer while it moves; the rest of the app remains visually stationary. */ diff --git a/apps/web/src/main.test.ts b/apps/web/src/main.test.ts index 11b9b68c867d1..1200ff6afab58 100644 --- a/apps/web/src/main.test.ts +++ b/apps/web/src/main.test.ts @@ -6,7 +6,10 @@ vi.mock("./i18n/runtime", () => ({ whenLocaleReady: async () => {} })); vi.mock("react-dom/client", () => ({ default: { createRoot: () => ({ render: () => {} }) } })); // Boots the entry in a fresh page that shares the tab's sessionStorage, as a reload does. -async function bootThenFailLazyChunk(sessionStorage: Map) { +async function bootMain( + { sessionStorage = new Map(), anyHover = true }, + interact: (page: EventTarget, setAnyHover: (matches: boolean) => void) => void, +) { vi.resetModules(); const reload = vi.fn().mockName("location.reload"); const page = Object.assign(new EventTarget(), { @@ -16,22 +19,69 @@ async function bootThenFailLazyChunk(sessionStorage: Map) { removeItem: (key: string) => sessionStorage.delete(key), }, location: { reload }, + matchMedia: (query: string) => ({ + get matches() { + return anyHover && query.endsWith("hover: hover)"); + }, + addEventListener: (type: string, listener: () => void) => + page.addEventListener(type, listener), + }), }); + const setAnyHover = (matches: boolean) => { + anyHover = matches; + page.dispatchEvent(new Event("change")); + }; + const root = { + attributes: new Set(), + toggleAttribute: (name: string, force: boolean) => + force ? root.attributes.add(name) : root.attributes.delete(name), + }; vi.stubGlobal("window", page); - vi.stubGlobal("document", { getElementById: () => ({}) }); + vi.stubGlobal("document", { getElementById: () => ({}), documentElement: root }); try { const { boot } = await import("./bootstrap"); await boot; - page.dispatchEvent(new Event("vite:preloadError", { cancelable: true })); - return reload; + interact(page, setAnyHover); + return { reload, root }; } finally { vi.unstubAllGlobals(); } } +const failLazyChunk = (page: EventTarget) => + page.dispatchEvent(new Event("vite:preloadError", { cancelable: true })); + it("reloads only once when a lazy chunk fails after every boot", async () => { const sessionStorage = new Map(); - expect(await bootThenFailLazyChunk(sessionStorage)).toHaveBeenCalledTimes(1); - expect(await bootThenFailLazyChunk(sessionStorage)).not.toHaveBeenCalled(); + expect((await bootMain({ sessionStorage }, failLazyChunk)).reload).toHaveBeenCalledTimes(1); + expect((await bootMain({ sessionStorage }, failLazyChunk)).reload).not.toHaveBeenCalled(); }); + +it.each([ + { anyHover: false, input: "pointermove:touch pointermove:pen", marked: false }, + { anyHover: false, input: "pointermove:touch pointermove:mouse", marked: true }, + { anyHover: false, input: "pointermove:mouse pointermove:touch", marked: false }, + { anyHover: false, input: "pointermove:mouse pointerdown:mouse", marked: true }, + { + anyHover: false, + input: "pointermove:mouse pointerdown:touch pointerdown:mouse", + marked: false, + }, + { anyHover: true, input: "pointermove:touch", marked: true }, + { anyHover: true, input: "pointermove:touch any-hover:none pointermove:mouse", marked: true }, + { anyHover: true, input: "pointermove:mouse any-hover:none pointermove:touch", marked: false }, +])( + "hover markers are $marked with any-hover $anyHover after $input", + async ({ anyHover, input, marked }) => { + const { root } = await bootMain({ anyHover }, (page, setAnyHover) => { + for (const event of input.split(" ")) { + const [type = "", pointerType] = event.split(":"); + if (type === "any-hover") setAnyHover(false); + else page.dispatchEvent(Object.assign(new Event(type), { pointerType })); + } + }); + + expect([...root.attributes].toSorted()).toEqual(marked ? ["data-any-hover", "data-hover"] : []); + }, +); diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx index 2640ca1e167d0..c0b7c97a5c8eb 100644 --- a/apps/web/src/main.tsx +++ b/apps/web/src/main.tsx @@ -19,6 +19,41 @@ const router = startAppServices(); const clerkPublishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY as string | undefined; +// index.css gates hover variants on these markers instead of media queries, so a VDI/VM mouse that +// Chromium reports as non-hovering can set them too. +const root = document.documentElement; +const hoverQuery = window.matchMedia("(hover: hover)"); +const anyHoverQuery = window.matchMedia("(any-hover: hover)"); +let lastPointerIsMouse = false; +const syncHoverMarkers = () => { + const mouseFallback = lastPointerIsMouse && !anyHoverQuery.matches; + root.toggleAttribute("data-hover", hoverQuery.matches || mouseFallback); + root.toggleAttribute("data-any-hover", anyHoverQuery.matches || mouseFallback); +}; +hoverQuery.addEventListener("change", syncHoverMarkers); +anyHoverQuery.addEventListener("change", syncHoverMarkers); +syncHoverMarkers(); + +const setLastPointer = (isMouse: boolean) => { + if (isMouse === lastPointerIsMouse) return; + lastPointerIsMouse = isMouse; + syncHoverMarkers(); +}; +// Capture phase: a handler that stops propagation must not hide the pointer type. +const pointerOptions = { capture: true, passive: true }; +window.addEventListener( + "pointermove", + (event) => setLastPointer(event.pointerType === "mouse"), + pointerOptions, +); +// A tap may never move, so presses clear the marker before its hover sticks. They never set +// it: the app replays synthetic mouse presses (HostedBrowserWebview). +window.addEventListener( + "pointerdown", + (event) => event.pointerType !== "mouse" && setLastPointer(false), + pointerOptions, +); + const app = ; // Split by runtime to keep Clerk out of local startup and clerk-js out of browser builds.