-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add Carousel component #5123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andriicallstack
wants to merge
4
commits into
callstack:main
Choose a base branch
from
andriicallstack:feat/carousel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0446f67
feat: add Carousel component
andriicallstack 2bc8cc6
refactor(carousel): drop tokens the component cannot consume
andriicallstack 999732c
feat(carousel): settle flings on the M3 spring
andriicallstack bfc2d32
refactor(carousel): collapse to a single testID
andriicallstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /Users/andrii.doroshenko/react-native-paper/example/node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| import * as React from 'react'; | ||
| import { Image, StyleSheet, View } from 'react-native'; | ||
|
|
||
| import { | ||
| Button, | ||
| Carousel, | ||
| CarouselItem, | ||
| CarouselItemContent, | ||
| Switch, | ||
| Text, | ||
| useTheme, | ||
| type CarouselHandle, | ||
| type CarouselLayout, | ||
| } from 'react-native-paper'; | ||
|
|
||
| import ScreenWrapper from '../ScreenWrapper'; | ||
|
|
||
| const photos = [ | ||
| { | ||
| id: 'beach', | ||
| title: 'Beach', | ||
| source: require('../../assets/images/beach.jpg'), | ||
| }, | ||
| { | ||
| id: 'bridge', | ||
| title: 'Bridge', | ||
| source: require('../../assets/images/bridge.jpg'), | ||
| }, | ||
| { | ||
| id: 'city', | ||
| title: 'City', | ||
| source: require('../../assets/images/city.jpg'), | ||
| }, | ||
| { | ||
| id: 'forest', | ||
| title: 'Forest', | ||
| source: require('../../assets/images/forest.jpg'), | ||
| }, | ||
| { | ||
| id: 'chameleon', | ||
| title: 'Chameleon', | ||
| source: require('../../assets/images/chameleon.jpg'), | ||
| }, | ||
| { | ||
| id: 'strawberries', | ||
| title: 'Strawberries', | ||
| source: require('../../assets/images/strawberries.jpg'), | ||
| }, | ||
| ]; | ||
|
|
||
| const Section = ({ | ||
| title, | ||
| caption, | ||
| children, | ||
| }: { | ||
| title: string; | ||
| caption?: string; | ||
| children: React.ReactNode; | ||
| }) => ( | ||
| <View style={styles.section}> | ||
| <Text variant="titleSmall">{title}</Text> | ||
| {caption ? ( | ||
| <Text variant="bodySmall" style={styles.caption}> | ||
| {caption} | ||
| </Text> | ||
| ) : null} | ||
| <View style={styles.carousel}>{children}</View> | ||
| </View> | ||
| ); | ||
|
|
||
| type PhotoCarouselProps = { | ||
| layout: CarouselLayout; | ||
| alignment?: 'start' | 'center'; | ||
| itemWidth?: number; | ||
| height?: number; | ||
| outlined?: boolean; | ||
| disabled?: boolean; | ||
| onIndexChange?: (index: number) => void; | ||
| ref?: React.RefObject<CarouselHandle | null>; | ||
| }; | ||
|
|
||
| const PhotoCarousel = ({ | ||
| layout, | ||
| alignment, | ||
| itemWidth = 220, | ||
| height = 200, | ||
| outlined, | ||
| disabled, | ||
| onIndexChange, | ||
| ref, | ||
| }: PhotoCarouselProps) => { | ||
| const theme = useTheme(); | ||
|
|
||
| return ( | ||
| <Carousel | ||
| ref={ref} | ||
| data={photos} | ||
| layout={layout} | ||
| alignment={alignment} | ||
| height={height} | ||
| itemWidth={itemWidth} | ||
| itemSpacing={8} | ||
| outlined={outlined} | ||
| disabled={disabled} | ||
| onIndexChange={onIndexChange} | ||
| keyExtractor={(photo) => photo.id} | ||
| aria-label="Photos" | ||
| renderItem={({ item, mask }) => ( | ||
| <CarouselItem mask={mask}> | ||
| <Image | ||
| source={item.source} | ||
| style={styles.image} | ||
| resizeMode="cover" | ||
| accessibilityIgnoresInvertColors | ||
| /> | ||
| {/* Pinned to the mask's leading edge, so it slides with the visible | ||
| rectangle and fades out as the item collapses. */} | ||
| <CarouselItemContent mask={mask} style={styles.label}> | ||
| <View | ||
| style={[ | ||
| styles.labelPill, | ||
| { | ||
| backgroundColor: theme.colors.surface, | ||
| borderRadius: theme.shapes.corner.small, | ||
| }, | ||
| ]} | ||
| > | ||
| <Text variant="labelLarge">{item.title}</Text> | ||
| </View> | ||
| </CarouselItemContent> | ||
| </CarouselItem> | ||
| )} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| const CarouselExample = () => { | ||
| const carouselRef = React.useRef<CarouselHandle>(null); | ||
| const [focused, setFocused] = React.useState(0); | ||
| const [outlined, setOutlined] = React.useState(false); | ||
| const [disabled, setDisabled] = React.useState(false); | ||
|
|
||
| return ( | ||
| <ScreenWrapper contentContainerStyle={styles.container}> | ||
| <Text variant="bodySmall" style={styles.caption}> | ||
| Items are never resized. What changes as an item moves through the | ||
| keylines is its mask rectangle and a translation — every item stays | ||
| measured at the large size throughout, which is why the photos never | ||
| reflow. | ||
| </Text> | ||
|
|
||
| <Section | ||
| title="Multi-browse" | ||
| caption="One or two large items, then a medium and a small one. Momentum decays across several items before it settles." | ||
| > | ||
| <PhotoCarousel | ||
| ref={carouselRef} | ||
| layout="multi-browse" | ||
| onIndexChange={setFocused} | ||
| /> | ||
| </Section> | ||
| <Text variant="bodySmall" style={styles.caption}> | ||
| Focused item: {photos[focused]?.title} | ||
| </Text> | ||
| {/* Programmatic moves settle on the same spring a fling does. */} | ||
| <View style={styles.row}> | ||
| <Button | ||
| mode="outlined" | ||
| onPress={() => carouselRef.current?.scrollToIndex(focused - 1)} | ||
| > | ||
| Previous | ||
| </Button> | ||
| <Button | ||
| mode="outlined" | ||
| onPress={() => carouselRef.current?.scrollToIndex(focused + 1)} | ||
| > | ||
| Next | ||
| </Button> | ||
| </View> | ||
|
|
||
| <Section | ||
| title="Hero, start-aligned" | ||
| caption="A single large item with one peek. One item per fling." | ||
| > | ||
| <PhotoCarousel layout="hero" alignment="start" /> | ||
| </Section> | ||
|
|
||
| <Section | ||
| title="Hero, centre-aligned" | ||
| caption="A peek on either side. Falls back to start-aligned below three items." | ||
| > | ||
| <PhotoCarousel layout="hero" alignment="center" /> | ||
| </Section> | ||
|
|
||
| <Section | ||
| title="Uncontained" | ||
| caption="Items keep the width they were given and the carousel does not snap — the layout to use when aspect ratios matter." | ||
| > | ||
| <PhotoCarousel layout="uncontained" itemWidth={260} /> | ||
| </Section> | ||
|
|
||
| <Section title="Full-screen" caption="One item filling the container."> | ||
| <PhotoCarousel layout="full-screen" height={260} /> | ||
| </Section> | ||
|
|
||
| <Section | ||
| title="Outlined and disabled" | ||
| caption="Outlined draws a 1dp border on each item; disabled stops scrolling and drops the items to 38% opacity." | ||
| > | ||
| <PhotoCarousel | ||
| layout="multi-browse" | ||
| outlined={outlined} | ||
| disabled={disabled} | ||
| /> | ||
| </Section> | ||
|
|
||
| <View style={styles.row}> | ||
| <Text variant="bodyMedium">Outlined</Text> | ||
| <Switch value={outlined} onValueChange={setOutlined} /> | ||
| </View> | ||
| <View style={styles.row}> | ||
| <Text variant="bodyMedium">Disabled</Text> | ||
| <Switch value={disabled} onValueChange={setDisabled} /> | ||
| </View> | ||
| </ScreenWrapper> | ||
| ); | ||
| }; | ||
|
|
||
| CarouselExample.title = 'Carousel'; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| paddingVertical: 16, | ||
| gap: 8, | ||
| }, | ||
| section: { | ||
| gap: 4, | ||
| paddingTop: 16, | ||
| }, | ||
| caption: { | ||
| opacity: 0.7, | ||
| paddingHorizontal: 16, | ||
| }, | ||
| carousel: { | ||
| paddingHorizontal: 16, | ||
| }, | ||
| image: { | ||
| width: '100%', | ||
| height: '100%', | ||
| }, | ||
| label: { | ||
| padding: 16, | ||
| }, | ||
| labelPill: { | ||
| paddingHorizontal: 12, | ||
| paddingVertical: 6, | ||
| }, | ||
| row: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| justifyContent: 'space-between', | ||
| paddingHorizontal: 16, | ||
| paddingVertical: 8, | ||
| }, | ||
| }); | ||
|
|
||
| export default CarouselExample; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /Users/andrii.doroshenko/react-native-paper/node_modules |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover from development