Skip to main content

Product detail

A product page for a bag of coffee: photos to swipe, the average rating, the bag size (the price follows it), a grind (sold-out ones can't be picked), and Add to bag, which asks for a grind first and then confirms with a toast you can act on.

Loading…
import { useState } from 'react';
import { Image, ScrollView, StyleSheet, View } from 'react-native';
import { Button, Carousel, ChipGroup, Heading, HStack, Rating, SegmentedControl, Text, Toaster, toast, useTheme } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const PHOTOS = [
  { id: 766, alt: 'Roasted beans, close up' },
  { id: 1060, alt: 'Brewing a pour-over at the bar' },
  { id: 431, alt: 'A flat white on the counter' },
];
const BAGS = [
  { label: '250 g', value: 250, price: 12 },
  { label: '500 g', value: 500, price: 22 },
  { label: '1 kg', value: 1000, price: 39 },
];
const SOLD_OUT = ['Moka pot'];
const GRINDS = ['Whole bean', 'Espresso', 'Moka pot', 'Pour-over', 'French press'].map((grind) => ({
  label: grind,
  value: grind,
  disabled: SOLD_OUT.includes(grind),
}));
const photo = (id: number) => `https://picsum.photos/id/${id}/800/640`;

export default function ProductDetail() {
  const { colors } = useTheme();
  const [index, setIndex] = useState(0);
  const [bag, setBag] = useState(250);
  const [grind, setGrind] = useState<string | null>(null);
  const [adding, setAdding] = useState(false);
  const [missingGrind, setMissingGrind] = useState(false);
  const price = BAGS.find((b) => b.value === bag)?.price ?? 0;

  const add = () => {
    if (!grind) {
      setMissingGrind(true);
      return;
    }
    setAdding(true);
    setTimeout(() => {
      setAdding(false);
      toast.success('Added to your bag', {
        description: `Alfama blend · ${BAGS.find((b) => b.value === bag)?.label} · ${grind}`,
        action: { label: 'View bag', onPress: () => {} },
      });
    }, 600);
  };

  return (
    <View style={[styles.screen, { backgroundColor: colors.background }]}>
      <ScrollView contentContainerStyle={styles.content}>
        <Carousel value={index} onValueChange={setIndex} labels={PHOTOS.map((p) => p.alt)} accessibilityLabel="Product photos">
          {PHOTOS.map((p) => (
            <Image key={p.id} source={{ uri: photo(p.id) }} style={styles.photo} accessibilityLabel={p.alt} />
          ))}
        </Carousel>

        <View style={styles.details}>
          <View style={styles.titles}>
            <Text size="sm" color="textSecondary">
              Tejo Roasters · Medium roast
            </Text>
            <Heading level={3}>Alfama blend</Heading>
            <HStack gap="sm" align="center">
              <Rating value={4.6} readOnly size={16} accessibilityLabel="Rated 4.6 out of 5" />
              <Text size="sm" color="textSecondary">
                4.6 · 212 reviews
              </Text>
            </HStack>
          </View>
          <Text weight="semibold" size="xl">
            €{price}
          </Text>
          <Text color="textSecondary">
            Brazilian and Ethiopian beans, roasted on Tuesdays in Marvila. Milk chocolate, toasted almond and a little orange peel. Ships the day after roasting.
          </Text>

          <Text weight="semibold">Bag</Text>
          <SegmentedControl items={BAGS} value={bag} onValueChange={setBag} accessibilityLabel="Bag size" />

          <HStack justify="space-between" align="center">
            <Text weight="semibold">Grind</Text>
            <Text size="sm" color={missingGrind ? 'danger' : 'textSecondary'}>
              {missingGrind ? 'Pick a grind first' : 'For your brewer'}
            </Text>
          </HStack>
          <ChipGroup
            items={GRINDS}
            value={grind}
            onValueChange={(next) => {
              setGrind(next);
              setMissingGrind(false);
            }}
            accessibilityLabel="Grind"
          />
        </View>
      </ScrollView>

      <HStack gap="sm" style={[styles.footer, { borderColor: colors.border }]}>
        <Button size="lg" variant="outline" accessibilityLabel="Save for later">
          <Icon name="heart" size="sm" color="text" />
        </Button>
        <Button size="lg" loading={adding} onPress={add} style={styles.flex}>
          Add to bag · €{price}
        </Button>
      </HStack>
      <Toaster />
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1 },
  content: { paddingBottom: 16 },
  photo: { width: '100%', aspectRatio: 5 / 4 },
  details: { gap: 12, padding: 20 },
  titles: { gap: 4 },
  footer: { padding: 16, borderTopWidth: StyleSheet.hairlineWidth },
  flex: { flex: 1 },
});

Components used​

How it works​

  • No silent failure. Add to bag with no grind picked doesn't do nothing: the hint beside Grind turns red and says why.
  • The toast can act. Its action takes the shopper to the bag; it slides away on its own otherwise.
  • The buttons stay in reach, pinned under the scroll view.