Skip to main content

Shop filters sheet

A product list with a Filters button that opens a bottom sheet: a category, a price range, a size and brands. The Show 6 results button counts as you change things, Reset clears them, and nothing on the list changes until you apply. On the web the sheet opens as a centered dialog.

Loading…
import { useState } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { BottomSheet, Button, Checkbox, ChipGroup, HStack, Slider, Text, useDraft, useTheme } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

type Filters = { category: string; price: [number, number]; size: string | null; brands: string[] };

const PRODUCTS = [
  { name: 'Trail runner GTX', brand: 'Fjord', category: 'Sneakers', price: 149, sizes: ['40', '41', '42', '43'], tint: '#D9E4DD' },
  { name: 'Court low, chalk', brand: 'Arc', category: 'Sneakers', price: 95, sizes: ['38', '39', '40', '41'], tint: '#ECE7DF' },
  { name: 'Canvas high top', brand: 'Mesa', category: 'Sneakers', price: 69, sizes: ['39', '40', '42'], tint: '#E3E1F0' },
  { name: 'Waxed field jacket', brand: 'Nord', category: 'Jackets', price: 229, sizes: ['40', '42', '44'], tint: '#E6DDD3' },
  { name: 'Packable rain shell', brand: 'Tide', category: 'Jackets', price: 129, sizes: ['38', '40', '42'], tint: '#D8E6EE' },
  { name: 'Wool chore coat', brand: 'Kōen', category: 'Jackets', price: 260, sizes: ['42', '44'], tint: '#E9E2D6' },
  { name: 'Roll-top daypack', brand: 'Fjord', category: 'Bags', price: 89, sizes: [], tint: '#DCE5E0' },
  { name: 'Leather tote', brand: 'Verde', category: 'Bags', price: 175, sizes: [], tint: '#EADFD5' },
  { name: 'Recycled runner', brand: 'Tide', category: 'Sneakers', price: 110, sizes: ['40', '41', '43'], tint: '#DDE7EC' },
];
const CATEGORIES = ['All', 'Sneakers', 'Jackets', 'Bags'].map((label) => ({ label, value: label }));
const SIZES = ['38', '39', '40', '41', '42', '43', '44'].map((label) => ({ label, value: label }));
const BRAND_NAMES = ['Arc', 'Fjord', 'Kōen', 'Mesa', 'Nord', 'Tide', 'Verde'];
const PRICE: [number, number] = [0, 300];
const EMPTY: Filters = { category: 'All', price: PRICE, size: null, brands: [] };

const matches = (f: Filters) =>
  PRODUCTS.filter(
    (p) =>
      (f.category === 'All' || p.category === f.category) &&
      p.price >= f.price[0] &&
      p.price <= f.price[1] &&
      (!f.size || p.sizes.includes(f.size)) &&
      (!f.brands.length || f.brands.includes(p.brand))
  );
const countActive = (f: Filters) =>
  Number(f.category !== 'All') + Number(f.price[0] > PRICE[0] || f.price[1] < PRICE[1]) + Number(!!f.size) + f.brands.length;

export default function ShopFilters() {
  const { colors, radius } = useTheme();
  const [open, setOpen] = useState(false);
  const [filters, setFilters] = useState<Filters>({ ...EMPTY, category: 'Sneakers' });
  const [draft, setDraft] = useDraft(filters, open);
  const results = matches(filters);
  const preview = matches(draft).length;
  const active = countActive(filters);

  const toggleBrand = (brand: string, on: boolean) =>
    setDraft({ ...draft, brands: on ? [...draft.brands, brand] : draft.brands.filter((b) => b !== brand) });

  return (
    <View style={[styles.screen, { backgroundColor: colors.background }]}>
      <HStack justify="space-between" align="center" style={styles.bar}>
        <View>
          <Text weight="semibold" size="lg">
            {filters.category === 'All' ? 'Everything' : filters.category}
          </Text>
          <Text size="sm" color="textSecondary">
            {results.length} results
          </Text>
        </View>
        <Button variant="outline" size="sm" onPress={() => setOpen(true)} startIcon={<Icon name="sliders" size="sm" color="text" />}>
          {active ? `Filters · ${active}` : 'Filters'}
        </Button>
      </HStack>

      <ScrollView contentContainerStyle={styles.grid}>
        {results.map((p) => (
          <View key={p.name} style={styles.card}>
            <View style={[styles.photo, { backgroundColor: p.tint, borderRadius: radius.lg }]}>
              <Icon name="bag" size={32} color="#00000055" />
            </View>
            <Text size="xs" color="textSecondary">
              {p.brand}
            </Text>
            <Text numberOfLines={1}>{p.name}</Text>
            <Text weight="semibold">€{p.price}</Text>
          </View>
        ))}
        {results.length ? null : (
          <Text color="textSecondary" style={styles.none}>
            Nothing matches. Try fewer filters.
          </Text>
        )}
      </ScrollView>

      <BottomSheet
        open={open}
        onOpenChange={setOpen}
        title="Filters"
        height={540}
        headerRight={
          <Button variant="ghost" size="sm" onPress={() => setDraft(EMPTY)}>
            Reset
          </Button>
        }
        footer={
          <Button
            size="lg"
            disabled={!preview}
            onPress={() => {
              setFilters(draft);
              setOpen(false);
            }}
          >
            {preview ? `Show ${preview} results` : 'No results'}
          </Button>
        }
      >
        <ScrollView contentContainerStyle={styles.sheet}>
          <Text weight="semibold">Category</Text>
          <ChipGroup
            items={CATEGORIES}
            value={draft.category}
            onValueChange={(category) => setDraft({ ...draft, category })}
            accessibilityLabel="Category"
          />
          <HStack justify="space-between">
            <Text weight="semibold">Price</Text>
            <Text color="textSecondary">
              €{draft.price[0]} – €{draft.price[1]}
              {draft.price[1] === PRICE[1] ? '+' : ''}
            </Text>
          </HStack>
          <Slider
            range
            min={PRICE[0]}
            max={PRICE[1]}
            step={10}
            value={draft.price}
            onValueChange={(price) => setDraft({ ...draft, price })}
            thumbLabels={['Minimum price', 'Maximum price']}
            formatValue={(value) => `€${value}`}
          />
          <Text weight="semibold">Size (EU)</Text>
          <ChipGroup
            items={SIZES}
            value={draft.size}
            onValueChange={(size) => setDraft({ ...draft, size: size === draft.size ? null : size })}
            accessibilityLabel="Size"
          />
          <Text weight="semibold">Brand</Text>
          <View>
            {BRAND_NAMES.map((brand) => (
              <Checkbox
                key={brand}
                label={brand}
                checked={draft.brands.includes(brand)}
                onCheckedChange={(on) => toggleBrand(brand, on)}
              />
            ))}
          </View>
        </ScrollView>
      </BottomSheet>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1 },
  bar: { paddingHorizontal: 16, paddingVertical: 12 },
  grid: { flexDirection: 'row', flexWrap: 'wrap', gap: 12, padding: 16, paddingTop: 4 },
  card: { width: '47%', flexGrow: 1, gap: 2 },
  photo: { aspectRatio: 1, alignItems: 'center', justifyContent: 'center', marginBottom: 6 },
  none: { padding: 24, textAlign: 'center', width: '100%' },
  sheet: { gap: 14, paddingHorizontal: 16, paddingBottom: 16 },
});

Components used​

  • BottomSheet: title, a Reset button in headerRight, the Apply button in footer, and a fixed height its body scrolls in
  • useDraft: a copy of the filters to edit, taken each time the sheet opens
  • ChipGroup: category and size, scrolling sideways when they run out of room
  • Slider: range for the price, with step, thumbLabels and formatValue
  • Checkbox: the brands
  • Button, HStack, Text, Patine Icons

How it works​

  • Nothing changes until Apply. useDraft(filters, open) copies filters into draft each time the sheet opens. Edits change only the draft, Apply commits it, and closing any other way throws it away. It's the same mechanism the picker sheets use.
  • The count is a preview. The footer runs the same matches function on the draft, so the button says how many results you'll get before you commit, and turns off at zero.
  • Taller than the sheet? With a fixed height the sheet's body is flex: 1, so the filters scroll inside it while the footer stays put.