|
| 1 | +import React from 'react' |
| 2 | +import { Chrono } from '@lib' |
| 3 | +import type { DateRange } from '@lib/chrono' |
| 4 | +import { ChevronDown } from 'lucide-react' |
| 5 | +import { |
| 6 | + differenceInYears, |
| 7 | + differenceInMonths, |
| 8 | + differenceInWeeks, |
| 9 | + differenceInDays, |
| 10 | + differenceInHours, |
| 11 | + differenceInMinutes, |
| 12 | + differenceInSeconds |
| 13 | +} from 'date-fns' |
| 14 | + |
| 15 | +const colors = { |
| 16 | + bg: '#0A0A0A', |
| 17 | + surface: '#111111', |
| 18 | + border: '#222222', |
| 19 | + text: '#FFFFFF', |
| 20 | + textMuted: '#888888', |
| 21 | + cyan: '#00F0FF' |
| 22 | +} |
| 23 | + |
| 24 | +export default function ChronoExample() { |
| 25 | + const [dateRange, setDateRange] = React.useState<DateRange>({ |
| 26 | + startDate: new Date(Date.now() - 1000 * 60 * 15), // 15 minutes ago |
| 27 | + endDate: new Date() |
| 28 | + }) |
| 29 | + const [isOpen, setIsOpen] = React.useState(false) |
| 30 | + |
| 31 | + const onDateRangeChange = (range: DateRange) => { |
| 32 | + setDateRange(range) |
| 33 | + } |
| 34 | + |
| 35 | + const getDurationLabel = (start: Date, end: Date) => { |
| 36 | + const diffSeconds = differenceInSeconds(end, start) |
| 37 | + const diffMinutes = differenceInMinutes(end, start) |
| 38 | + const diffHours = differenceInHours(end, start) |
| 39 | + const diffDays = differenceInDays(end, start) |
| 40 | + const diffWeeks = differenceInWeeks(end, start) |
| 41 | + const diffMonths = differenceInMonths(end, start) |
| 42 | + const diffYears = differenceInYears(end, start) |
| 43 | + |
| 44 | + if (diffYears > 0) return `${diffYears}y` |
| 45 | + if (diffMonths > 0) return `${diffMonths}mo` |
| 46 | + if (diffWeeks > 0) return `${diffWeeks}w` |
| 47 | + if (diffDays > 0) return `${diffDays}d` |
| 48 | + if (diffHours > 0) return `${diffHours}h` |
| 49 | + if (diffMinutes > 0) return `${diffMinutes}m` |
| 50 | + |
| 51 | + return `${diffSeconds}s` |
| 52 | + } |
| 53 | + |
| 54 | + const activeDurationLabel = React.useMemo(() => { |
| 55 | + if (!dateRange.startDate || !dateRange.endDate) return '-' |
| 56 | + return getDurationLabel(dateRange.startDate, dateRange.endDate) |
| 57 | + }, [dateRange]) |
| 58 | + |
| 59 | + return ( |
| 60 | + <Chrono.Root |
| 61 | + onDateRangeChange={onDateRangeChange} |
| 62 | + defaultDateRange={dateRange} |
| 63 | + onOpenChange={setIsOpen} |
| 64 | + > |
| 65 | + <Chrono.Trigger asChild> |
| 66 | + <div className="flex items-center space-x-2 w-full"> |
| 67 | + <div |
| 68 | + className="flex-1 flex items-center rounded-lg px-4 py-3 transition-all duration-200 cursor-pointer group" |
| 69 | + style={{ |
| 70 | + border: `1px solid ${colors.border}`, |
| 71 | + backgroundColor: colors.surface |
| 72 | + }} |
| 73 | + > |
| 74 | + <div className="flex items-center space-x-3 w-full"> |
| 75 | + <div |
| 76 | + className="h-6 min-w-[48px] rounded text-xs text-center leading-6 font-mono font-medium" |
| 77 | + style={{ |
| 78 | + backgroundColor: `${colors.cyan}20`, |
| 79 | + color: colors.cyan |
| 80 | + }} |
| 81 | + > |
| 82 | + {activeDurationLabel} |
| 83 | + </div> |
| 84 | + <Chrono.Input |
| 85 | + className="bg-transparent text-sm w-full outline-none border-none cursor-pointer overflow-hidden truncate" |
| 86 | + style={{ color: colors.textMuted }} |
| 87 | + /> |
| 88 | + <div |
| 89 | + className={`transition-transform duration-200 ${isOpen ? 'rotate-180' : ''}`} |
| 90 | + style={{ color: colors.textMuted }} |
| 91 | + > |
| 92 | + <ChevronDown className="h-4 w-4" /> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </Chrono.Trigger> |
| 98 | + |
| 99 | + <Chrono.Portal |
| 100 | + className="relative rounded-lg mt-2 p-1.5 flex flex-col gap-0.5 text-sm shadow-2xl z-50 w-full transition-all duration-100 animate-in fade-in-0 zoom-in-95" |
| 101 | + style={{ |
| 102 | + backgroundColor: colors.surface, |
| 103 | + border: `1px solid ${colors.border}` |
| 104 | + }} |
| 105 | + > |
| 106 | + <div |
| 107 | + className="pb-2 px-2 mb-1" |
| 108 | + style={{ borderBottom: `1px solid ${colors.border}` }} |
| 109 | + > |
| 110 | + <div |
| 111 | + className="text-[10px] font-mono font-medium uppercase tracking-widest" |
| 112 | + style={{ color: colors.textMuted }} |
| 113 | + > |
| 114 | + Quick select |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + <Chrono.Shortcut duration={{ minutes: 15 }} asChild> |
| 118 | + <div |
| 119 | + className="p-2.5 rounded-md cursor-pointer transition-colors duration-100 flex items-center justify-between" |
| 120 | + style={{ color: colors.text }} |
| 121 | + onMouseEnter={(e) => |
| 122 | + (e.currentTarget.style.backgroundColor = `${colors.cyan}10`) |
| 123 | + } |
| 124 | + onMouseLeave={(e) => |
| 125 | + (e.currentTarget.style.backgroundColor = 'transparent') |
| 126 | + } |
| 127 | + > |
| 128 | + <span className="text-sm">15 minutes</span> |
| 129 | + <span className="text-xs font-mono" style={{ color: colors.cyan }}> |
| 130 | + 15m |
| 131 | + </span> |
| 132 | + </div> |
| 133 | + </Chrono.Shortcut> |
| 134 | + <Chrono.Shortcut duration={{ hours: 1 }} asChild> |
| 135 | + <div |
| 136 | + className="p-2.5 rounded-md cursor-pointer transition-colors duration-100 flex items-center justify-between" |
| 137 | + style={{ color: colors.text }} |
| 138 | + onMouseEnter={(e) => |
| 139 | + (e.currentTarget.style.backgroundColor = `${colors.cyan}10`) |
| 140 | + } |
| 141 | + onMouseLeave={(e) => |
| 142 | + (e.currentTarget.style.backgroundColor = 'transparent') |
| 143 | + } |
| 144 | + > |
| 145 | + <span className="text-sm">1 hour</span> |
| 146 | + <span className="text-xs font-mono" style={{ color: colors.cyan }}> |
| 147 | + 1h |
| 148 | + </span> |
| 149 | + </div> |
| 150 | + </Chrono.Shortcut> |
| 151 | + <Chrono.Shortcut duration={{ days: 1 }} asChild> |
| 152 | + <div |
| 153 | + className="p-2.5 rounded-md cursor-pointer transition-colors duration-100 flex items-center justify-between" |
| 154 | + style={{ color: colors.text }} |
| 155 | + onMouseEnter={(e) => |
| 156 | + (e.currentTarget.style.backgroundColor = `${colors.cyan}10`) |
| 157 | + } |
| 158 | + onMouseLeave={(e) => |
| 159 | + (e.currentTarget.style.backgroundColor = 'transparent') |
| 160 | + } |
| 161 | + > |
| 162 | + <span className="text-sm">1 day</span> |
| 163 | + <span className="text-xs font-mono" style={{ color: colors.cyan }}> |
| 164 | + 1d |
| 165 | + </span> |
| 166 | + </div> |
| 167 | + </Chrono.Shortcut> |
| 168 | + <Chrono.Shortcut duration={{ months: 1 }} asChild> |
| 169 | + <div |
| 170 | + className="p-2.5 rounded-md cursor-pointer transition-colors duration-100 flex items-center justify-between" |
| 171 | + style={{ color: colors.text }} |
| 172 | + onMouseEnter={(e) => |
| 173 | + (e.currentTarget.style.backgroundColor = `${colors.cyan}10`) |
| 174 | + } |
| 175 | + onMouseLeave={(e) => |
| 176 | + (e.currentTarget.style.backgroundColor = 'transparent') |
| 177 | + } |
| 178 | + > |
| 179 | + <span className="text-sm">1 month</span> |
| 180 | + <span className="text-xs font-mono" style={{ color: colors.cyan }}> |
| 181 | + 1mo |
| 182 | + </span> |
| 183 | + </div> |
| 184 | + </Chrono.Shortcut> |
| 185 | + </Chrono.Portal> |
| 186 | + </Chrono.Root> |
| 187 | + ) |
| 188 | +} |
0 commit comments