summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/decks/AbilityCardZoom.svelte
blob: 35cad2554a7defe50199309fea59759165dcacc2 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!--
	The ability-card zoom (the reference `AbilityCardZoom`): the current
	card at ~2.5x scale, tap (or Escape) to close.
-->
<script lang="ts">
	import type { MonsterAbilityCard as AbilityCard } from '$lib/protocol/types';
	import type { StatMonster } from '$lib/game/line/statApplier';
	import MonsterAbilityCard from '../cards/MonsterAbilityCard.svelte';

	const DEFAULT_ZOOM = 2.5;

	let {
		card,
		stat,
		fh,
		flying = false,
		open = $bindable(true)
	}: {
		card: AbilityCard;
		stat: StatMonster | null;
		fh: boolean;
		flying?: boolean;
		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;
		const w = window.innerWidth;
		const h = window.innerHeight;
		let zoom = Math.min(
			DEFAULT_ZOOM,
			(w - 80) / 142.4,
			(h - 120) / 94.4
		);
		return Math.max(zoom, 0.5);
	}
	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)}
	>
		<MonsterAbilityCard
			{card}
			{stat}
			{fh}
			{flying}
			scale={scale}
			applyStats={false}
		/>
	</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>