Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions apps/web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 <html>: 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. */
Expand Down
62 changes: 56 additions & 6 deletions apps/web/src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>) {
async function bootMain(
{ sessionStorage = new Map<string, string>(), 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(), {
Expand All @@ -16,22 +19,69 @@ async function bootThenFailLazyChunk(sessionStorage: Map<string, string>) {
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<string>(),
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<string, string>();

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"] : []);
},
);
35 changes: 35 additions & 0 deletions apps/web/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <AppRoot router={router} />;

// Split by runtime to keep Clerk out of local startup and clerk-js out of browser builds.
Expand Down
Loading