Product Details
Product overview with a recoverable image, variant content, live stock limits, and add-to-cart action.
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 product-detailsSource files
Expand a file to read the source the installer copies into your app.
components/blocks/product-details/ProductDetails.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import {
Image,
ImageFallback,
ImageLoading,
ImageRetry,
ImageSource,
type ImageSourceValue,
} from '@/components/ui/image'
import { QuantityStepper } from '@/components/ui/quantity-stepper'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'
export interface ProductDetailsProps extends ViewProps {
className?: string
title: string
/** Price for the selected variant, formatted by the caller. */
price: string
description?: string
image?: ImageSourceValue
imageLabel?: string
/** Number of units still available for this purchase. Zero disables adding. */
availableQuantity?: number
unavailableReason?: string
error?: string
loading?: boolean
quantityLabel?: string
addLabel?: string
soldOutLabel?: string
photoUnavailableLabel?: string
retryPhotoLabel?: string
/** Variant controls or product details authored by the caller. */
children?: React.ReactNode
onAdd?: (quantity: number) => void
}
export function ProductDetails({
className,
title,
price,
description,
image,
imageLabel = title,
availableQuantity,
unavailableReason,
error,
loading = false,
quantityLabel = 'Quantity',
addLabel = 'Add to cart',
soldOutLabel = 'Sold out',
photoUnavailableLabel = 'Photo unavailable',
retryPhotoLabel = 'Retry photo',
children,
onAdd,
...props
}: ProductDetailsProps) {
const stock =
availableQuantity != null && Number.isSafeInteger(availableQuantity) ? Math.max(0, availableQuantity) : 99
const maximum = Math.max(1, stock)
const blocked = stock === 0 || Boolean(unavailableReason)
const reason = unavailableReason || (stock === 0 ? soldOutLabel : undefined)
const [requested, setRequested] = React.useState(1)
const quantity = Math.min(requested, maximum)
useAnnouncement(error || reason)
React.useEffect(() => {
if (requested > maximum) setRequested(maximum)
}, [requested, maximum])
return (
<View {...props} className={cn('gap-[20px]', className)}>
{image !== undefined ? (
<Image source={image} style={{ aspectRatio: 3 / 2 }}>
<ImageSource accessible accessibilityRole="image" accessibilityLabel={imageLabel} />
<ImageLoading />
<ImageFallback>
<Text size="sm" variant="muted" className="text-center">
{photoUnavailableLabel}
</Text>
<ImageRetry accessibilityLabel={retryPhotoLabel}>{retryPhotoLabel}</ImageRetry>
</ImageFallback>
</Image>
) : null}
<View className="gap-[8px]">
<Text size="title" accessibilityRole="header">
{title}
</Text>
<Text size="lg" className="font-semibold" style={{ fontVariant: ['tabular-nums'] }}>
{price}
</Text>
{description ? <Text variant="muted">{description}</Text> : null}
</View>
{children}
{reason ? (
<Text size="sm" variant="muted">
{reason}
</Text>
) : null}
{error ? (
<Text size="sm" accessibilityRole="alert" className="text-destructive">
{error}
</Text>
) : null}
{onAdd ? (
<>
<View className="flex-row flex-wrap items-center justify-between gap-[8px]">
<Text size="sm" aria-hidden className="shrink font-medium">
{quantityLabel}
</Text>
<QuantityStepper
accessibilityLabel={quantityLabel}
value={quantity}
min={1}
max={maximum}
disabled={loading || blocked}
onValueChange={setRequested}
/>
</View>
<Button
accessibilityLabel={addLabel}
disabled={blocked}
loading={loading}
onPress={() => {
if (!loading && !blocked) onAdd(quantity)
}}
>
{addLabel}
</Button>
</>
) : null}
</View>
)
}
components/blocks/product-details/index.ts
export { ProductDetails, type ProductDetailsProps } from './ProductDetails'