Multiple calendars
Calends draws one events array, so showing several calendars at once (a team's, a person's, and the public holidays, say) is a matter of merging them before they go in. Toggling one off is a filter, and coloring them is a field on each event.
The legend is the CalendarSources recipe, in the toolbar slot of MonthCalendar. It works the same in every view's toolbar.
npx shadcn-vue@latest add https://benjivm.github.io/calends/r/calendar-sources.jsonThe recipe's source
<script lang="ts">
export interface CalendarSource {
id: string;
label: string;
/** Any CSS color, like `var(--chart-1)`. Give the source's events the same `color`. */
color?: string;
}
</script>
<script setup lang="ts" generic="S extends CalendarSource">
/**
* A legend of calendar sources, each a toggle. It only tracks which are hidden: the app filters
* its events with `v-model:hidden`, so a source's events can keep loading while it's off.
*/
withDefaults(
defineProps<{
sources: readonly S[];
/** Names the group for screen readers. */
label?: string;
}>(),
{ label: 'Calendars' },
);
defineSlots<{
source?(props: { source: S; visible: boolean }): unknown;
}>();
const hidden = defineModel<string[]>('hidden', { default: () => [] });
function toggle(id: string) {
hidden.value = hidden.value.includes(id)
? hidden.value.filter((other) => other !== id)
: [...hidden.value, id];
}
</script>
<template>
<div
role="group"
:aria-label="label"
class="flex flex-wrap items-center gap-1.5"
>
<button
v-for="source in sources"
:key="source.id"
type="button"
:aria-pressed="!hidden.includes(source.id)"
:style="{ '--source-color': source.color }"
@click="toggle(source.id)"
class="group inline-flex h-8 items-center gap-2 rounded-md border border-border bg-background px-2.5 text-sm font-medium text-foreground shadow-xs outline-none [--source-color:var(--primary)] hover:bg-accent hover:text-accent-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-[pressed=false]:text-muted-foreground"
>
<span
aria-hidden="true"
class="flex size-3.5 items-center justify-center rounded-sm border-2 border-(--source-color) bg-(--source-color) text-background group-aria-[pressed=false]:bg-transparent"
>
<svg
class="size-2.5 group-aria-[pressed=false]:hidden"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="4"
stroke-linecap="round"
stroke-linejoin="round"
>
<path d="M20 6 9 17l-5-5" />
</svg>
</span>
<slot
name="source"
:source="source"
:visible="!hidden.includes(source.id)"
>
{{ source.label }}
</slot>
</button>
</div>
</template>Merging
Give each source an id and a color, and tag its events with both. Prefix the event ids too, since two sources can each have an event 42, and ids must be unique across the whole array:
import { computed, ref } from 'vue';
const calendars = [
{ id: 'work', label: 'Work', color: 'var(--chart-1)' },
{ id: 'personal', label: 'Personal', color: 'var(--chart-2)' },
{ id: 'holidays', label: 'Holidays', color: 'var(--chart-3)' },
];
const loaded = ref<Record<string, AppEvent[]>>({});
const hidden = ref<string[]>([]);
const events = computed(() =>
calendars
.filter((calendar) => !hidden.value.includes(calendar.id))
.flatMap((calendar) =>
(loaded.value[calendar.id] ?? []).map((event) => ({
...event,
id: `${calendar.id}:${event.id}`,
calendar: calendar.id,
color: calendar.color,
})),
),
);<MonthCalendar :events="events">
<template #toolbar>
<CalendarSources v-model:hidden="hidden" :sources="calendars" />
</template>
</MonthCalendar>CalendarSources only tracks which ids are hidden, so a hidden calendar keeps its loaded events and comes back instantly. Every recipe tints its default bar with an event's color, which can be any CSS color. The chart-1 to chart-5 tokens are part of shadcn-vue's theme, so they follow your palette and dark mode. Without the field, bars use primary.
For the primitives, put the color on the element yourself, like :style="{ '--event-color': event.source.color }", and style with it.
Loading by range
To fetch only what's in view, watch the period and load each calendar for it. The month grid pads its weeks with days from the months either side, and visibleMonthRange covers them:
import { watch } from 'vue';
import { visibleMonthRange } from '@calends/core';
const loading = ref(false);
watch(
date,
async (value) => {
const { start: from, end: to } = visibleMonthRange(value);
loading.value = true;
const results = await Promise.all(
calendars.map((calendar) => fetchEvents(calendar.id, from, to)),
);
loaded.value = Object.fromEntries(
calendars.map((calendar, index) => [calendar.id, results[index]]),
);
loading.value = false;
},
{ immediate: true },
);Pass loading to the recipe to show placeholder bars while that runs. A source that loads slowly can fill in on its own instead: set its entry in loaded as soon as it arrives.
Committing changes
An event that's dragged comes back with its calendar and prefixed id, so the drop handler knows where to send it:
import type { EventDropDetail } from '@calends/vue';
function moveEvent({ event, start, end }: EventDropDetail<(typeof events.value)[number]>) {
const id = event.id.slice(event.calendar.length + 1);
loaded.value[event.calendar] = loaded.value[event.calendar].map((item) =>
item.id === id ? { ...item, start, end } : item,
);
saveEvent(event.calendar, id, { start, end });
}To keep a read-only calendar (like holidays) from being dragged, pass :can-edit="(event) => event.calendar !== 'holidays'".