UIPackage React Native
Search

Size Guide

Readable product size chart with centimeter and inch measurements converted from canonical values.

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 size-guide

Source files

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

components/blocks/size-guide/SizeGuide.tsx
import { useMemo, useState } from 'react'
import { View, type ViewProps } from 'react-native'
import { SegmentedControl, SegmentedControlItem } from '@/components/ui/segmented-control'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export type SizeGuideUnit = 'cm' | 'in'

export interface SizeGuideMeasurement {
  label: string
  /** Canonical millimeters, converted only for display. */
  minMm: number
  maxMm?: number
}

export interface SizeGuideRow {
  id: string
  label: string
  measurements: readonly SizeGuideMeasurement[]
}

interface SizeGuideLabels {
  units: string
  centimeters: string
  inches: string
  centimeterSymbol: string
  inchSymbol: string
  empty: string
  unavailable: string
}

export interface SizeGuideProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  description?: string
  /** A small chart for one product category, with measurements suitable to that category. */
  rows?: readonly SizeGuideRow[]
  unit?: SizeGuideUnit
  defaultUnit?: SizeGuideUnit
  onUnitChange?: (unit: SizeGuideUnit) => void
  locale?: string
  labels?: Partial<SizeGuideLabels>
}

function measureFormat(locale?: string) {
  const options = { maximumFractionDigits: 1 }
  try {
    return new Intl.NumberFormat(locale, options)
  } catch {
    return new Intl.NumberFormat(undefined, options)
  }
}

export function SizeGuide({
  className,
  title = 'Size guide',
  description,
  rows = [],
  unit,
  defaultUnit = 'cm',
  onUnitChange,
  locale,
  labels,
  ...props
}: SizeGuideProps) {
  const [internalUnit, setInternalUnit] = useState<SizeGuideUnit>(defaultUnit)
  const current = unit ?? internalUnit
  const formatter = useMemo(() => measureFormat(locale), [locale])
  const divisor = current === 'cm' ? 10 : 25.4
  const symbol = current === 'cm' ? (labels?.centimeterSymbol ?? 'cm') : (labels?.inchSymbol ?? 'in')

  function describe({ minMm, maxMm }: SizeGuideMeasurement) {
    if (!Number.isFinite(minMm) || minMm <= 0 || (maxMm !== undefined && (!Number.isFinite(maxMm) || maxMm < minMm))) {
      return labels?.unavailable ?? 'Unavailable'
    }
    const lower = formatter.format(minMm / divisor)
    const upper = maxMm === undefined ? lower : formatter.format(maxMm / divisor)
    return `${lower === upper ? lower : `${lower}–${upper}`} ${symbol}`
  }

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <View className="gap-[8px]">
        <Text size="title" accessibilityRole="header">
          {title}
        </Text>
        {description ? <Text variant="muted">{description}</Text> : null}
      </View>
      {rows.length ? (
        <>
          <SegmentedControl
            accessibilityLabel={labels?.units ?? 'Measurement units'}
            value={current}
            onValueChange={(next) => {
              if (next !== 'cm' && next !== 'in') return
              if (unit === undefined) setInternalUnit(next)
              onUnitChange?.(next)
            }}
          >
            <SegmentedControlItem value="cm">{labels?.centimeters ?? 'Centimeters'}</SegmentedControlItem>
            <SegmentedControlItem value="in">{labels?.inches ?? 'Inches'}</SegmentedControlItem>
          </SegmentedControl>
          <View className="gap-[8px]">
            {rows.map((row) => (
              <View key={row.id} className="gap-[8px] rounded-lg border border-border bg-card p-[16px]">
                <Text accessibilityRole="header" className="font-semibold text-card-foreground">
                  {row.label}
                </Text>
                {row.measurements.map((measurement) => (
                  <View key={measurement.label} className="flex-row flex-wrap items-start justify-between gap-[8px]">
                    <Text size="sm" variant="muted" className="min-w-[120px] shrink">
                      {measurement.label}
                    </Text>
                    <Text size="sm" className="shrink text-card-foreground" style={{ fontVariant: ['tabular-nums'] }}>
                      {describe(measurement)}
                    </Text>
                  </View>
                ))}
              </View>
            ))}
          </View>
        </>
      ) : (
        <Text variant="muted">{labels?.empty ?? 'Size measurements aren’t available.'}</Text>
      )}
    </View>
  )
}
components/blocks/size-guide/index.ts
export {
  SizeGuide,
  type SizeGuideMeasurement,
  type SizeGuideProps,
  type SizeGuideRow,
  type SizeGuideUnit,
} from './SizeGuide'

Dependencies

Browse shopping and reservations