summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/decks/ModifierCardZoom.svelte
blob: 98f32ea5e4ccdcc6db245923ff3329d21155261d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!--
	The modifier-card zoom (the reference `ModifierCardZoom`): the top card
	at ~3x scale (shrunk to fit the screen width), tap (or Escape) to close.
-->
<script lang="ts">
	import type { ModifierCard as Card } from '$lib/protocol/types';
	import ModifierCard from '../cards/ModifierCard.svelte';

	const DEFAULT_ZOOM = 3.0;
	const CARD_WIDTH = 58.6666;

	let {
		card,
		name,
		perks = null,
		open = $bindable(true)
	}: {
		card: Card;
		/** The deck name ("" party, "allies", or a character id). */
		name: string;
		perks?: string[][] | null;
		open?: boolean;
	} = $props();

	// Fit the card into the viewport (the reference shrinks the zoom to fit).
	function scaleFor(): number {
		if (typeof window === 'undefined') return DEFAULT_ZOOM;
		return Math.min(DEFAULT_ZOOM, window.innerWidth / (CARD_WIDTH + 80));
	}
	const scale = $state(scaleFor());

	let zoomEl: HTMLDivElement | undefined = $state();
	$effect(() => {
		// Focus the overlay when it opens so Escape (keyboard) can close it.
		if (open) zoomEl?.focus();
	});
</script>

{#if open}
	<div
		bind:this={zoomEl}
		class="zoom"
		role="dialog"
		aria-modal="true"
		tabindex="-1"
		onclick={() => (open = false)}
		onkeydown={(e) => e.key === 'Escape' && (open = false)}
	>
		<ModifierCard {card} {name} revealed {perks} {scale} />
	</div>
{/if}

<style>
	.zoom {
		position: fixed;
		inset: 0;
		z-index: 40;
		display: flex;
		align-items: center;
		justify-content: center;
		background: rgba(0, 0, 0, 0.75);
		cursor: pointer;
	}
</style>