UIPackage React Native
Search
Menu

Loyalty Pass

Member pass with a points balance, reward progress, code availability and caller-owned reward actions.

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 loyalty-pass

Source files

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

components/blocks/loyalty-pass/LoyaltyPass.tsx
import type { ReactElement } from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { Progress } from '@/components/ui/progress'
import { Spinner } from '@/components/ui/spinner'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export type LoyaltyPassCodeStatus = 'loading' | 'ready' | 'unavailable'

interface LoyaltyPassLabels {
  points: string
  pointsUnavailable: string
  rewardAt: string
  progress: string
  code: string
  codeLoading: string
  codeUnavailable: string
  redeem: string
  rules: string
}

export interface LoyaltyPassProps extends Omit<ViewProps, 'children'> {
  className?: string
  program: string
  points?: number
  rewardAt?: number
  /** Override threshold-based availability for programs with other eligibility rules. */
  redeemable?: boolean
  redeeming?: boolean
  error?: string
  code?: string
  codeStatus?: LoyaltyPassCodeStatus
  /** Optional scannable member code. Mounted only while the code is ready. */
  verification?: ReactElement
  labels?: Partial<LoyaltyPassLabels>
  formatPoints?: (points: number) => string
  onRedeem?: () => void
  onViewRules?: () => void
}

const pointFormatter = new Intl.NumberFormat()

export function LoyaltyPass({
  className,
  program,
  points,
  rewardAt,
  redeemable,
  redeeming = false,
  error,
  code,
  codeStatus,
  verification,
  labels,
  formatPoints,
  onRedeem,
  onViewRules,
  ...props
}: LoyaltyPassProps) {
  const balance = points != null && Number.isFinite(points) && points >= 0 ? points : undefined
  const threshold = rewardAt != null && Number.isFinite(rewardAt) && rewardAt > 0 ? rewardAt : undefined
  const eligible = redeemable ?? (balance !== undefined && threshold !== undefined && balance >= threshold)
  const hasCode = Boolean(code?.trim())
  const showCode = (codeStatus === undefined || codeStatus === 'ready') && (hasCode || Boolean(verification))
  const format = formatPoints ?? pointFormatter.format
  const pointsLabel = labels?.points ?? 'points'
  useAnnouncement(error)

  return (
    <View {...props} className={cn('gap-[20px] rounded-lg border border-border bg-card p-[16px]', className)}>
      <Text size="title" accessibilityRole="header" className="text-card-foreground">
        {program}
      </Text>
      <View className="gap-[8px]">
        {balance !== undefined ? (
          <View className="flex-row flex-wrap items-baseline gap-[8px]">
            <Text size="title" className="text-card-foreground" style={{ fontVariant: ['tabular-nums'] }}>
              {format(balance)}
            </Text>
            <Text variant="muted">{pointsLabel}</Text>
          </View>
        ) : (
          <Text variant="muted">{labels?.pointsUnavailable ?? 'Point balance unavailable'}</Text>
        )}
        {balance !== undefined && threshold !== undefined ? (
          <>
            <Progress
              accessible
              max={threshold}
              value={balance}
              accessibilityLabel={labels?.progress ?? 'Reward progress'}
            />
            <Text
              size="sm"
              variant="muted"
            >{`${labels?.rewardAt ?? 'Reward at'} ${format(threshold)} ${pointsLabel}`}</Text>
          </>
        ) : null}
      </View>

      {codeStatus !== undefined || hasCode || verification ? (
        <View className="items-center gap-[8px] border-t border-border pt-[16px]">
          {showCode ? (
            <>
              {verification}
              {hasCode ? (
                <>
                  <Text size="sm" variant="muted">
                    {labels?.code ?? 'Member code'}
                  </Text>
                  <Text
                    selectable
                    size="lg"
                    className="text-center font-semibold text-card-foreground"
                    style={{ fontVariant: ['tabular-nums'] }}
                  >
                    {code}
                  </Text>
                </>
              ) : null}
            </>
          ) : codeStatus === 'loading' ? (
            <Spinner accessibilityLabel={labels?.codeLoading ?? 'Loading member code'} />
          ) : (
            <Text size="sm" variant="muted" className="text-center">
              {labels?.codeUnavailable ?? 'Member code unavailable'}
            </Text>
          )}
        </View>
      ) : null}
      {error ? (
        <Text accessibilityRole="alert" size="sm" className="text-destructive">
          {error}
        </Text>
      ) : null}
      {onRedeem || onViewRules ? (
        <View className="gap-[8px]">
          {onRedeem ? (
            <Button
              accessibilityLabel={labels?.redeem ?? 'Redeem reward'}
              disabled={!eligible}
              loading={redeeming}
              onPress={() => {
                if (eligible && !redeeming) onRedeem()
              }}
            >
              {labels?.redeem ?? 'Redeem reward'}
            </Button>
          ) : null}
          {onViewRules ? (
            <Button variant="outline" accessibilityLabel={labels?.rules ?? 'Program rules'} onPress={onViewRules}>
              {labels?.rules ?? 'Program rules'}
            </Button>
          ) : null}
        </View>
      ) : null}
    </View>
  )
}
components/blocks/loyalty-pass/index.ts
export { LoyaltyPass, type LoyaltyPassCodeStatus, type LoyaltyPassProps } from './LoyaltyPass'

Dependencies

Browse shopping and reservations