From 40bb37737be495b4105a2d64bbfe8ff6cf16d4a8 Mon Sep 17 00:00:00 2001 From: Jarrod Servilla Date: Sun, 25 Oct 2026 23:29:07 -0700 Subject: [PATCH 1/2] feat(life): add fullscreen mode to life counter (#103) A Fullscreen toggle in the toolbar pins the board as a fixed full-viewport overlay above the site header, footer and mobile nav, and requests browser fullscreen on the document element when the Fullscreen API exists. Requesting it on the document (not the board) keeps portaled popovers and dialogs visible in native fullscreen. Where the API is missing (iPhone Safari) the overlay alone hides the chrome, and Escape exits. Leaving native fullscreen via Escape or the browser control fires fullscreenchange and restores the normal layout. Game state is untouched since the board never unmounts. Claude-Session: https://claude.ai/code/session_01MdQiSNDf2TEmigzRFD1rGF --- .../life-counter/life-counter.test.tsx | 94 ++++++++++++++++++- app/_components/life-counter/life-counter.tsx | 42 ++++++++- .../life-counter/use-fullscreen.ts | 45 +++++++++ 3 files changed, 175 insertions(+), 6 deletions(-) create mode 100644 app/_components/life-counter/use-fullscreen.ts diff --git a/app/_components/life-counter/life-counter.test.tsx b/app/_components/life-counter/life-counter.test.tsx index cf9c9db..fc04498 100644 --- a/app/_components/life-counter/life-counter.test.tsx +++ b/app/_components/life-counter/life-counter.test.tsx @@ -1,8 +1,44 @@ -import { describe, expect, it } from "vitest"; -import { render, screen, within } from "@testing-library/react"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { act, fireEvent, render, screen, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { LifeCounter } from "./life-counter"; +function stubFullscreenApi() { + let element: Element | null = null; + const requestFullscreen = vi.fn(async () => { + element = document.documentElement; + document.dispatchEvent(new Event("fullscreenchange")); + }); + const exitFullscreen = vi.fn(async () => { + element = null; + document.dispatchEvent(new Event("fullscreenchange")); + }); + Object.defineProperty(document, "fullscreenElement", { configurable: true, get: () => element }); + Object.defineProperty(document, "exitFullscreen", { configurable: true, value: exitFullscreen }); + Object.defineProperty(document.documentElement, "requestFullscreen", { + configurable: true, + value: requestFullscreen, + }); + return { + requestFullscreen, + exitFullscreen, + nativeExit() { + element = null; + document.dispatchEvent(new Event("fullscreenchange")); + }, + }; +} + +function board() { + return screen.getByRole("heading", { name: "Life counter" }).closest("[data-fullscreen]"); +} + +afterEach(() => { + delete (document as { fullscreenElement?: unknown }).fullscreenElement; + delete (document as { exitFullscreen?: unknown }).exitFullscreen; + delete (document.documentElement as { requestFullscreen?: unknown }).requestFullscreen; +}); + describe("LifeCounter", () => { it("lowers life when commander damage is marked", async () => { const user = userEvent.setup(); @@ -27,4 +63,58 @@ describe("LifeCounter", () => { expect(within(panel).getByText("Commander tax")).toBeInTheDocument(); expect(container).not.toHaveTextContent(/Cmdr/); }); + + describe("fullscreen", () => { + it("enters and exits browser fullscreen from the toolbar", async () => { + const api = stubFullscreenApi(); + const user = userEvent.setup(); + render(); + + await user.click(screen.getByRole("button", { name: "Fullscreen" })); + expect(api.requestFullscreen).toHaveBeenCalledOnce(); + expect(board()).toHaveAttribute("data-fullscreen", "true"); + + await user.click(screen.getByRole("button", { name: "Exit fullscreen" })); + expect(api.exitFullscreen).toHaveBeenCalledOnce(); + expect(board()).toHaveAttribute("data-fullscreen", "false"); + }); + + it("returns to the normal layout when the browser leaves fullscreen", async () => { + const api = stubFullscreenApi(); + const user = userEvent.setup(); + render(); + + await user.click(screen.getByRole("button", { name: "Fullscreen" })); + act(() => api.nativeExit()); + + expect(board()).toHaveAttribute("data-fullscreen", "false"); + expect(screen.getByRole("button", { name: "Fullscreen" })).toBeInTheDocument(); + }); + + it("goes fullscreen in-page when the Fullscreen API is unavailable", async () => { + const user = userEvent.setup(); + render(); + + await user.click(screen.getByRole("button", { name: "Fullscreen" })); + expect(board()).toHaveAttribute("data-fullscreen", "true"); + + fireEvent.keyDown(document, { key: "Escape" }); + expect(board()).toHaveAttribute("data-fullscreen", "false"); + }); + + it("keeps life totals across entering and exiting fullscreen", async () => { + stubFullscreenApi(); + const user = userEvent.setup(); + render(); + + const life = () => screen.getByLabelText("Player 1 life", { exact: true }); + await user.click(screen.getByRole("button", { name: "Decrease Player 1 life" })); + const before = life().textContent!; + + await user.click(screen.getByRole("button", { name: "Fullscreen" })); + await user.click(screen.getByRole("button", { name: "Exit fullscreen" })); + + expect(life()).toHaveTextContent(before); + }); + }); }); diff --git a/app/_components/life-counter/life-counter.tsx b/app/_components/life-counter/life-counter.tsx index d201e39..a0aedca 100644 --- a/app/_components/life-counter/life-counter.tsx +++ b/app/_components/life-counter/life-counter.tsx @@ -1,7 +1,7 @@ "use client"; import { useState, type ReactNode } from "react"; -import { Dices, RotateCcw, Settings2 } from "lucide-react"; +import { Dices, Maximize, Minimize, RotateCcw, Settings2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { Eyebrow } from "@/components/ui/eyebrow"; @@ -17,7 +17,9 @@ import { type LifeCounterAction, type Player, } from "@/lib/life-counter/state"; +import { cn } from "@/lib/utils"; import { PlayerPanel } from "./player-panel"; +import { useFullscreen } from "./use-fullscreen"; import { useLifeCounter } from "./use-life-counter"; type Dispatch = (action: LifeCounterAction) => void; @@ -159,7 +161,25 @@ function SettingsPopover({ game, dispatch }: { game: GameState; dispatch: Dispat ); } -function Toolbar({ game, dispatch }: { game: GameState; dispatch: Dispatch }) { +function FullscreenToggle({ + isFullscreen, + onEnter, + onExit, +}: { + isFullscreen: boolean; + onEnter: () => void; + onExit: () => void; +}) { + const label = isFullscreen ? "Exit fullscreen" : "Fullscreen"; + return ( + + ); +} + +function Toolbar({ game, dispatch, fullscreen }: { game: GameState; dispatch: Dispatch; fullscreen: ReactNode }) { return (

Life counter

@@ -184,6 +204,7 @@ function Toolbar({ game, dispatch }: { game: GameState; dispatch: Dispatch }) { } /> + {fullscreen}
); } @@ -207,11 +228,24 @@ function boardRows(game: GameState): { players: Player[]; rotated: boolean }[] { export function LifeCounter() { const [game, dispatch] = useLifeCounter(); + const [isFullscreen, enterFullscreen, exitFullscreen] = useFullscreen(); const seatOf = new Map(game.players.map((p, i) => [p.id, i])); return ( -
- +
+ } + />
{boardRows(game).map((row, i) => (
void, () => void] { + const [isFullscreen, setIsFullscreen] = useState(false); + + useEffect(() => { + function onFullscreenChange() { + if (!document.fullscreenElement) setIsFullscreen(false); + } + document.addEventListener("fullscreenchange", onFullscreenChange); + return () => + document.removeEventListener("fullscreenchange", onFullscreenChange); + }, []); + + useEffect(() => { + if (!isFullscreen || document.fullscreenElement) return; + function onKeyDown(e: KeyboardEvent) { + if (e.key === "Escape" && !e.defaultPrevented) setIsFullscreen(false); + } + document.addEventListener("keydown", onKeyDown); + return () => document.removeEventListener("keydown", onKeyDown); + }, [isFullscreen]); + + const enter = useCallback(() => { + setIsFullscreen(true); + if (supportsFullscreenApi()) { + document.documentElement.requestFullscreen().catch(() => {}); + } + }, []); + + const exit = useCallback(() => { + setIsFullscreen(false); + if (document.fullscreenElement) { + document.exitFullscreen().catch(() => {}); + } + }, []); + + return [isFullscreen, enter, exit]; +} From 7b60a5a1021959c7a823266559f09842ec1b959f Mon Sep 17 00:00:00 2001 From: Jarrod Servilla Date: Sun, 25 Oct 2026 23:38:26 -0700 Subject: [PATCH 2/2] test(life): cover fullscreen rejection and in-page exit paths Covers the browser refusing requestFullscreen (overlay still applies), exitFullscreen rejecting, an Escape already handled by a popover, and exiting in-page mode, bringing function coverage back to 100%. Claude-Session: https://claude.ai/code/session_01MdQiSNDf2TEmigzRFD1rGF --- .../life-counter/use-fullscreen.test.tsx | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 app/_components/life-counter/use-fullscreen.test.tsx diff --git a/app/_components/life-counter/use-fullscreen.test.tsx b/app/_components/life-counter/use-fullscreen.test.tsx new file mode 100644 index 0000000..98514b9 --- /dev/null +++ b/app/_components/life-counter/use-fullscreen.test.tsx @@ -0,0 +1,59 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { act, renderHook } from "@testing-library/react"; +import { useFullscreen } from "./use-fullscreen"; + +afterEach(() => { + delete (document as { fullscreenElement?: unknown }).fullscreenElement; + delete (document as { exitFullscreen?: unknown }).exitFullscreen; + delete (document.documentElement as { requestFullscreen?: unknown }).requestFullscreen; +}); + +describe("useFullscreen", () => { + it("stays fullscreen in-page when the browser refuses native fullscreen", async () => { + Object.defineProperty(document.documentElement, "requestFullscreen", { + configurable: true, + value: vi.fn().mockRejectedValue(new TypeError("Permissions check failed")), + }); + const { result } = renderHook(() => useFullscreen()); + + await act(async () => result.current[1]()); + + expect(result.current[0]).toBe(true); + }); + + it("exits even when the browser rejects leaving native fullscreen", async () => { + Object.defineProperty(document, "fullscreenElement", { configurable: true, value: document.documentElement }); + Object.defineProperty(document, "exitFullscreen", { + configurable: true, + value: vi.fn().mockRejectedValue(new TypeError("Document not active")), + }); + const { result } = renderHook(() => useFullscreen()); + + await act(async () => result.current[1]()); + await act(async () => result.current[2]()); + + expect(result.current[0]).toBe(false); + }); + + it("ignores an Escape another component already handled", () => { + const { result } = renderHook(() => useFullscreen()); + act(() => result.current[1]()); + + const event = new KeyboardEvent("keydown", { key: "Escape", cancelable: true }); + event.preventDefault(); + act(() => { + document.dispatchEvent(event); + }); + + expect(result.current[0]).toBe(true); + }); + + it("exits in-page fullscreen without touching the Fullscreen API", () => { + const { result } = renderHook(() => useFullscreen()); + + act(() => result.current[1]()); + act(() => result.current[2]()); + + expect(result.current[0]).toBe(false); + }); +});