Preact
Try the live Preact demo for interactive picker and copy-paste snippets.
Support tier
Partial compatibility through Web Components. There is no
./preact export. Register
defineCustomElements from
universal-datetime-picker/wc. As an alternative, use
preact/compat with the React component entry for a native
React-like API.
Install + deps
npm install universal-datetime-picker
Web Components path: no React required. Compat path: install
preact and alias React to preact/compat, then
use the main package entry like a React app.
Setup
For the WC path, call defineCustomElements() once in
main.tsx, import the stylesheet, and render
datetime-picker* tags with onchange props.
Exact import path
// Web Components path (recommended default for Preact)
import { defineCustomElements } from "universal-datetime-picker/wc";
import "universal-datetime-picker/style.css";
defineCustomElements(); // Alternative: preact/compat + React entry
import DateTime, { DateTimeInput } from "universal-datetime-picker";
import "universal-datetime-picker/style.css"; Minimal example
import { useState } from "preact/hooks";
import { defineCustomElements } from "universal-datetime-picker/wc";
import "universal-datetime-picker/style.css";
defineCustomElements();
export function App() {
const [date, setDate] = useState<Date | null>(null);
return (
<>
<datetime-picker
inline
mode="date"
as-string="false"
onchange={(e: Event) => {
const next = (e as CustomEvent).detail;
setDate(next instanceof Date ? next : null);
}}
/>
<p>Selected: {date ? date.toString() : "null"}</p>
</>
);
} Realistic example
import { useState } from "preact/hooks";
import { defineCustomElements } from "universal-datetime-picker/wc";
import "universal-datetime-picker/style.css";
defineCustomElements();
export function Booking() {
const [date, setDate] = useState<Date | null>(null);
const [open, setOpen] = useState(false);
return (
<>
<datetime-picker-input
placeholder="Pick date & time"
as-string="false"
onchange={(e: Event) => {
const next = (e as CustomEvent).detail;
setDate(next instanceof Date ? next : null);
}}
onopenchange={(e: Event) => {
setOpen(Boolean((e as CustomEvent).detail));
}}
/>
<p>Open: {String(open)}</p>
<p>{date ? date.toLocaleString() : "No date"}</p>
</>
);
} Configuration notes
-
Prefer
./wcunless you already standardize onpreact/compatand want React props likeonChange/asString. -
On the WC path, attributes stay kebab-case and events use
onchange/onopenchange. - The compat + React path follows the React guide once aliases are in place.
-
There is no
universal-datetime-picker/preactsubpath.
Events
-
WC:
onchangeandonopenchangeCustomEvents withdetail -
Compat + React:
onChangeandonOpenChangecallbacks
Customization links
- Web Components
- React for the compat path
- Theming
- Props
Pitfalls
-
Assuming a dedicated Preact build exists. Use
./wcor compat. -
Mixing WC tags with React prop names like
asString={false}on a custom element does not map the same way as React components. Useas-string="false"on the WC path. - Incomplete React aliases break the compat path at runtime.
Troubleshooting
Wrong entry and missing CSS are the common failures. See Troubleshooting.
Related links
Preact live demo · Examples · Props · Troubleshooting · Web Components