Skip to content

Support auto height layout for vertical carousel #1294

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

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion demo/src/screens/componentScreens/CarouselVerticalScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const BACKGROUND_COLORS = [
Colors.purple60
];

const pageHeight = Constants.windowHeight / 2;
const pageHeight = Constants.windowHeight / 3;

class CarouselVerticalScreen extends Component<{}, State> {
carousel = React.createRef<typeof Carousel>();
Expand Down Expand Up @@ -101,6 +101,17 @@ class CarouselVerticalScreen extends Component<{}, State> {
</Carousel>
{this.renderAnimatedCounter()}
</View>

<View centerH flex marginT-s3>
<Text h3 marginB-s2>
iOS Widgets Carousel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it called this way?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm good question..
Cause it's very similar to the layout of widgets in iOS 😬
I didn't have a better name..
I can call it auto height layout or something like that, but it felt less appealing (:

</Text>
<Carousel horizontal={false} containerStyle={styles.widgetsCarousel}>
<View flex bg-blue30/>
<View flex bg-orange30/>
<View flex bg-green30/>
</Carousel>
</View>
</View>
);
}
Expand Down Expand Up @@ -130,6 +141,13 @@ const styles = StyleSheet.create({
position: 'absolute',
top: 20,
left: 20
},
widgetsCarousel: {
height: 150,
width: 300,
backgroundColor: Colors.grey60,
borderRadius: 24,
overflow: 'hidden'
}
});

Expand Down
9 changes: 6 additions & 3 deletions src/components/carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
pageHeight
});

this.setState({containerWidth, pageWidth, initialOffset});
this.setState({containerWidth, pageWidth, pageHeight, initialOffset});
};

shouldAllowAccessibilityLayout() {
Expand Down Expand Up @@ -327,7 +327,9 @@ class Carousel extends Component<CarouselProps, CarouselState> {

renderChild = (child: ReactNode, key: Key): JSX.Element | undefined => {
if (child) {
const paddingLeft = this.props.horizontal ? this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined : 0;
const {pageWidth, pageHeight} = this.state;
const {horizontal} = this.props;
const paddingLeft = horizontal ? this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined : 0;
const index = Number(key);
const length = presenter.getChildrenLength(this.props);
const containerMarginHorizontal = this.getContainerMarginHorizontal();
Expand All @@ -338,7 +340,8 @@ class Carousel extends Component<CarouselProps, CarouselState> {
return (
<View
style={{
width: this.state.pageWidth,
width: pageWidth,
height: !horizontal ? pageHeight : undefined,
paddingLeft,
marginLeft,
marginRight,
Expand Down