Vitral 0.2.18
Form

DateRange

Two dates and the days between them, picked on two months side by side or one, with the span drawn before it is committed.

Import

main.ts
import { DateRange } from '@vitral/vue';

Two months

The default. Pick a start, then an end; the band between them follows the pointer before you commit. Each calendar stops at its own month — the days either side are left blank rather than repeating the month next door.

Your stay
6 nights
<script setup lang="ts">import { DateRange, type DateRangeValue, StackPanel } from '@vitral/vue';import { computed, ref } from 'vue'; const at = (offset: number) => {    const d = new Date();    d.setDate(d.getDate() + offset);    d.setHours(0, 0, 0, 0);    return d;}; const stay = ref<DateRangeValue>({ start: at(3), end: at(9) });const nights = computed(() => (stay.value.start && stay.value.end ? Math.round((stay.value.end.getTime() - stay.value.start.getTime()) / 86400000) : 0));</script> <template>    <StackPanel spacing="0.375rem">        <span id="dr-stay">Your stay</span>        <DateRange v-model="stay" :min-date="at(0)" show-clear-button aria-labelledby="dr-stay" />        <small style="color: var(--vt-text-muted-color)">{{ nights ? `${nights} night${nights === 1 ? '' : 's'}` : 'No dates yet' }}</small>    </StackPanel></template>

One month

The same range in a single calendar, for a form with no room for two. With no neighbour to repeat, the days either side of the month come back.

Dates
Empty
<script setup lang="ts">import { DateRange, type DateRangeValue, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const single = ref<DateRangeValue>({ start: null, end: null });</script> <template>    <StackPanel spacing="0.375rem">        <span id="dr-one">Dates</span>        <DateRange v-model="single" :months="1" placeholder="Pick two dates" aria-labelledby="dr-one" />        <small style="color: var(--vt-text-muted-color)">{{ single.start ? `${single.start.toLocaleDateString()} → ${single.end?.toLocaleDateString() ?? '…'}` : 'Empty' }}</small>    </StackPanel></template>

Month and year

The month and the year in each calendar's title are buttons. The year opens a page of twenty years, a year opens its months, and the month chosen is put in the calendar whose title was pressed, so a range years away is reached without paging there. The range itself is not touched on the way, and Escape goes back to the days.

Report period, up to today
Press a year, then a month · Mon Mar 04 2019 – Thu Apr 18 2019
Inline
SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
2930
SuMoTuWeThFrSa
12345
6789101112
13141516171819
20212223242526
2728293031
No range yet
<script setup lang="ts">import { DateRange, type DateRangeValue, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const period = ref<DateRangeValue>({ start: new Date(2019, 2, 4), end: new Date(2019, 3, 18) });const inline = ref<DateRangeValue>({ start: null, end: null }); const shown = (range: DateRangeValue) => (range.start && range.end ? `${range.start.toDateString()} – ${range.end.toDateString()}` : 'No range yet');</script> <template>    <StackPanel orientation="horizontal" spacing="1.5rem" align="start" wrap>        <StackPanel spacing="0.375rem">            <span id="dr-period">Report period, up to today</span>            <DateRange v-model="period" :max-date="new Date()" aria-labelledby="dr-period" />            <small style="color: var(--vt-text-muted-color)">Press a year, then a month · {{ shown(period) }}</small>        </StackPanel>        <StackPanel spacing="0.375rem">            <span id="dr-month-year-inline">Inline</span>            <DateRange v-model="inline" inline aria-labelledby="dr-month-year-inline" />            <small style="color: var(--vt-text-muted-color)">{{ shown(inline) }}</small>        </StackPanel>    </StackPanel></template>

Without the month or the year grid

Both are on by default and each is a prop. `:year-picker="false"` leaves the year as text, `:month-picker="false"` the month, and with both off the titles are only text and the calendars are paged a month at a time.

Months only, within this school year
Years only
Neither: the titles are only text
<script setup lang="ts">import { DateRange, type DateRangeValue, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const year = new Date().getFullYear(); const term = ref<DateRangeValue>({ start: null, end: null });const years = ref<DateRangeValue>({ start: null, end: null });const paged = ref<DateRangeValue>({ start: null, end: null });</script> <template>    <StackPanel orientation="horizontal" spacing="0.75rem" align="start" wrap>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <span id="dr-term">Months only, within this school year</span>            <DateRange v-model="term" :year-picker="false" :min-date="new Date(year, 1, 1)" :max-date="new Date(year, 11, 15)" placeholder="Pick a range" aria-labelledby="dr-term" />        </StackPanel>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <span id="dr-years">Years only</span>            <DateRange v-model="years" :month-picker="false" placeholder="Pick a range" aria-labelledby="dr-years" />        </StackPanel>        <StackPanel spacing="0.375rem" style="min-width: 16rem">            <span id="dr-paged">Neither: the titles are only text</span>            <DateRange v-model="paged" :month-picker="false" :year-picker="false" placeholder="Pick a range" aria-labelledby="dr-paged" />        </StackPanel>    </StackPanel></template>

With times

`show-time` gives each end a time of day: a field for the start and one for the end, each waiting for its day. Closing the range leaves the calendars open for the times, and each end keeps its time when its day changes. `time-step` sets the minutes between the times listed, and `min-time` and `max-time` bound both ends, here from noon to ten at night. The range can also be typed, each end with its time.

From — to —
<script setup lang="ts">import { DateRange, Label, StackPanel, type DateRangeValue } from '@vitral/vue';import { ref } from 'vue'; const stay = ref<DateRangeValue>({ start: null, end: null });const write = (date: Date | null) => (date ? date.toLocaleString() : '—');</script> <template>    <StackPanel spacing="0.375rem" style="min-width: 20rem">        <Label for="dr-times">Check-in and check-out</Label>        <DateRange id="dr-times" v-model="stay" show-time :time-step="15" min-time="12:00" max-time="22:00" placeholder="Pick the dates and times" />        <small style="color: var(--vt-text-muted-color)">From {{ write(stay.start) }} to {{ write(stay.end) }}</small>    </StackPanel></template>

Typing a range

The box takes a range typed into it: two dates with the separator between them, read on Enter or when the box is left, sorted if typed backwards. A `date-format` of fixed width gives the box its shape, `__/__/____ – __/__/____`. `:manual-input="false"` leaves the box for showing the range, and a press on it opens the calendars; `:mask="false"` keeps the typing and drops the shape.

<script setup lang="ts">import { DateRange, Label, StackPanel, type DateRangeValue } from '@vitral/vue';import { ref } from 'vue'; const typed = ref<DateRangeValue>({ start: null, end: null });const shown = ref<DateRangeValue>({ start: null, end: null });</script> <template>    <StackPanel orientation="horizontal" spacing="0.75rem" align="start" wrap>        <StackPanel spacing="0.375rem" style="min-width: 18rem">            <Label for="dr-typed">Typed into its shape</Label>            <DateRange id="dr-typed" v-model="typed" />        </StackPanel>        <StackPanel spacing="0.375rem" style="min-width: 18rem">            <Label for="dr-shown">Shown only, opens on a press</Label>            <DateRange id="dr-shown" v-model="shown" :manual-input="false" placeholder="Pick a range" />        </StackPanel>    </StackPanel></template>

With the spans people ask for by name

The footer is a slot: presets, a count of nights, whatever the form needs.

<script setup lang="ts">import { Button, DateRange, type DateRangeValue, StackPanel } from '@vitral/vue';import { ref } from 'vue'; const at = (offset: number) => {    const d = new Date();    d.setDate(d.getDate() + offset);    d.setHours(0, 0, 0, 0);    return d;}; const report = ref<DateRangeValue>({ start: at(-30), end: at(0) }); // A range picker is usually next to a few spans people ask for by name.const presets = [    { label: 'Last 7 days', days: 7 },    { label: 'Last 30 days', days: 30 },    { label: 'Last 90 days', days: 90 }];const usePreset = (days: number) => (report.value = { start: at(-days + 1), end: at(0) });</script> <template>    <DateRange v-model="report" :max-date="at(0)" aria-label="Reporting period">        <template #footer="{ nights: n, clear }">            <StackPanel orientation="horizontal" spacing="0.375rem" align="center" wrap as="span">                <Button v-for="p in presets" :key="p.days" :label="p.label" severity="secondary" variant="text" size="small" @click="usePreset(p.days)" />            </StackPanel>            <small style="color: var(--vt-text-muted-color)">{{ n + 1 }} days · <a href="#" @click.prevent="clear">clear</a></small>        </template>    </DateRange></template>

Inline

Without the text box or the popup.

SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
2930
SuMoTuWeThFrSa
12345
6789101112
13141516171819
20212223242526
2728293031
<script setup lang="ts">import { DateRange, type DateRangeValue } from '@vitral/vue';import { ref } from 'vue'; const at = (offset: number) => {    const d = new Date();    d.setDate(d.getDate() + offset);    d.setHours(0, 0, 0, 0);    return d;}; const inline = ref<DateRangeValue>({ start: at(1), end: at(5) });</script> <template>    <DateRange v-model="inline" inline aria-label="Dates" /></template>

Showing the neighbouring days anyway

`show-other-months` overrides what the number of calendars decided. Two months with their edges drawn: February’s last days appear again as March’s first, and a range spanning the boundary is painted twice.

SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
293012345
6789101112
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
<script setup lang="ts">import { DateRange, type DateRangeValue } from '@vitral/vue';import { ref } from 'vue'; const at = (offset: number) => {    const d = new Date();    d.setDate(d.getDate() + offset);    d.setHours(0, 0, 0, 0);    return d;}; const inline = ref<DateRangeValue>({ start: at(1), end: at(5) });</script> <template>    <DateRange v-model="inline" inline show-other-months aria-label="Dates, edges drawn" /></template>

The shape of it

<script setup lang="ts">import { DateRange, type DateRangeValue } from '@vitral/vue';import { ref } from 'vue'; const stay = ref<DateRangeValue>({ start: null, end: null });</script> <template>    <DateRange v-model="stay" :min-date="new Date()" show-clear-button aria-label="Stay" /></template>

API

Read from packages/vue/src/components/DateRange/types.ts, so it says what the component actually accepts.

Props

NameTypeDescription
months numberHow many months to show side by side. Two is the default and the reason this component exists: a span that crosses a month boundary is chosen in one gesture rather than by paging. One shows the same range in a single calendar.
showOtherMonths booleanWhether the days either side of each month are drawn. Left unset, they are on a single calendar and off on several: with two months side by side the last days of one are the first days of the next, so the same date appears twice, a range paints across both copies, and the boundary between the calendars stops meaning anything. The cells stay — the grid keeps its six rows and its height — they are simply left empty.
dateFormat stringA `formatDate` pattern; defaults to the locale's `dateFormat`.
separator stringBetween the two dates in the text box.
manualInput booleanWhether a range can be typed into the text box: two dates with the separator between them, each with its time under `showTime`, read on Enter or when the box is left. On by default; off, the box only shows the range and a press on it opens the calendars.
mask boolean | MaskPropWhether the text box types into the shape the range is written in. On by default, for a `dateFormat` of fixed width: `99/99/9999 – 99/99/9999`. `false` leaves the box free; a mask of your own, as InputMask takes, replaces the derived one.
minDate Date | null—
maxDate Date | null—
disabledDates Date[]—
disabledDays number[]Weekdays that cannot be chosen, 0 = Sunday.
monthPicker booleanWhether the month in each calendar's title opens a grid of the twelve months. On by default; off, the month is only text and is reached by paging.
yearPicker booleanWhether the year in each calendar's title opens a grid of years. On by default; off, the year is only text. With the month grid off as well, the calendars can only be paged a month at a time.
showTime booleanTake a time of day with each end. The calendars gain a time field for the start and one for the end, the text box writes each time after its date, and closing the range leaves the calendars open for the times. A time field waits for its end to be chosen. Off by default.
timeStep numberMinutes between the times the time fields offer. Defaults to 30; any minute can still be typed.
minTime number | string | nullThe earliest time of day, as minutes past midnight or `'08:00'`. A time chosen or typed before it is moved up to it.
maxTime number | string | nullThe latest time of day, likewise.
hour12 booleanForce twelve- or twenty-four-hour time; defaults to what the locale writes.
showClearButton booleanA footer button that empties the range.
firstDayOfWeek number0 = Sunday; defaults to the locale's `firstDayOfWeek`.
placeholder string—
inline booleanShow the calendars in place, without a text box or popup.
size Size—
variant InputVariantDefaults to the plugin's `inputVariant`.
invalid boolean—
disabled boolean—
readonly boolean—
fluid boolean—
placement OverlayPlacement—
appendTo string`'body'` (the default), `'self'` to render in place, or a selector.

Plus pt, dt and unstyled from BaseProps, see pass-through and unstyled mode.

Emits

EventPayloadDescription
rangeSelect range: DateRangeValueBoth ends are down. Fires once a range is whole, not on the first press; with `showTime`, also when a time changes.
clear ——
show ——
hide ——
monthChange event: { month: number; year: number }—

Slots

NameSlot propsDescription
date (props: { date: Date; day: number; today: boolean; inRange: boolean; end: 'start' | 'end' | null; disabled: boolean; otherMonth: boolean })A day's content.
footer (props: { range: DateRangeValue; nights: number; clear: () => void })Under the calendars: presets, a count of nights, a pair of buttons.