Publish Schedule
Publishing schedule panel: a date and time for an article, validated, with a preview of when it goes live.
"use client"
import { useId, useState } from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { CalendarBlankIcon } from "@phosphor-icons/react"
import { addDays, format } from "date-fns"
import { Controller, useForm, useWatch } from "react-hook-form"
import { z } from "zod"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Calendar } from "@/components/ui/calendar"
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
Field,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import {
Item,
ItemActions,
ItemContent,
ItemDescription,
ItemTitle,
} from "@/components/ui/item"
import {
Popover,
PopoverContent,
PopoverTitle,
PopoverTrigger,
} from "@/components/ui/popover"
// The sample "today". It is a fixed date so the server and the browser
// render the same days; every other date here counts from it, and the day
// starts at midnight, so no time on it counts as past. In your app, read
// the real date and check the time against it.
const TODAY = new Date(2026, 10, 17)
// The copy: change text here, not in the markup.
const COPY = {
title: "Schedule publication",
description: "Choose when this article goes live.",
article: "Designing a quieter interface",
articleNumber: "Article 024",
date: "Publication date",
time: "Time (UTC)",
chooseDate: "Choose a date",
chooseTime: "Choose a time",
preview: "Publication preview",
channel: "Journal",
reset: "Reset",
schedule: "Schedule article",
update: "Update schedule",
scheduled: "Scheduled",
statusDraft: "Draft",
draft: "Your article stays in draft until you schedule it.",
scheduledFor: (date: string, time: string) =>
`Scheduled for ${date} at ${time} UTC.`,
unsaved: " Save to apply your changes.",
errors: {
date: "Choose a publication date.",
past: "Choose today or a later date.",
time: "Enter a time in HH:MM format.",
},
} as const
const schema = z.object({
date: z.date(COPY.errors.date).min(TODAY, COPY.errors.past),
time: z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/, COPY.errors.time),
})
type PublishingValues = z.infer<typeof schema>
const initialValues = (): PublishingValues => ({
date: addDays(TODAY, 8),
time: "09:00",
})
/**
* A publishing card that schedules an article for a date and a time, with
* validation and a preview of when it goes live. The date is picked from a
* calendar in a popover; the status badge and the button follow what is
* saved.
*
* The layout is a grid that fits as many columns as there is room for, so
* the card also works in a sidebar, a dialog or half a page.
*
* The words live in `COPY`; the starting date and time are `initialValues`.
*/
export default function PublishSchedule01() {
const id = useId()
// The date picker, written in place: a button that opens the calendar.
const [dateOpen, setDateOpen] = useState(false)
const [scheduled, setScheduled] = useState<PublishingValues | null>(null)
const form = useForm<PublishingValues>({
resolver: zodResolver(schema),
defaultValues: initialValues(),
})
const [date, time] = useWatch({
control: form.control,
name: ["date", "time"],
})
const dirty = form.formState.isDirty
function schedule(values: PublishingValues) {
setScheduled({ date: new Date(values.date), time: values.time })
form.reset(values)
}
function reset() {
form.reset(initialValues())
setScheduled(null)
}
return (
<Card
role="region"
aria-labelledby={`${id}-title`}
className="mx-auto w-full max-w-xl"
>
<CardHeader>
<CardTitle>
<h2 id={`${id}-title`}>{COPY.title}</h2>
</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
</CardHeader>
<CardContent>
<form
id={`${id}-form`}
onSubmit={form.handleSubmit(schedule)}
noValidate
>
<FieldGroup>
<Item variant="outline">
<ItemContent>
<ItemTitle>{COPY.article}</ItemTitle>
<ItemDescription>{COPY.articleNumber}</ItemDescription>
</ItemContent>
<ItemActions>
<Badge variant={scheduled ? "default" : "secondary"}>
{scheduled ? COPY.scheduled : COPY.statusDraft}
</Badge>
</ItemActions>
</Item>
<div className="grid grid-cols-[repeat(auto-fit,minmax(11rem,1fr))] gap-4">
<Controller
name="date"
control={form.control}
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={`${id}-date`}>{COPY.date}</FieldLabel>
<Popover open={dateOpen} onOpenChange={setDateOpen}>
<PopoverTrigger
render={
<Button
id={`${id}-date`}
variant="outline"
aria-invalid={fieldState.invalid}
aria-describedby={
fieldState.invalid
? `${id}-date-error`
: undefined
}
className="justify-start px-2.5 font-normal"
/>
}
>
<CalendarBlankIcon data-icon="inline-start" />
{field.value ? (
format(field.value, "MMM d, yyyy")
) : (
<span className="text-muted-foreground">
{COPY.chooseDate}
</span>
)}
</PopoverTrigger>
<PopoverContent
align="start"
className="max-h-(--available-height) w-auto overflow-y-auto p-0"
>
<PopoverTitle className="sr-only">
{COPY.date}
</PopoverTitle>
<Calendar
mode="single"
required
defaultMonth={field.value}
selected={field.value}
disabled={{ before: TODAY }}
onSelect={(next) => {
field.onChange(next)
if (next) setDateOpen(false)
}}
/>
</PopoverContent>
</Popover>
<FieldError
id={`${id}-date-error`}
errors={[fieldState.error]}
/>
</Field>
)}
/>
<Controller
name="time"
control={form.control}
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={`${id}-time`}>{COPY.time}</FieldLabel>
<Input
{...field}
id={`${id}-time`}
type="time"
required
aria-invalid={fieldState.invalid}
aria-describedby={
fieldState.invalid ? `${id}-time-error` : undefined
}
className="tabular-nums"
/>
<FieldError
id={`${id}-time-error`}
errors={[fieldState.error]}
/>
</Field>
)}
/>
</div>
<Item variant="muted">
<ItemContent>
<ItemDescription>{COPY.preview}</ItemDescription>
<ItemTitle className="tabular-nums">
{date ? format(date, "EEE, MMM d, yyyy") : COPY.chooseDate}
{" · "}
{time ? `${time} UTC` : COPY.chooseTime}
</ItemTitle>
<ItemDescription>{COPY.channel}</ItemDescription>
</ItemContent>
</Item>
<FieldDescription
role="status"
aria-live="polite"
aria-atomic="true"
>
{scheduled
? COPY.scheduledFor(
format(scheduled.date, "MMM d, yyyy"),
scheduled.time
)
: COPY.draft}
{scheduled && dirty ? COPY.unsaved : null}
</FieldDescription>
</FieldGroup>
</form>
</CardContent>
<CardFooter className="justify-between border-t">
<Button type="button" variant="ghost" onClick={reset}>
{COPY.reset}
</Button>
<Button
type="submit"
form={`${id}-form`}
disabled={Boolean(scheduled) && !dirty}
>
{scheduled ? (dirty ? COPY.update : COPY.scheduled) : COPY.schedule}
</Button>
</CardFooter>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/publish-schedule-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/controls/publish-schedule-01/index.tsx: change them there, not in the markup. TODAY, also at the top of the file, holds its lists and settings.
Built from
npm: @hookform/resolvers, @phosphor-icons/react, date-fns, react-hook-form, zod. The CLI installs them for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on