|
8 | 8 | endOfMonth as endOfMonthFunc,
|
9 | 9 | format,
|
10 | 10 | addMonths,
|
11 |
| - subMonths, |
12 | 11 | isSameDay,
|
13 | 12 | isWithinInterval,
|
14 | 13 | } from 'date-fns';
|
|
33 | 32 | startOfMonthFunc(selected.from)) ||
|
34 | 33 | startOfMonthFunc(new Date());
|
35 | 34 |
|
36 |
| - // TODO: Variable shows error if set to `endOfMonth` maybe due to import |
37 |
| - $: endOfMonth2 = endOfMonthFunc(startOfMonth); |
| 35 | + $: endOfMonth = endOfMonthFunc(startOfMonth); |
38 | 36 | $: monthDaysByWeek = getMonthDaysByWeek(startOfMonth);
|
39 | 37 |
|
| 38 | + /** |
| 39 | + * Hide controls and date. Useful to control externally |
| 40 | + */ |
| 41 | + export let hideControls = false; |
| 42 | +
|
40 | 43 | /**
|
41 | 44 | * Show days before and after selected month
|
42 | 45 | */
|
|
65 | 68 | $: isDayHidden = (day: Date) => {
|
66 | 69 | const isCurrentMonth = isWithinInterval(day, {
|
67 | 70 | start: startOfMonth,
|
68 |
| - end: endOfMonth2, |
| 71 | + end: endOfMonth, |
69 | 72 | });
|
70 | 73 | return !isCurrentMonth && !showOutsideDays;
|
71 | 74 | };
|
72 | 75 |
|
73 | 76 | $: isDayFaded = (day: Date) => {
|
74 | 77 | const isCurrentMonth = isWithinInterval(day, {
|
75 | 78 | start: startOfMonth,
|
76 |
| - end: endOfMonth2, |
| 79 | + end: endOfMonth, |
77 | 80 | });
|
78 | 81 | return !isCurrentMonth && showOutsideDays;
|
79 | 82 | };
|
80 | 83 | </script>
|
81 | 84 |
|
82 |
| -<div class="flex m-2"> |
83 |
| - <Button |
84 |
| - icon={mdiChevronLeft} |
85 |
| - class="p-2" |
86 |
| - on:click={() => (startOfMonth = subMonths(startOfMonth, 1))} |
87 |
| - /> |
88 |
| - <div class="flex flex-1 items-center justify-center"> |
89 |
| - <span>{format(startOfMonth, 'MMMM yyyy')}</span> |
| 85 | +{#if !hideControls} |
| 86 | + <div class="flex m-2"> |
| 87 | + <Button |
| 88 | + icon={mdiChevronLeft} |
| 89 | + class="p-2" |
| 90 | + on:click={() => (startOfMonth = addMonths(startOfMonth, -1))} |
| 91 | + /> |
| 92 | + |
| 93 | + <div class="flex flex-1 items-center justify-center"> |
| 94 | + <span>{format(startOfMonth, 'MMMM yyyy')}</span> |
| 95 | + </div> |
| 96 | + |
| 97 | + <Button |
| 98 | + icon={mdiChevronRight} |
| 99 | + class="p-2" |
| 100 | + on:click={() => (startOfMonth = addMonths(startOfMonth, 1))} |
| 101 | + /> |
90 | 102 | </div>
|
91 |
| - <Button |
92 |
| - icon={mdiChevronRight} |
93 |
| - class="p-2" |
94 |
| - on:click={() => (startOfMonth = addMonths(startOfMonth, 1))} |
95 |
| - /> |
96 |
| -</div> |
| 103 | +{/if} |
97 | 104 |
|
98 | 105 | <div class="flex">
|
99 | 106 | {#each monthDaysByWeek[0] ?? [] as day (day.getDate())}
|
|
0 commit comments