Add month grid
This commit is contained in:
1 parent
e270c5a39f
commit
a1cc191fde
1 file changed
+84
@@ -0,0 +1,84 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { CalEvent } from '../lib/api';
|
||||||
|
import { iso, monthLabel, monthWeeks, parse } from '../lib/dates';
|
||||||
|
|
||||||
|
let {
|
||||||
|
month,
|
||||||
|
events,
|
||||||
|
colorFor = () => 'var(--accent)',
|
||||||
|
onday = null,
|
||||||
|
}: {
|
||||||
|
month: string;
|
||||||
|
events: CalEvent[];
|
||||||
|
colorFor?: (e: CalEvent) => string;
|
||||||
|
onday?: ((date: string) => void) | null;
|
||||||
|
} = $props();
|
||||||
|
|
||||||
|
const weeks = $derived(monthWeeks(month));
|
||||||
|
const byDay = $derived.by(() => {
|
||||||
|
const m = new Map<string, CalEvent[]>();
|
||||||
|
for (const e of events) {
|
||||||
|
const d = parse(e.date);
|
||||||
|
const end = parse(e.end_date ?? e.date);
|
||||||
|
for (let i = 0; d <= end && i < 62; i++) {
|
||||||
|
const k = iso(d);
|
||||||
|
if (!m.has(k)) m.set(k, []);
|
||||||
|
m.get(k)!.push(e);
|
||||||
|
d.setDate(d.getDate() + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
});
|
||||||
|
// Weekday names in the device's language, Sunday first (2026-01-04 was a Sunday).
|
||||||
|
const names = Array.from({ length: 7 }, (_, i) =>
|
||||||
|
new Date(2026, 0, 4 + i).toLocaleDateString(undefined, { weekday: 'short' }),
|
||||||
|
);
|
||||||
|
const today = iso(new Date());
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<section class="grid-wrap" aria-label={monthLabel(month)}>
|
||||||
|
<h3>{monthLabel(month)}</h3>
|
||||||
|
<div class="grid">
|
||||||
|
{#each names as n}<div class="head">{n}</div>{/each}
|
||||||
|
{#each weeks as week}
|
||||||
|
{#each week as day}
|
||||||
|
{@const list = byDay.get(day) ?? []}
|
||||||
|
{@const outside = day.slice(0, 7) !== month}
|
||||||
|
{#snippet cell()}
|
||||||
|
<span class="num">{parse(day).getDate()}</span>
|
||||||
|
{#each list.slice(0, 3) as e (e.id)}
|
||||||
|
<span class="chip" style:--c={colorFor(e)} title={e.title}>{e.title}</span>
|
||||||
|
{/each}
|
||||||
|
{#if list.length > 3}<span class="more">+{list.length - 3}</span>{/if}
|
||||||
|
{/snippet}
|
||||||
|
{#if onday}
|
||||||
|
<button type="button" class="day" class:outside class:today={day === today} onclick={() => onday(day)}
|
||||||
|
aria-label={`${parse(day).toLocaleDateString(undefined, { month: 'long', day: 'numeric' })}: ${list.length} events. Add an event`}>
|
||||||
|
{@render cell()}
|
||||||
|
</button>
|
||||||
|
{:else}
|
||||||
|
<div class="day" class:outside class:today={day === today}>{@render cell()}</div>
|
||||||
|
{/if}
|
||||||
|
{/each}
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 2px; }
|
||||||
|
.head { font-size: 0.75rem; color: var(--muted); text-align: center; padding: 4px 0; }
|
||||||
|
.day {
|
||||||
|
min-height: 72px; padding: 3px; border-radius: 6px; background: var(--surface); border: 1px solid var(--border);
|
||||||
|
display: flex; flex-direction: column; align-items: stretch; gap: 2px; text-align: left; font-size: 0.7rem;
|
||||||
|
overflow: hidden; min-width: 0; justify-content: flex-start;
|
||||||
|
}
|
||||||
|
button.day { cursor: pointer; }
|
||||||
|
.outside { opacity: 0.45; }
|
||||||
|
.today .num { color: var(--accent); font-weight: 700; }
|
||||||
|
.num { font-size: 0.75rem; }
|
||||||
|
.chip {
|
||||||
|
display: block; background: color-mix(in srgb, var(--c) 22%, transparent); border-left: 3px solid var(--c);
|
||||||
|
border-radius: 3px; padding: 0 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
|
||||||
|
}
|
||||||
|
.more { color: var(--muted); }
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user