Access Expiry
Access expiration card for a collaborator: preset durations or a custom last day, validated, with the saved expiry announced.
"use client"
import { useId, useState } from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { CalendarBlankIcon } from "@phosphor-icons/react"
import { addDays, format, isBefore, startOfDay } 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,
FieldContent,
FieldDescription,
FieldError,
FieldGroup,
FieldLabel,
FieldLegend,
FieldSet,
FieldTitle,
} from "@/components/ui/field"
import {
Item,
ItemActions,
ItemContent,
ItemDescription,
ItemTitle,
} from "@/components/ui/item"
import {
Popover,
PopoverContent,
PopoverTitle,
PopoverTrigger,
} from "@/components/ui/popover"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
// 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. In your app,
// read the real date where you load the data.
const TODAY = new Date(2026, 10, 17)
// The copy: change text here, not in the markup.
const COPY = {
title: "Access expiration",
description: "Set when this collaborator’s access ends.",
person: "Mara Lindqvist",
role: "External collaborator",
permission: "Read-only access",
policy: "Expiration",
date: "Last day of access",
noDate: "No expiration",
submit: "Save expiration",
cancel: "Cancel changes",
note: "Dated access ends at 23:59 UTC. Existing project permissions stay the same.",
error: "Choose an expiration date from November 17 onward.",
ends: (day: string) => `Access ends ${day} at 23:59 UTC.`,
open: "Access stays open until you remove it.",
choose: "Choose a policy, then save to apply it.",
} as const
const POLICIES = [
{ value: "never", label: "No expiration", detail: "Until you remove it" },
{ value: "7", label: "In 7 days", detail: "A week to review" },
{ value: "30", label: "In 30 days", detail: "A little more time" },
{ value: "custom", label: "Choose a date", detail: "Set the last day" },
] as const
const schema = z
.object({
policy: z.enum(["never", "7", "30", "custom"]),
expiresAt: z.date().optional(),
})
.superRefine(({ policy, expiresAt }, context) => {
if (
policy === "custom" &&
(!expiresAt || isBefore(startOfDay(expiresAt), TODAY))
) {
context.addIssue({
code: "custom",
path: ["expiresAt"],
message: COPY.error,
})
}
})
type ExpiryValues = z.infer<typeof schema>
const DEFAULTS: ExpiryValues = {
policy: "7",
expiresAt: addDays(TODAY, 7),
}
/**
* A card that sets when a collaborator's access ends: four choices, three
* of them presets and one a date picked from a calendar. The date button is
* on only for "Choose a date"; the form validates, and the saved expiry is
* announced.
*
* 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 choices in `POLICIES` and the starting
* values in `DEFAULTS`.
*/
export default function AccessExpiry01() {
const id = useId()
// The date picker, written in place: a button that opens the calendar.
const [dateOpen, setDateOpen] = useState(false)
const [saved, setSaved] = useState<ExpiryValues | null>(null)
const form = useForm<ExpiryValues>({
resolver: zodResolver(schema),
defaultValues: DEFAULTS,
})
const policy = useWatch({ control: form.control, name: "policy" })
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((values) => {
const expiresAt =
values.policy === "never"
? undefined
: values.policy === "custom"
? values.expiresAt
: addDays(TODAY, Number(values.policy))
const next = { ...values, expiresAt }
setSaved(next)
form.reset(next)
})}
>
<FieldGroup>
<Item variant="outline">
<ItemContent>
<ItemTitle>{COPY.person}</ItemTitle>
<ItemDescription>{COPY.role}</ItemDescription>
</ItemContent>
<ItemActions>
<Badge variant="secondary">{COPY.permission}</Badge>
</ItemActions>
</Item>
<Controller
control={form.control}
name="policy"
render={({ field, fieldState }) => (
<FieldSet data-invalid={fieldState.invalid}>
<FieldLegend variant="label">{COPY.policy}</FieldLegend>
<RadioGroup
name={field.name}
value={field.value}
onValueChange={(value) => {
const next = POLICIES.find(
(item) => item.value === value
)?.value
if (!next) return
field.onChange(next)
// A preset fills the date in; "never" empties it.
form.setValue(
"expiresAt",
next === "never"
? undefined
: addDays(
TODAY,
next === "custom" ? 7 : Number(next)
),
{ shouldDirty: true, shouldValidate: true }
)
}}
onBlur={field.onBlur}
aria-invalid={fieldState.invalid}
className="grid-cols-[repeat(auto-fit,minmax(13rem,1fr))]"
>
{POLICIES.map((item) => (
<FieldLabel
key={item.value}
htmlFor={`${id}-policy-${item.value}`}
>
<Field orientation="horizontal">
<FieldContent>
<FieldTitle>{item.label}</FieldTitle>
<FieldDescription>{item.detail}</FieldDescription>
</FieldContent>
<RadioGroupItem
id={`${id}-policy-${item.value}`}
value={item.value}
aria-invalid={fieldState.invalid}
/>
</Field>
</FieldLabel>
))}
</RadioGroup>
<FieldError errors={[fieldState.error]} />
</FieldSet>
)}
/>
<Controller
control={form.control}
name="expiresAt"
render={({ field, fieldState }) => (
<Field
data-invalid={fieldState.invalid}
data-disabled={policy !== "custom"}
>
<FieldLabel htmlFor={`${id}-date`}>{COPY.date}</FieldLabel>
<Popover open={dateOpen} onOpenChange={setDateOpen}>
<PopoverTrigger
render={
<Button
id={`${id}-date`}
variant="outline"
disabled={policy !== "custom"}
aria-invalid={fieldState.invalid}
aria-describedby={
fieldState.invalid
? `${id}-date-error`
: `${id}-date-note`
}
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.noDate}
</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>
<FieldDescription id={`${id}-date-note`}>
{COPY.note}
</FieldDescription>
<FieldError
id={`${id}-date-error`}
errors={[fieldState.error]}
/>
</Field>
)}
/>
</FieldGroup>
</form>
</CardContent>
<CardFooter className="flex-wrap justify-between gap-4 border-t">
<span role="status">
{saved
? saved.expiresAt
? COPY.ends(format(saved.expiresAt, "EEE, MMM d"))
: COPY.open
: COPY.choose}
</span>
<div className="flex items-center gap-2">
<Button
type="button"
variant="ghost"
onClick={() => form.reset(saved ?? DEFAULTS)}
>
{COPY.cancel}
</Button>
<Button
type="submit"
form={`${id}-form`}
disabled={Boolean(saved) && !form.formState.isDirty}
>
{COPY.submit}
</Button>
</div>
</CardFooter>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/access-expiry-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/controls/access-expiry-01/index.tsx: change them there, not in the markup. TODAY,POLICIESandDEFAULTS, also at the top of the file, hold 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