Date adds inline void elements that display a date label inside text. One required value preserves either a canonical YYYY-MM-DD date or authored text such as sometime next week.
date element.date.insert transaction command.value for canonical dates or authored date text.<date value="YYYY-MM-DD" /> and <date text="…" />, and reading of <date> child text.DateKit installs DatePlugin with the registry DateElement.
'use client';
import { cn } from 'cn';
import { formatDateValue, parseCanonicalDateValue } from 'platejs/date';
import { DatePlugin } from 'platejs/date/react';
import {
type EditorElementProps,
EditorElement,
useEditorReadOnly,
} from 'platejs/react';
import * as React from 'react';
import { Calendar } from '@/components/ui/calendar';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
import { inlineSuggestionVariants } from '@/lib/inline-suggestion';
const DAY_MS = 86_400_000;
const RELATIVE_DAYS: Partial<Record<number, string>> = {
[-1]: 'Yesterday',
0: 'Today',
1: 'Tomorrow',
};
const dateFormat = new Intl.DateTimeFormat('en-US', {
dateStyle: 'long',
timeZone: 'UTC',
});
const getDateLabel = (value: string) => {
if (!parseCanonicalDateValue(value)) return value;
const day = Date.parse(value);
const today = Date.parse(formatDateValue(new Date()));
return RELATIVE_DAYS[(day - today) / DAY_MS] ?? dateFormat.format(day);
};
export function DateElement(props: EditorElementProps<typeof DatePlugin>) {
const { editor, element } = props;
const readOnly = useEditorReadOnly();
const openRef = React.useRef(false);
const openAtPointerDownRef = React.useRef(false);
const [open, setOpen] = React.useState(false);
const selectedDate = parseCanonicalDateValue(element.value);
const setOpenState = React.useCallback((nextOpen: boolean) => {
openRef.current = nextOpen;
setOpen(nextOpen);
}, []);
const trigger = (
<button
className={cn(
'w-fit cursor-pointer rounded-sm bg-muted px-1 text-muted-foreground',
inlineSuggestionVariants({ on: 'parent' })
)}
contentEditable={false}
draggable
onClick={(event) => {
const beganOpen =
event.detail === 0 ? openRef.current : openAtPointerDownRef.current;
if (readOnly || beganOpen) return;
event.preventDefault();
setOpenState(true);
}}
onPointerDown={() => {
openAtPointerDownRef.current = openRef.current;
}}
type="button"
>
{getDateLabel(element.value)}
</button>
);
return (
<EditorElement
{...props}
className="inline-block"
attributes={{
...props.attributes,
contentEditable: false,
}}
>
{readOnly ? (
trigger
) : (
<Popover open={open} onOpenChange={setOpenState}>
<PopoverTrigger asChild>{trigger}</PopoverTrigger>
<PopoverContent className="w-auto p-0">
<Calendar
defaultMonth={selectedDate}
selected={selectedDate}
onSelect={(date) => {
if (!date) return;
editor.update.nodes.set(
{ value: formatDateValue(date) },
{ at: element }
);
}}
mode="single"
autoFocus
/>
</PopoverContent>
</Popover>
)}
{props.children}
</EditorElement>
);
}
export const DateKit = [DatePlugin.configure({ component: DateElement })];'use client';
import { cn } from 'cn';
import { formatDateValue, parseCanonicalDateValue } from 'platejs/date';
import { DatePlugin } from 'platejs/date/react';
import {
type EditorElementProps,
EditorElement,
useEditorReadOnly,
} from 'platejs/react';
import * as React from 'react';
import { Calendar } from '@/components/ui/calendar';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
import { createEditor } from 'platejs/react';
import { DateKit } from '@/components/editor/date';
export const editor = createEditor({
plugins: DateKit,
});import { createEditor } from 'platejs/react';
import { DateKit } from '@/components/editor/date';
export const editor = createEditor({
plugins: DateKit,
date owns the inline wrapper, label, popover and calendar picker.
'use client';
import { cn } from 'cn';
import { formatDateValue, parseCanonicalDateValue } from 'platejs/date';
import { DatePlugin } from 'platejs/date/react';
import {
type EditorElementProps,
EditorElement,
useEditorReadOnly,
} from 'platejs/react';
import * as React from 'react';
import { Calendar } from '@/components/ui/calendar';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
The registry insert toolbar maps PLUGINS.date to the plugin-owned update.
import { BaseDatePlugin } from 'platejs/date';
import { PLUGINS } from 'platejs';
export const insertInlineMap = {
[PLUGINS.date]: (editor) =>
editor.plugin(BaseDatePlugin).update.insert({}, { select: true }),
};import { BaseDatePlugin } from 'platejs/date';
| Layer | Owner | What It Does |
|---|---|---|
platejs/date | Package | Exports BaseDatePlugin, formatDateValue and parseCanonicalDateValue, and maps the <date> Markdown tag. |
platejs/date/react | Package | Exports DatePlugin. |
date | Registry | Adds DatePlugin.configure({ component: DateElement }) and renders the label and calendar popover. |
date-static | Registry | Adds BaseDatePlugin.configure({ component: DateElementStatic }) and renders the full-date label. |
platejs/markdown | Package | Runs BaseDatePlugin's <date> mapping when Markdown is parsed or serialized. |
BaseDatePlugin is inline and void. The text child exists only to satisfy Plate's element shape.
Use the React plugin when the editor renders the calendar popover.
import { DatePlugin } from 'platejs/date/react';
import { createEditor } from 'platejs/react';
import { DateElement } from '@/components/editor/date';
export const editor = createEditor({
plugins: [DatePlugin.configure({ component: DateElement })],
});import { DatePlugin } from 'platejs/date/react';
import { createEditor } from 'platejs/react';
import { DateElement } from '@/components/editor/date';
export const editor = createEditor({
plugins: [DatePlugin.configure({ component: DateElement })],
});Use the base kit when rendering read-only output with platejs/static.
import { BaseDatePlugin, parseCanonicalDateValue } from 'platejs/date';
import { type EditorElementProps, EditorElement } from 'platejs/static';
import * as React from 'react';
const dateFormat = new Intl.DateTimeFormat('en-US', {
dateStyle: 'long',
timeZone: 'UTC',
});
const getDateLabel = (value: string) =>
parseCanonicalDateValue(value) ? dateFormat.format(Date.parse(value)) : value;
export
DatePlugin exposes date insertion through the date transaction group.
editor.update((tx) =>
tx.date.insert({ value: '2026-03-23' }, { select: true })
);editor.update((tx) =>
tx.date.insert({ value: '2026-03-23' }, { select: true })
);Date elements store one non-empty authored string. Parsing is derived from that value rather than persisted as a second field.
const value = [
{
children: [
{ text: 'Due ' },
{
children: [{ text: '' }],
type: 'date',
value: '2026-03-23',
},
{ text: '.' },
],
type: 'paragraph',
},
];const value = [
{
children: [
{ text: 'Due ' },
{
children: [{ text: '' }],
type: 'date',
value: '2026-03-23',
},
{ text: '.' },
],
type: 'paragraph',
},
];| Field | Type | Notes |
|---|---|---|
type | 'date' | Persisted element type owned by BaseDatePlugin. |
children | [{ text: '' }] | Required Plate child for the inline void element. |
value | string | Required canonical date or authored date text. |
update.insert stores one string.
| Input | Stored Value |
|---|---|
No value | Today's local day as YYYY-MM-DD. |
Date object | formatDateValue(value); an invalid Date inserts nothing. |
| String | The trimmed string, such as 2026-03-23 or sometime next week; a blank string inserts nothing. |
parseCanonicalDateValue(value) returns a Date for a valid YYYY-MM-DD day and undefined for authored text. A string passed to insert is stored as written, so pass a Date to store a day.
The v54 migration and the Markdown reader read toDateString() text, such as Mon Mar 23 2026, as the day 2026-03-23 when its weekday matches. Other text stays as written.
Each copied component builds its own label. DateElement shows Today, Yesterday or Tomorrow for nearby days and the full date, such as March 23, 2026, otherwise. It reads the clock while rendering, so server HTML uses the server's day. DateElementStatic always prints the full date, so an export never goes stale. Both print US English; edit the copied dateFormat and words to change the language.
The registry element is display-only while read-only. In editable mode, clicking the inline label opens a calendar popover.
| State | Behavior |
|---|---|
canonical value | The trigger shows Today, Yesterday or Tomorrow for nearby days and the full date otherwise. |
other value | The trigger shows the authored string. |
| calendar selection | The node is set to { value: formatDateValue(date) }. |
The registry element uses contentEditable={false} on the inline wrapper, so users edit the date through the calendar instead of typing inside the void node.
Canonical values serialize as a self-closing date tag with a value attribute.
Date: <date value="2026-03-23" />Date: <date value="2026-03-23" />Other authored values serialize with a text attribute, which the reader keeps as written. Child text would not survive, because Markdown can change it, such as at a blank line or an emoji shortcode.
Date: <date text="sometime next week" />Date: <date text="sometime next week" />The reader also accepts child text, such as <date>sometime next week</date>. It reads toDateString() text in a value attribute or child text, such as <date>Mon Mar 23 2026</date>, as value: '2026-03-23' when its weekday matches.
| API | Package | Use |
|---|---|---|
BaseDatePlugin | platejs/date | Headless inline void date plugin. |
DatePlugin | platejs/date/react | React date plugin. |
editor.plugin(BaseDatePlugin).update.insert(input?, options?) | BaseDatePlugin update | Inserts a date from a Date or string value, with node placement options. |
formatDateValue(date) | platejs/date | Formats a Date object's local day as YYYY-MM-DD. |
parseCanonicalDateValue(value) | platejs/date | Returns a Date for a valid YYYY-MM-DD day, or undefined. |
DateElement | platejs/date | Element shape with one required value. |
'use client';
import { cn } from 'cn';
import { formatDateValue, parseCanonicalDateValue } from 'platejs/date';
import { DatePlugin } from 'platejs/date/react';
import {
type EditorElementProps,
EditorElement,
useEditorReadOnly,
} from 'platejs/react';
import * as React from 'react';
import { Calendar } from '@/components/ui/calendar';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
import { inlineSuggestionVariants } from '@/lib/inline-suggestion';
const DAY_MS = 86_400_000;
const RELATIVE_DAYS: Partial<Record<number, string>> = {
[-1]: 'Yesterday',
0: 'Today',
1: 'Tomorrow',
};
const dateFormat = new Intl.DateTimeFormat('en-US', {
dateStyle: 'long',
timeZone: 'UTC',
});
const getDateLabel = (value: string) => {
if (!parseCanonicalDateValue(value)) return value;
const day = Date.parse(value);
const today = Date.parse(formatDateValue(new Date()));
return RELATIVE_DAYS[(day - today) / DAY_MS] ?? dateFormat.format(day);
};
export function DateElement(props: EditorElementProps<typeof DatePlugin>) {
const { editor, element } = props;
const readOnly = useEditorReadOnly();
const openRef = React.useRef(false);
const openAtPointerDownRef = React.useRef(false);
const [open, setOpen] = React.useState(false);
const selectedDate = parseCanonicalDateValue(element.value);
const setOpenState = React.useCallback((nextOpen: boolean) => {
openRef.current = nextOpen;
setOpen(nextOpen);
}, []);
const trigger = (
<button
className={cn(
'w-fit cursor-pointer rounded-sm bg-muted px-1 text-muted-foreground',
inlineSuggestionVariants({ on: 'parent' })
)}
contentEditable={false}
draggable
onClick={(event) => {
const beganOpen =
event.detail === 0 ? openRef.current : openAtPointerDownRef.current;
if (readOnly || beganOpen) return;
event.preventDefault();
setOpenState(true);
}}
onPointerDown={() => {
openAtPointerDownRef.current = openRef.current;
}}
type="button"
>
{getDateLabel(element.value)}
</button>
);
return (
<EditorElement
{...props}
className="inline-block"
attributes={{
...props.attributes,
contentEditable: false,
}}
>
{readOnly ? (
trigger
) : (
<Popover open={open} onOpenChange={setOpenState}>
<PopoverTrigger asChild>{trigger}</PopoverTrigger>
<PopoverContent className="w-auto p-0">
<Calendar
defaultMonth={selectedDate}
selected={selectedDate}
onSelect={(date) => {
if (!date) return;
editor.update.nodes.set(
{ value: formatDateValue(date) },
{ at: element }
);
}}
mode="single"
autoFocus
/>
</PopoverContent>
</Popover>
)}
{props.children}
</EditorElement>
);
}
export const DateKit = [DatePlugin.configure({ component: DateElement })];import { BaseDatePlugin, parseCanonicalDateValue } from 'platejs/date';
import { type EditorElementProps, EditorElement } from 'platejs/static';
import * as React from 'react';
const dateFormat = new Intl.DateTimeFormat('en-US', {
dateStyle: 'long',
timeZone: 'UTC',
});
const getDateLabel = (value: string) =>
parseCanonicalDateValue(value) ? dateFormat.format(Date.parse(value)) : value;
export function DateElementStatic(
props: EditorElementProps<typeof BaseDatePlugin>
) {
const { element } = props;
return (
<EditorElement as="span" className="inline-block" {...props}>
<span className="w-fit rounded-sm bg-muted px-1 text-muted-foreground">
{getDateLabel(element.value)}
</span>
{props.children}
</EditorElement>
);
}
export const BaseDateKit = [
BaseDatePlugin.configure({ component: DateElementStatic }),
];