{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "calendar-date-picker-24",
  "title": "Pricing calendar",
  "description": "Two months with a fictional fare under each day, the cheap ones in green.",
  "dependencies": [
    "date-fns",
    "motion",
    "react-day-picker"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/animated-size.json",
    "https://ui.ndekocode.dev/r/calendar.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/calendar-date-picker/calendar-date-picker-24.tsx",
      "content": "'use client';\n\nimport { addDays, format } from 'date-fns';\nimport { createContext, useContext, useState } from 'react';\nimport { DayButton, type DayButtonProps } from 'react-day-picker';\n\nimport { cn } from '@/registry/ndeko/lib/utils';\n\nimport { AnimatedCalendar } from './_parts/animated-calendar';\n\nconst GOOD_PRICE_THRESHOLD = 100;\nconst PRICED_DAYS = 180;\nconst LOWEST_PRICE = 80;\nconst PRICE_SPREAD = 121;\nconst DATE_KEY = 'yyyy-MM-dd';\n\nconst PRICING_CLASS_NAMES = {\n  months: 'flex-row flex-wrap justify-center gap-x-8 gap-y-4',\n  month: [\n    'relative before:absolute before:bg-border first-of-type:before:hidden',\n    'max-md:before:inset-x-2 max-md:before:-top-4 max-md:before:h-(--hairline)',\n    'md:before:inset-y-2 md:before:-left-4 md:before:w-(--hairline)',\n  ].join(' '),\n  weekday: 'w-10 sm:w-12',\n  day_button: 'size-10 sm:size-12',\n  today: '*:after:hidden',\n};\n\ntype Prices = Record<string, number>;\n\nconst PricesContext = createContext<Prices>({});\n\nfunction priceOfDay(dayIndex: number) {\n  const wave = Math.sin(dayIndex * 12.9898) * 43758.5453;\n  const fraction = wave - Math.floor(wave);\n  const offset = Math.floor(fraction * PRICE_SPREAD);\n  return LOWEST_PRICE + offset;\n}\n\nfunction buildPrices(): Prices {\n  const today = new Date();\n  const prices: Prices = {};\n  for (let dayIndex = 0; dayIndex < PRICED_DAYS; dayIndex++) {\n    const day = addDays(today, dayIndex);\n    const key = format(day, DATE_KEY);\n    prices[key] = priceOfDay(dayIndex);\n  }\n  return prices;\n}\n\nfunction PricedDayButton({ children, ...props }: DayButtonProps) {\n  const prices = useContext(PricesContext);\n  const price = prices[format(props.day.date, DATE_KEY)];\n  const hasPrice = price !== undefined;\n  const isGoodPrice = price < GOOD_PRICE_THRESHOLD;\n\n  return (\n    <DayButton {...props}>\n      <span className='flex flex-col'>\n        {children}\n        {hasPrice && (\n          <span\n            className={cn(\n              'text-muted-foreground group-data-selected:text-primary-foreground/70 text-[10px] font-medium tabular-nums',\n              isGoodPrice &&\n                'text-success-foreground group-data-selected:text-primary-foreground'\n            )}\n          >\n            ${price}\n          </span>\n        )}\n      </span>\n    </DayButton>\n  );\n}\n\nexport default function CalendarDatePicker24() {\n  const [date, setDate] = useState<Date | undefined>(() => new Date());\n  const [prices] = useState(buildPrices);\n\n  function isUnpriced(day: Date) {\n    return prices[format(day, DATE_KEY)] === undefined;\n  }\n\n  return (\n    <PricesContext.Provider value={prices}>\n      <AnimatedCalendar\n        mode='single'\n        selected={date}\n        onSelect={setDate}\n        numberOfMonths={2}\n        pagedNavigation\n        showOutsideDays={false}\n        className='bg-card hairline-ring ring-border rounded-xl p-2 shadow-[0_1px_2px_rgb(0_0_0/0.04)]'\n        classNames={PRICING_CLASS_NAMES}\n        components={{ DayButton: PricedDayButton }}\n        disabled={isUnpriced}\n      />\n    </PricesContext.Provider>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/calendar-date-picker/calendar-date-picker-24.tsx"
    },
    {
      "path": "registry/ndeko/components/calendar-date-picker/_parts/animated-calendar.tsx",
      "content": "'use client';\n\nimport { differenceInCalendarDays } from 'date-fns';\nimport {\n  motion,\n  type TargetAndTransition,\n  type Transition,\n} from 'motion/react';\nimport { useState } from 'react';\nimport {\n  Day,\n  useDayPicker,\n  type DayProps,\n  type MonthProps,\n} from 'react-day-picker';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { AnimatedSize } from '@/registry/ndeko/ui/animated-size';\nimport { Calendar } from '@/registry/ndeko/ui/calendar';\n\ntype CalendarProps = React.ComponentProps<typeof Calendar>;\n\ntype ShownMonth = { time: number; direction: number };\n\nconst MONTH_SHIFT_PX = 12;\nconst RANGE_STEP_MS = 12;\nconst RANGE_GROWTH_MAX_MS = 240;\n\nconst MONTH_TRANSITION: Transition = {\n  duration: 0.2,\n  ease: [0.23, 1, 0.32, 1],\n};\n\nconst MONTH_AT_REST: TargetAndTransition = { opacity: 1, x: 0 };\n\nconst SETTLING_DAY_BUTTON = [\n  'transition-[color,background-color,border-radius,box-shadow,scale] ease-(--ease-out-strong) active:scale-90',\n  'group-[.range-middle]:[transition-delay:var(--range-delay),var(--range-delay),0ms,0ms,0ms]',\n].join(' ');\n\nfunction rangeStartOf(selected: unknown) {\n  if (!selected) {\n    return undefined;\n  }\n  if (typeof selected !== 'object') {\n    return undefined;\n  }\n  if (!('from' in selected)) {\n    return undefined;\n  }\n  return selected.from as Date | undefined;\n}\n\nfunction rangeDelayOf(day: Date, rangeStart: Date | undefined) {\n  if (!rangeStart) {\n    return '0ms';\n  }\n  const step = differenceInCalendarDays(day, rangeStart);\n  const delay = Math.min(step * RANGE_STEP_MS, RANGE_GROWTH_MAX_MS);\n  const positiveDelay = Math.max(0, delay);\n  return `${positiveDelay}ms`;\n}\n\nfunction monthEnterOf(direction: number, shouldReduceMotion: boolean | null) {\n  if (direction === 0) {\n    return false;\n  }\n  if (shouldReduceMotion) {\n    return { opacity: 0 };\n  }\n  const offset = direction * MONTH_SHIFT_PX;\n  return { opacity: 0, x: offset };\n}\n\nfunction GrowingRangeDay({ style, ...props }: DayProps) {\n  const { selected } = useDayPicker();\n  const rangeStart = rangeStartOf(selected);\n  const delay = rangeDelayOf(props.day.date, rangeStart);\n  const dayStyle = { ...style, '--range-delay': delay } as React.CSSProperties;\n\n  return <Day {...props} style={dayStyle} />;\n}\n\nfunction SlidingMonth({\n  calendarMonth,\n  displayIndex,\n  children,\n  ...props\n}: MonthProps) {\n  const time = calendarMonth.date.getTime();\n  const [shown, setShown] = useState<ShownMonth>({ time, direction: 0 });\n  const shouldReduceMotion = usePrefersReducedMotion();\n\n  if (shown.time !== time) {\n    const direction = Math.sign(time - shown.time);\n    setShown({ time, direction });\n  }\n\n  return (\n    <div data-display-index={displayIndex} {...props}>\n      <AnimatedSize>\n        <motion.div\n          key={time}\n          initial={monthEnterOf(shown.direction, shouldReduceMotion)}\n          animate={MONTH_AT_REST}\n          transition={MONTH_TRANSITION}\n        >\n          {children}\n        </motion.div>\n      </AnimatedSize>\n    </div>\n  );\n}\n\nfunction withSettlingDays(classNames: CalendarProps['classNames']) {\n  const dayButton = cn(SETTLING_DAY_BUTTON, classNames?.day_button);\n  return { ...classNames, day_button: dayButton };\n}\n\n/**\n * The calendar primitive with the date picker micro-interactions: a picked day settles in,\n * a range grows from its start to its end, a new month slides in from the side it comes from\n * and the card resizes to the number of weeks.\n */\nfunction AnimatedCalendar({ classNames, components, ...props }: CalendarProps) {\n  return (\n    <Calendar\n      classNames={withSettlingDays(classNames)}\n      components={{ Day: GrowingRangeDay, Month: SlidingMonth, ...components }}\n      {...props}\n    />\n  );\n}\n\nexport { AnimatedCalendar };\n",
      "type": "registry:component",
      "target": "components/calendar-date-picker/_parts/animated-calendar.tsx"
    }
  ],
  "categories": [
    "components",
    "calendar-date-picker"
  ],
  "type": "registry:component"
}