Tailwind CSS Datepicker Plugin API
Headless, unstyled datepicker plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Datepicker plugin via npm, else you can skip this step if you are already using Preline UI as a package.
-
Install the plugin
Install
@preline/datepickervia npmTerminalnpm i @preline/select @preline/datepicker -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/select */ /* [!code highlight:2] */ @source "../node_modules/@preline/select/*.js"; @import "./node_modules/@preline/select/variants.css"; /* @preline/datepicker */ /* [!code highlight:3] */ @source "../node_modules/@preline/datepicker/*.js"; @import "./node_modules/@preline/datepicker/variants.css"; @import "./node_modules/@preline/datepicker/styles.css"; /* [!code highlight:1] */ @import "./node_modules/@preline/datepicker/theme.css";main.css@import "tailwindcss"; /* @preline/select */ /* [!code highlight:3] */ @source "../node_modules/@preline/select/*.js"; @import "./node_modules/@preline/select/variants.css"; @import "./node_modules/@preline/select/theme.css"; /* @preline/datepicker */ /* [!code highlight:3] */ @source "../node_modules/@preline/datepicker/*.js"; @import "./node_modules/@preline/datepicker/variants.css"; @import "./node_modules/@preline/datepicker/styles-utility.css";Use
styles.csstogether withtheme.cssfor the default themed appearance. Usestyles-utility.cssinstead when you plan to style the datepicker entirely with Tailwind utility classes, in this casetheme.cssis not needed.@preline/selectis required for styling the embedded select dropdowns used incustom-selectmode. -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.
vanilla-calendar-proand the Advanced Select plugin are bundled inside@preline/datepicker- no separate imports needed.JavaScript (auto)import "@preline/datepicker";JavaScript (non-auto)import HSDatepicker from "@preline/datepicker/non-auto"; HSDatepicker.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#datepicker"); if (el) new HSDatepicker(el);
Data Options
Vanilla Calendar Pro
Please note that this component requires the use of the Vanilla Calendar Pro plugin. Most options available in the plugin are available in our wrapper.
Methods
Vanilla Calendar Pro
Please note that this component requires the use of the Vanilla Calendar Pro plugin. Most methods available in the plugin are available in our wrapper.
The HSDatepicker object is contained within the global window object
Get current state.
const form = document.querySelector('#subscription-form');
form.addEventListener('submit', (evt) => {
evt.preventDefault();
const { element } = HSDatepicker.getInstance('#datepicker', true);
const { selectedDates, selectedTime } = element.getCurrentState();
const formData = new FormData(event.target);
const serializedData = Object.fromEntries(formData.entries());
serializedData.selectedDates = selectedDates;
serializedData.selectedTime = selectedTime;
fetch('/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(serializedData)
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
});
Events
When select changes event example.
const { element } = HSDatepicker.getInstance('#datepicker', true);
element.on('change', (data) => {...});
Actions
Vanilla Calendar Pro
Please note that this component requires the use of the Vanilla Calendar Pro plugin. Most actions available in the plugin are available in our wrapper.
When change month arrow was clicked.
const datepicker = new HSDatepicker(document.querySelector('#datepicker'), {
onClickArrow: () => {
alert('Arrow clicked!');
}
});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Advanced Datepicker
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View Advanced Datepicker examples