UIPackage React Native
Search
Menu

Week Strip

A native seven-day picker with local date keys, checked state, locale formatting, and responsive layout.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add week-strip

Source files

Expand a file to read the source the installer copies into your app.

components/ui/week-strip/WeekStrip.tsx
import * as React from 'react'
import { Pressable, View, useWindowDimensions, type ViewProps } from 'react-native'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface WeekStripProps extends Omit<ViewProps, 'children'> {
  className?: string
  /** A Gregorian date key for any day in the week, in YYYY-MM-DD form. */
  weekOf: string
  /** Sunday is 0 and Monday is 1. */
  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6
  value?: string | null
  defaultValue?: string | null
  onValueChange?: (day: string) => void
  isDateDisabled?: (day: string) => boolean
  /** Adds a visible dot and an accessible label to a day. */
  getDateMarker?: (day: string) => string | undefined
  disabled?: boolean
  locale?: string
  emptyLabel?: string
}

function dayKey(date: Date) {
  return date.toISOString().slice(0, 10)
}

function weekDays(weekOf: string, weekStartsOn: number) {
  if (!/^\d{4}-\d{2}-\d{2}$/.test(weekOf) || !Number.isInteger(weekStartsOn) || weekStartsOn < 0 || weekStartsOn > 6) {
    return []
  }
  const anchor = new Date(`${weekOf}T12:00:00Z`)
  if (!Number.isFinite(anchor.getTime()) || dayKey(anchor) !== weekOf) return []

  // UTC arithmetic is only for the date keys, with no time-of-day conversion at DST boundaries.
  const offset = (anchor.getUTCDay() - weekStartsOn + 7) % 7
  const days = Array.from({ length: 7 }, (_, index) => {
    const date = new Date(anchor)
    date.setUTCDate(anchor.getUTCDate() - offset + index)
    return date
  })
  return days[0].getUTCFullYear() < 1 || days[6].getUTCFullYear() > 9999 ? [] : days
}

export function weekDateKeys(weekOf: string, weekStartsOn: WeekStripProps['weekStartsOn'] = 1) {
  return weekDays(weekOf, weekStartsOn).map(dayKey)
}

export function WeekStrip({
  className,
  weekOf,
  weekStartsOn = 1,
  value,
  defaultValue,
  onValueChange,
  isDateDisabled,
  getDateMarker,
  disabled = false,
  locale,
  emptyLabel = 'This week is unavailable.',
  accessibilityLabel = 'Choose a day',
  onLayout,
  ...props
}: WeekStripProps) {
  const [internal, setInternal] = React.useState(defaultValue)
  const [measuredWidth, setMeasuredWidth] = React.useState<number>()
  const { width, fontScale } = useWindowDimensions()
  const availableWidth = measuredWidth ?? width
  const stacked = fontScale >= 1.4 || availableWidth < 208
  const basis = stacked ? '100%' : availableWidth < 364 ? '25%' : '14.2857%'
  const checkedDay = value === undefined ? internal : value
  const days = React.useMemo(() => weekDays(weekOf, weekStartsOn), [weekOf, weekStartsOn])
  const formats = React.useMemo(() => {
    const dateOptions = { timeZone: 'UTC', calendar: 'gregory' } as const
    return {
      weekday: new Intl.DateTimeFormat(locale, { ...dateOptions, weekday: 'short' }),
      longWeekday: new Intl.DateTimeFormat(locale, { ...dateOptions, weekday: 'long' }),
      day: new Intl.DateTimeFormat(locale, { ...dateOptions, day: 'numeric' }),
      monthAndDay: new Intl.DateTimeFormat(locale, { ...dateOptions, month: 'short', day: 'numeric' }),
      full: new Intl.DateTimeFormat(locale, {
        ...dateOptions,
        weekday: 'long',
        month: 'long',
        day: 'numeric',
        year: 'numeric',
      }),
    }
  }, [locale])

  return (
    <View
      {...props}
      accessible={false}
      accessibilityRole={days.length ? 'radiogroup' : undefined}
      accessibilityLabel={days.length ? accessibilityLabel : undefined}
      className={cn('flex-row flex-wrap', className)}
      onLayout={(event) => {
        setMeasuredWidth(event.nativeEvent.layout.width)
        onLayout?.(event)
      }}
    >
      {days.length ? (
        days.map((date) => {
          const day = dayKey(date)
          const checked = checkedDay === day
          const blocked = disabled || !!isDateDisabled?.(day)
          const marker = getDateMarker?.(day)
          const dateLabel = formats.full.format(date)
          return (
            <View key={day} style={{ width: basis }} className="p-0.5">
              <Pressable
                accessible
                accessibilityRole="radio"
                accessibilityLabel={marker ? `${dateLabel}, ${marker}` : dateLabel}
                accessibilityState={{ checked, disabled: blocked }}
                disabled={blocked}
                onPress={() => {
                  if (blocked || checked) return
                  if (value === undefined) setInternal(day)
                  onValueChange?.(day)
                }}
                className={cn(
                  'min-h-[48px] items-center justify-center rounded-lg border border-transparent px-1 py-2 active:bg-muted',
                  stacked && 'flex-row flex-wrap justify-between gap-2 px-3',
                  checked && 'border-primary bg-muted',
                  blocked && 'opacity-50',
                )}
              >
                <Text size="sm" variant="muted" className={stacked ? 'shrink' : 'text-center'}>
                  {(stacked ? formats.longWeekday : formats.weekday).format(date)}
                </Text>
                <Text
                  size="sm"
                  className={cn(stacked ? 'shrink' : 'text-center', checked && 'font-semibold')}
                  style={{ fontVariant: ['tabular-nums'] }}
                >
                  {(stacked ? formats.monthAndDay : formats.day).format(date)}
                </Text>
                {getDateMarker ? (
                  <View aria-hidden className={cn('size-1.5 rounded-full', marker && 'bg-primary')} />
                ) : null}
              </Pressable>
            </View>
          )
        })
      ) : (
        <Text size="sm" variant="muted">
          {emptyLabel}
        </Text>
      )}
    </View>
  )
}
components/ui/week-strip/index.ts
export { WeekStrip, weekDateKeys, type WeekStripProps } from './WeekStrip'

Dependencies

Registry items

Browse forms and selection