-
Notifications
You must be signed in to change notification settings - Fork 4
/
DayGrid.svelte
73 lines (66 loc) · 2.13 KB
/
DayGrid.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
<script lang="ts">
import dayjs, { type Dayjs } from 'dayjs';
import weekOfYear from 'dayjs/plugin/weekOfYear.js';
import isBetween from 'dayjs/plugin/isBetween.js';
dayjs.extend(weekOfYear);
dayjs.extend(isBetween);
import type { CalendarEventModel } from '$models/classes/calendar-event.model';
import { getDayGridForMonth } from '$utils/date';
import GridHeader from './GridHeader.svelte';
import GridCell from './GridCell.svelte';
import CellValue from './CellValue.svelte';
export let cursor: Dayjs;
export let today: Dayjs;
export let todayPing: boolean;
export let hideOutsideDates: boolean;
export let picking: string | null;
export let calendarEvents: CalendarEventModel[];
const weeksToShow = 6;
$: dayGridItems = Array.from(getDayGridForMonth(cursor, weeksToShow).values());
$: weeksOfMonth = Array.from(dayGridItems, (weekOfMonth: Dayjs[]) => {
const [firstDayOfWeek] = weekOfMonth;
return {
yearNumber: firstDayOfWeek.year(),
weekNumber: firstDayOfWeek.week(),
daysOfWeek: weekOfMonth
};
});
$: dayNames = Array.from([...weeksOfMonth][0].daysOfWeek, (day: Dayjs) => day.format('ddd'));
</script>
<GridHeader items={dayNames} />
<div
class="grid w-full grid-cols-{dayNames.length} grid-rows-{weeksOfMonth.length} rounded-b-lg border border-t-0 bg-white dark:border-gray-700 dark:bg-gray-800"
>
{#each weeksOfMonth as { yearNumber, weekNumber, daysOfWeek }, rowIndex}
<div class="contents">
<time id="week-row-{rowIndex}" datetime={`${yearNumber}-W${weekNumber}`} class="sr-only">
{`Week ${weekNumber} of ${yearNumber}`}
</time>
{#each daysOfWeek as dayOfWeek, columnIndex}
<GridCell
hilightOnHover={!!picking}
let:isCurrentMonth
let:descriptor
let:isToday
{cursor}
{rowIndex}
{columnIndex}
lastRowIndex={weeksOfMonth.length - 1}
lastColumnIndex={daysOfWeek.length - 1}
{dayOfWeek}
{today}
>
<CellValue
{calendarEvents}
value={dayOfWeek}
{descriptor}
{todayPing}
{isToday}
shouldHideValue={hideOutsideDates && !isCurrentMonth}
on:dayClick
/>
</GridCell>
{/each}
</div>
{/each}
</div>