forked from techniq/svelte-ux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonth.svelte
More file actions
161 lines (142 loc) · 4.55 KB
/
Month.svelte
File metadata and controls
161 lines (142 loc) · 4.55 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<script lang="ts">
// import { fade, slide } from 'svelte/transition';
// import { fly } from '../utils/transition';
import {
startOfDay as startOfDayFunc,
endOfDay as endOfDayFunc,
startOfMonth as startOfMonthFunc,
endOfMonth as endOfMonthFunc,
addMonths,
isSameDay,
isWithinInterval,
} from 'date-fns';
import { mdiChevronLeft, mdiChevronRight } from '@mdi/js';
import { getMonthDaysByWeek, PeriodType } from '../utils/date';
import type { SelectedDate } from '../utils/date';
import { hasKeyOf } from '../types/typeGuards';
import Button from './Button.svelte';
import DateButton from './DateButton.svelte';
import { getSettings } from '.';
export let selected: SelectedDate | undefined = undefined;
export let startOfMonth =
(selected instanceof Date && startOfMonthFunc(selected)) ||
(selected instanceof Array && selected.length && startOfMonthFunc(selected[0])) ||
(selected &&
hasKeyOf<{ from: Date }>(selected, 'from') &&
selected.from &&
startOfMonthFunc(selected.from)) ||
startOfMonthFunc(new Date());
const { format } = getSettings();
$: dateFormat = $format.settings.formats.dates;
$: endOfMonth = endOfMonthFunc(startOfMonth);
$: monthDaysByWeek = getMonthDaysByWeek(startOfMonth, dateFormat.weekStartsOn);
/**
* Hide controls and date. Useful to control externally
*/
export let hideControls = false;
/**
* Show days before and after selected month
*/
export let showOutsideDays: boolean | undefined = undefined;
/**
* Day(s) to disable (not selectable)
*/
export let disabledDays: any = undefined; // TODO: Improve types - See isDayDisabled
$: isDayDisabled = (date: Date) => {
return disabledDays instanceof Function
? disabledDays(date)
: disabledDays instanceof Date
? isSameDay(date, disabledDays)
: disabledDays instanceof Array
? disabledDays.some((d) => isSameDay(date, d))
: disabledDays instanceof Object
? isWithinInterval(date, {
start: startOfDayFunc(disabledDays.from),
end: endOfDayFunc(disabledDays.to || disabledDays.from),
})
: false;
};
$: isDayHidden = (day: Date) => {
const isCurrentMonth = isWithinInterval(day, {
start: startOfMonth,
end: endOfMonth,
});
return !isCurrentMonth && !showOutsideDays;
};
$: isDayFaded = (day: Date) => {
const isCurrentMonth = isWithinInterval(day, {
start: startOfMonth,
end: endOfMonth,
});
return !isCurrentMonth && showOutsideDays;
};
</script>
{#if !hideControls}
<div class="flex m-2">
<Button
icon={mdiChevronLeft}
class="p-2"
on:click={() => (startOfMonth = addMonths(startOfMonth, -1))}
/>
<div class="flex flex-1 items-center justify-center">
<span>{$format(startOfMonth, PeriodType.MonthYear)}</span>
</div>
<Button
icon={mdiChevronRight}
class="p-2"
on:click={() => (startOfMonth = addMonths(startOfMonth, 1))}
/>
</div>
{/if}
<div class="flex">
{#each monthDaysByWeek[0] ?? [] as day (day.getDate())}
<div class="flex-1 text-center">
<span class="text-xs text-surface-content/50">
{$format(day, PeriodType.Day, { custom: 'eee' })}
</span>
</div>
{/each}
</div>
<div class="grid grid-cols-7 grid-rows-6 gap-y-4">
{#each monthDaysByWeek ?? [] as week, weekIndex (weekIndex)}
{#each week ?? [] as day (day.valueOf())}
<DateButton
date={day}
periodType={PeriodType.Day}
bind:selected
hidden={isDayHidden(day)}
fade={isDayFaded(day)}
disabled={isDayDisabled(day)}
on:dateChange
/>
{/each}
{/each}
</div>
<!--
TODO: Transition
- [ ] Detect direction and set +/- accordingly (left-to-right or right-to-left)
- [ ] Fix changing period types on DateRange (|local not a workaround)
-->
<!-- <div class="grid overflow-hidden">
{#key startOfMonth.valueOf()}
<div
class="col-span-full row-span-full grid grid-cols-7 grid-rows-6 gap-y-4"
in:fly|local={{ x: '-100%' }}
out:fly|local={{ x: '100%' }}
>
{#each monthDaysByWeek ?? [] as week, weekIndex (weekIndex)}
{#each week ?? [] as day (day.valueOf())}
<DateButton
date={day}
periodType={PeriodType.Day}
bind:selected
hidden={isDayHidden(day)}
fade={isDayFaded(day)}
disabled={isDayDisabled(day)}
on:dateChange
/>
{/each}
{/each}
</div>
{/key}
</div> -->