"use client"
import { useEffect, useRef, type ComponentProps } from "react"
import {
DayPicker,
defaultDateLib,
getDefaultClassNames,
type ChevronProps,
type ClassNames,
type DateRange,
type DayButtonProps,
type DayPickerProps,
type DayProps,
type DropdownProps,
} from "react-day-picker"
import * as stylex from "@stylexjs/stylex"
import type { StyleXStyles } from "@stylexjs/stylex"
import { clsx } from "clsx"
import { CaretDownIcon } from "@phosphor-icons/react/dist/csr/CaretDown"
import { CaretLeftIcon } from "@phosphor-icons/react/dist/csr/CaretLeft"
import { CaretRightIcon } from "@phosphor-icons/react/dist/csr/CaretRight"
import { CaretUpIcon } from "@phosphor-icons/react/dist/csr/CaretUp"
import { Button } from "@/components/ui/button"
import { selectStyles } from "@/components/ui/select"
import { breakpoints } from "@/theme/breakpoints.stylex"
import { states } from "@/theme/states"
import { color, duration, ease, layer, leading, radius, space, text } from "@/theme/tokens.stylex"
const cellSize = "var(--arene-calendar-cell-size)"
const styles = stylex.create({
root: {
position: "relative",
width: "fit-content",
padding: space.s3,
fontSize: text.sm,
lineHeight: leading.sm,
"--arene-calendar-cell-size": space.s8,
},
day: {
width: cellSize,
height: cellSize,
padding: 0,
textAlign: "center",
},
rangeStart: {
backgroundColor: color.muted,
borderStartStartRadius: radius.lg,
borderEndStartRadius: radius.lg,
},
rangeMiddle: {
backgroundColor: color.muted,
borderStartStartRadius: { default: 0, ":first-of-type": radius.lg },
borderEndStartRadius: { default: 0, ":first-of-type": radius.lg },
borderStartEndRadius: { default: 0, ":last-of-type": radius.lg },
borderEndEndRadius: { default: 0, ":last-of-type": radius.lg },
},
rangeEnd: {
backgroundColor: color.muted,
borderStartEndRadius: radius.lg,
borderEndEndRadius: radius.lg,
},
dayButton: {
position: "relative",
zIndex: { default: null, ":focus-visible": layer.raised },
display: "flex",
alignItems: "center",
justifyContent: "center",
width: cellSize,
height: cellSize,
padding: 0,
borderRadius: radius.lg,
borderWidth: 1,
borderStyle: "solid",
backgroundColor: { default: color.transparent, ":hover": color.itemHighlightBg },
color: color.foreground,
fontSize: text.sm,
lineHeight: leading.sm,
fontVariantNumeric: "tabular-nums",
userSelect: "none",
transitionProperty: "color, background-color, border-color, box-shadow",
transitionDuration: duration.color,
transitionTimingFunction: ease.standard,
},
outside: { color: color.mutedForeground },
today: { backgroundColor: color.muted, fontWeight: 500 },
rangeMiddleButton: {
"--arene-border-color": color.transparent,
borderRadius: 0,
backgroundColor: { default: color.transparent, ":hover": color.itemHighlightBg },
color: color.foreground,
},
navButton: { pointerEvents: "auto" },
dropdownRoot: { position: "relative", display: "inline-flex" },
dropdown: {
appearance: "none",
paddingInlineEnd: space.s6,
fontWeight: 500,
},
dropdownIcon: {
position: "absolute",
insetBlock: 0,
insetInlineEnd: space.s2,
width: space.s3_5,
height: "100%",
color: color.mutedForeground,
pointerEvents: "none",
},
})
const parts = stylex.create({
months: {
position: "relative",
display: "flex",
flexDirection: { default: "column", [breakpoints.md]: "row" },
gap: space.s4,
},
month: { display: "flex", flexDirection: "column", gap: space.s3 },
nav: {
position: "absolute",
insetBlockStart: 0,
insetInline: 0,
display: "flex",
alignItems: "center",
justifyContent: "space-between",
height: cellSize,
pointerEvents: "none",
},
month_caption: {
display: "flex",
alignItems: "center",
justifyContent: "center",
height: cellSize,
paddingInline: cellSize,
},
caption_label: { fontWeight: 500, whiteSpace: "nowrap", userSelect: "none" },
dropdowns: { display: "flex", alignItems: "center", gap: space.s1_5 },
month_grid: { borderCollapse: "collapse" },
weekday: {
width: cellSize,
paddingBlockEnd: space.s1,
fontSize: text.xs,
lineHeight: leading.xs,
fontWeight: 400,
color: color.mutedForeground,
userSelect: "none",
},
week_number_header: { width: cellSize },
week_number: {
width: cellSize,
fontSize: text.xs,
lineHeight: leading.xs,
fontWeight: 400,
color: color.mutedForeground,
fontVariantNumeric: "tabular-nums",
userSelect: "none",
},
footer: { paddingBlockStart: space.s3, color: color.mutedForeground },
})
type CalendarPart = keyof typeof parts
const calendarStyles = {
root: styles.root,
parts,
day: styles.day,
dayButton: [styles.dayButton, states.focusRing],
outside: styles.outside,
today: styles.today,
selected: states.choiceChecked,
disabled: states.disabled,
range: { start: styles.rangeStart, middle: styles.rangeMiddle, end: styles.rangeEnd },
}
const defaultClassNames = getDefaultClassNames()
function partClassNames(classNames: Partial<ClassNames> = {}): Partial<ClassNames> {
const merged: Partial<ClassNames> = { ...classNames }
for (const part of Object.keys(parts) as CalendarPart[]) {
merged[part] = clsx(
defaultClassNames[part],
stylex.props(parts[part]).className,
classNames[part],
)
}
return merged
}
function focusableDay(container: HTMLElement | null) {
return (
container?.querySelector<HTMLElement>("[data-slot='calendar'] td button[tabindex='0']") ?? null
)
}
function CalendarDay({ day: _day, modifiers, className, style, ...props }: DayProps) {
const sx = stylex.props(
styles.day,
modifiers.range_start && styles.rangeStart,
modifiers.range_middle && styles.rangeMiddle,
modifiers.range_end && styles.rangeEnd,
)
return (
<td
{...sx}
{...props}
className={clsx(sx.className, className)}
style={{ ...sx.style, ...style }}
/>
)
}
function CalendarDayButton({ day: _day, modifiers, className, style, ...props }: DayButtonProps) {
const ref = useRef<HTMLButtonElement>(null)
useEffect(() => {
if (modifiers.focused) ref.current?.focus()
}, [modifiers.focused])
const sx = stylex.props(
styles.dayButton,
states.focusRing,
modifiers.outside && styles.outside,
modifiers.today && styles.today,
modifiers.selected && states.choiceChecked,
modifiers.range_middle && styles.rangeMiddleButton,
modifiers.disabled && states.disabled,
)
return (
<button
ref={ref}
{...sx}
{...props}
className={clsx(sx.className, className)}
style={{ ...sx.style, ...style }}
/>
)
}
function CalendarNavButton(props: ComponentProps<"button">) {
return (
<Button
variant="ghost"
size="icon-sm"
xstyle={[styles.navButton, props["aria-disabled"] && states.disabled]}
{...props}
/>
)
}
const chevrons = {
up: CaretUpIcon,
down: CaretDownIcon,
left: CaretLeftIcon,
right: CaretRightIcon,
}
function CalendarChevron({ orientation = "left", className, style }: ChevronProps) {
const Icon = chevrons[orientation]
return <Icon aria-hidden="true" className={className} style={style} />
}
function CalendarDropdown({ options, className, style, ...props }: DropdownProps) {
const sx = stylex.props(
selectStyles.trigger,
selectStyles.sizes.sm,
styles.dropdown,
props.disabled && selectStyles.triggerDisabled,
)
return (
<span {...stylex.props(styles.dropdownRoot)}>
<select
{...sx}
{...props}
className={clsx(sx.className, className)}
style={{ ...sx.style, ...style }}
>
{options?.map(({ value, label, disabled }) => (
<option key={value} value={value} disabled={disabled}>
{label}
</option>
))}
</select>
<CaretDownIcon aria-hidden="true" {...stylex.props(styles.dropdownIcon)} />
</span>
)
}
type CalendarProps = DayPickerProps & {
animate?: never
xstyle?: StyleXStyles
}
function Calendar({
showOutsideDays = true,
classNames,
components,
formatters,
xstyle,
className,
style,
...props
}: CalendarProps) {
const sx = stylex.props(styles.root, xstyle)
return (
<DayPicker
data-slot="calendar"
showOutsideDays={showOutsideDays}
{...props}
className={clsx(sx.className, className)}
style={{ ...sx.style, ...style }}
classNames={partClassNames(classNames)}
formatters={{
formatMonthDropdown: (month, dateLib = defaultDateLib) => dateLib.format(month, "LLL"),
...formatters,
}}
components={{
Day: CalendarDay,
DayButton: CalendarDayButton,
PreviousMonthButton: CalendarNavButton,
NextMonthButton: CalendarNavButton,
Chevron: CalendarChevron,
Dropdown: CalendarDropdown,
...components,
}}
/>
)
}
export {
Calendar,
CalendarDayButton,
calendarStyles,
focusableDay,
type CalendarProps,
type DateRange,
}