-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Hand.svelte
114 lines (99 loc) · 2.52 KB
/
_Hand.svelte
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<script>
import Card from '$lib/components/Card.svelte';
import { hand, focused } from '../_store.js';
import { choose } from '$lib/utils/actions.js';
import { flip } from 'svelte/animate';
import { onMount } from 'svelte';
let cardsElement;
let backgroundElement;
let overlap = 0;
function convertRemToPixels(rem = 0) {
return rem * parseFloat(getComputedStyle(document.documentElement).fontSize);
}
function fitCards() {
if (backgroundElement && cardsElement) {
// get entire height of cards el
const p = backgroundElement.clientHeight - convertRemToPixels(5);
// get individual card height
const a = cardsElement.firstElementChild.offsetHeight;
// get number of cards
const c = Object.entries($hand).length;
if (c * (a + 30) - 30 < p) {
// The cards won't overflow
overlap = 30;
} else {
overlap = (p - a * c) / (-1 + c);
}
}
}
$: if (cardsElement && sortedCards && sortedCards.length > 0) {
setTimeout(() => {
fitCards();
}, 100);
}
onMount(() => {
window.addEventListener('resize', fitCards);
});
let sortedCards = [];
$: if ($hand && Object.entries($hand).length !== sortedCards.length) {
sortedCards = Object.entries($hand).sort((a, b) => {
if (a[1].color === b[1].color) {
return parseInt(b[1].symbol) - parseInt(a[1].symbol);
} else {
return a[1].color.charCodeAt(0) - b[1].color.charCodeAt(0);
}
});
}
</script>
<div class="hand">
<div class="hand-bg" on:click={() => choose('hand')} bind:this={backgroundElement} />
<div class="cards" bind:this={cardsElement} style="">
{#each sortedCards as [cardIndex, card], index (cardIndex)}
<div
style="margin-top: {index !== 0 ? overlap : 0}px;
"
animate:flip={{ duration: 500 }}
>
<Card
{...card}
{cardIndex}
focused={$focused.location === 'hand' && $focused.cardIndex === cardIndex}
on:click={() => choose('hand', cardIndex)}
/>
</div>
{/each}
</div>
</div>
<style lang="scss">
.hand {
position: relative;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
width: 100%;
height: 100%;
max-height: 100%;
.hand-bg {
background-color: hsla(0, 0%, 0%, 0.4);
position: absolute;
width: 100%;
height: 100%;
}
.cards {
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: flex-end;
justify-content: center;
padding-right: 1rem;
pointer-events: none;
:global(.card) {
cursor: pointer;
pointer-events: auto;
}
}
}
</style>