Examples
Photo albums
A list of albums with a Sort menu that ticks the current order, and a ⋯ button on each album that opens its actions: share, rename, or delete in red. The actions come up as a sheet on phones and a menu on the web.
Loading…
import { useState } from 'react'; import { Image, ScrollView, StyleSheet, View } from 'react-native'; import { Button, DropdownMenu, Heading, HStack, Text, useActionSheet, useTheme } from 'patine-ui'; import { Icon } from '@patine-ui/icons'; type Album = { name: string; photos: number; cover: number }; const SORTS = ['Recently added', 'Name', 'Most photos'] as const; type Sort = (typeof SORTS)[number]; const ALBUMS: Album[] = [ { name: 'Yosemite road trip', photos: 214, cover: 1043 }, { name: 'Open studios weekend', photos: 86, cover: 1059 }, { name: 'Hiking the Alps', photos: 152, cover: 1018 }, { name: 'Weeknight dinners', photos: 37, cover: 292 }, { name: 'Coffee places', photos: 58, cover: 431 }, ]; const sorted = (albums: Album[], sort: Sort) => sort === 'Name' ? [...albums].sort((a, b) => a.name.localeCompare(b.name)) : sort === 'Most photos' ? [...albums].sort((a, b) => b.photos - a.photos) : albums; export default function PhotoAlbums() { const { colors, radius } = useTheme(); const [albums, setAlbums] = useState(ALBUMS); const [sort, setSort] = useState<Sort>('Recently added'); const [notice, setNotice] = useState(`${ALBUMS.length} albums`); const { show, actionSheet } = useActionSheet(); const openActions = (name: string) => show({ title: name, actions: [ { label: 'Share', description: 'Send a link to the album', onPress: () => setNotice(`Shared “${name}”`) }, { label: 'Rename', onPress: () => setNotice(`Renaming “${name}”`) }, { label: 'Delete album', destructive: true, onPress: () => { setAlbums((all) => all.filter((a) => a.name !== name)); setNotice(`Deleted “${name}”`); }, }, ], }); return ( <ScrollView style={{ backgroundColor: colors.background }} contentContainerStyle={styles.content}> <View style={styles.titles}> <Heading level={2}>Albums</Heading> <Text color="textSecondary">{notice}</Text> </View> <HStack justify="space-between" align="center"> <Text weight="medium">Sort by</Text> <DropdownMenu title="Sort albums by" accessibilityLabel={`Sort by ${sort}`} actions={SORTS.map((label) => ({ label, selected: label === sort, onPress: () => setSort(label) }))} > <HStack gap="xs" align="center"> <Text color="primary">{sort}</Text> <Icon name="sort" size="sm" color="primary" /> </HStack> </DropdownMenu> </HStack> {sorted(albums, sort).map((album) => ( <HStack key={album.name} gap="md" align="center"> <Image source={{ uri: `https://picsum.photos/id/${album.cover}/120/120` }} style={[styles.cover, { borderRadius: radius.md, backgroundColor: colors.surface }]} accessibilityIgnoresInvertColors /> <View style={styles.flex}> <Text weight="medium">{album.name}</Text> <Text size="sm" color="textSecondary"> {album.photos} photos </Text> </View> <Button variant="ghost" size="icon" accessibilityLabel={`${album.name} options`} onPress={() => openActions(album.name)}> <Icon name="more" /> </Button> </HStack> ))} {actionSheet} </ScrollView> ); } const styles = StyleSheet.create({ content: { gap: 14, padding: 20 }, titles: { gap: 4, marginBottom: 4 }, cover: { width: 52, height: 52 }, flex: { flex: 1 }, });
Components used
- DropdownMenu: the sort order, with
selectedticking the current one - useActionSheet: one action sheet for every album, opened with
show()and the album's own title and actions - Button:
size="icon"for ⋯, named for screen readers - Heading, Text, HStack, Patine Icons
How it works
- One sheet, many albums.
useActionSheetreturnsshowand theactionSheetelement to render once; each ⋯ callsshowwith that album's actions. - Destructive goes last, in red.
destructive: truecolours Delete and keeps it away from the thumb's first reach. - Sorting doesn't touch the data.
sortedreturns a new array for the view; the albums keep their own order underneath.