Integrations / Vue
Vue
@litopis/vue provides a component with standard Vue props and
v-model.
Quick start
npm install @litopis/vue @litopis/dom vue
Add this single-file component to src/App.vue. The stylesheet import
gives the picker its base styles.
<script setup lang="ts">
import { LitopisDatePicker } from "@litopis/vue";
import "@litopis/dom/styles/base.css";
</script>
<template>
<main>
<LitopisDatePicker mode="popover" label="Appointment date" />
</main>
</template>
Interactive example
Value: current date
Value
Use v-model when your app needs to read or set the selected date.
<script setup lang="ts">
import { ref } from "vue";
import { LitopisDatePicker, type LitopisDateValue } from "@litopis/vue";
import "@litopis/dom/styles/base.css";
const date = ref<LitopisDateValue | null>(null);
</script>
<template>
<main>
<LitopisDatePicker v-model="date" mode="popover" label="Start date" />
</main>
</template>
Component ref
The component instance exposes controller and getISOValue().
<script setup lang="ts">
import { ref } from "vue";
import { LitopisDatePicker } from "@litopis/vue";
import "@litopis/dom/styles/base.css";
const picker = ref<InstanceType<typeof LitopisDatePicker> | null>(null);
const value = ref("");
function readDate() {
value.value = picker.value!.getISOValue() || "No date selected";
}
</script>
<template>
<LitopisDatePicker ref="picker" label="Appointment date" />
<button type="button" @click="readDate">Read date</button>
<output>{{ value }}</output>
</template>
Range
Use one range object or bind its endpoints separately when the form already stores
from and to fields. These are alternative bindings.
<script setup lang="ts">
import { ref } from "vue";
import { LitopisDatePicker, type LitopisDateRange } from "@litopis/vue";
const stay = ref<LitopisDateRange>({ start: null, end: null });
</script>
<template>
<LitopisDatePicker
v-model="stay"
selection="range"
layout="split"
/>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { LitopisDatePicker, type LitopisDateValue } from "@litopis/vue";
const from = ref<LitopisDateValue | null>(null);
const to = ref<LitopisDateValue | null>(null);
</script>
<template>
<LitopisDatePicker
v-model:from="from"
v-model:to="to"
selection="range"
layout="split"
/>
</template>
Unavailable dates
Bind disabled-dates to an availability snapshot. Pass
is-date-disabled for a synchronous predicate. A complete range cannot
include an unavailable day. The picker emits visible-month-change with
the first day whenever its displayed month changes. Navigation or selection can
trigger it. It does not emit on initial render. Click the button to replace the
snapshot and see Vue update the picker.
This example uses date helpers from @litopis/core. Add it to the install
command above.
npm install @litopis/core
<script setup lang="ts">
import { addDays, getToday } from "@litopis/core";
import { ref } from "vue";
import {
LitopisDatePicker,
type LitopisDateValue,
} from "@litopis/vue";
import "@litopis/dom/styles/base.css";
const today = getToday();
const firstUnavailableDate = addDays(today, 3);
const secondUnavailableDate = addDays(today, 6);
const appointment = ref<LitopisDateValue | null>(null);
const disabledDates = ref<LitopisDateValue[]>([firstUnavailableDate]);
function toggleSecondUnavailableDate(): void {
disabledDates.value = disabledDates.value.length === 1
? [firstUnavailableDate, secondUnavailableDate]
: [firstUnavailableDate];
}
</script>
<template>
<div>
<LitopisDatePicker
v-model="appointment"
:today="today"
:disabled-dates="disabledDates"
mode="popover"
label="Appointment date"
/>
<button type="button" @click="toggleSecondUnavailableDate">
Toggle second unavailable date
</button>
</div>
</template>
Custom input
Use the input slot for your own input component or element. Litopis keeps
its classes and placeholder while supplying the picker behavior and accessibility
attributes.
<LitopisDatePicker v-model="date">
<template #input>
<input class="my-input" placeholder="Choose a date" />
</template>
</LitopisDatePicker>
Use the input and end-input slots for a split range.
Styling
Pass class to the component root. Import one Litopis stylesheet in your
application entry.