relative-time
kibo-ui-registry/relative-timeFreeComponent
A component that displays time in various timezones.
Live preview
live · rendered by the registry
Rendered by Kibo UI Registry on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.kibo-ui.com/r/relative-time.jsonSource
1"use client";23import { useControllableState } from "@radix-ui/react-use-controllable-state";4import {5 createContext,6 type HTMLAttributes,7 useContext,8 useEffect,9} from "react";10import { cn } from "@/lib/utils";1112const formatDate = (13 date: Date,14 timeZone: string,15 options?: Intl.DateTimeFormatOptions16) =>17 new Intl.DateTimeFormat(18 "en-US",19 options ?? {20 dateStyle: "long",21 timeZone,22 }23 ).format(date);2425const formatTime = (26 date: Date,27 timeZone: string,28 options?: Intl.DateTimeFormatOptions29) =>30 new Intl.DateTimeFormat(31 "en-US",32 options ?? {33 hour: "2-digit",34 minute: "2-digit",35 second: "2-digit",36 timeZone,37 }38 ).format(date);3940type RelativeTimeContextType = {41 time: Date;42 dateFormatOptions?: Intl.DateTimeFormatOptions;43 timeFormatOptions?: Intl.DateTimeFormatOptions;44};4546const RelativeTimeContext = createContext<RelativeTimeContextType>({47 time: new Date(),48 dateFormatOptions: {49 dateStyle: "long",50 },51 timeFormatOptions: {52 hour: "2-digit",53 minute: "2-digit",54 },55});5657export type RelativeTimeProps = HTMLAttributes<HTMLDivElement> & {58 time?: Date;59 defaultTime?: Date;60 onTimeChange?: (time: Date) => void;61 dateFormatOptions?: Intl.DateTimeFormatOptions;62 timeFormatOptions?: Intl.DateTimeFormatOptions;63};6465export const RelativeTime = ({66 time: controlledTime,67 defaultTime = new Date(),68 onTimeChange,69 dateFormatOptions,70 timeFormatOptions,71 className,72 ...props73}: RelativeTimeProps) => {74 const [time, setTime] = useControllableState<Date>({75 defaultProp: defaultTime,76 prop: controlledTime,77 onChange: onTimeChange,78 });7980 useEffect(() => {81 if (controlledTime) {82 return;83 }8485 const interval = setInterval(() => {86 setTime(new Date());87 }, 1000);8889 return () => clearInterval(interval);90 }, [setTime, controlledTime]);9192 return (93 <RelativeTimeContext.Provider94 value={{95 time: time ?? defaultTime,96 dateFormatOptions,97 timeFormatOptions,98 }}99 >100 <div className={cn("grid gap-2", className)} {...props} />101 </RelativeTimeContext.Provider>102 );103};104105export type RelativeTimeZoneProps = HTMLAttributes<HTMLDivElement> & {106 zone: string;107 dateFormatOptions?: Intl.DateTimeFormatOptions;108 timeFormatOptions?: Intl.DateTimeFormatOptions;109};110111export type RelativeTimeZoneContextType = {112 zone: string;113};114115const RelativeTimeZoneContext = createContext<RelativeTimeZoneContextType>({116// … truncated
What it pulls in
npm packages
Files it writes
More from Kibo UI Registry
All 41 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| announcementannouncement | Kibo UI Registry | Components | Free | no deps | |
| avatar-stackavatar-stack | Kibo UI Registry | Components | Free | no deps | |
| bannerbanner | Kibo UI Registry | Components | Free | 2 deps | |
| calendarcalendar | Kibo UI Registry | Components | Free | 3 deps | |
| choiceboxchoicebox | Kibo UI Registry | Components | Free | 2 deps | |
| code-blockcode-block | Kibo UI Registry | Components | Free | 5 deps · 2 files | |
| color-pickercolor-picker | Kibo UI Registry | Components | Free | 3 deps | |
| comboboxcombobox | Kibo UI Registry | Components | Free | 2 deps |
