Skip to content

Commit

Permalink
slim the Carousel card margins
Browse files Browse the repository at this point in the history
The gap between cards should not be bigger than the gap before the first card / after the last card. To get this effect, the first and last card need full size margins, while the inner margins can be half that.
Overall this gives us slimmer margins that are uniform.
  • Loading branch information
JGreenlee committed Sep 7, 2023
1 parent e0c7d82 commit 40a9b5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions www/js/components/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ type Props = {
cardMargin: number,
}
const Carousel = ({ children, cardWidth, cardMargin }: Props) => {
const numCards = React.Children.count(children);
return (
<ScrollView horizontal={true}
decelerationRate={0}
snapToInterval={cardWidth + cardMargin*2}
snapToInterval={cardWidth + cardMargin}
snapToAlignment={"center"}
style={s.carouselScroll}
style={s.carouselScroll(cardMargin)}
contentContainerStyle={{alignItems: 'flex-start'}}>
{React.Children.map(children, child => (
<View style={s.carouselCard(cardWidth, cardMargin)}>
{React.Children.map(children, (child, i) => (
<View style={s.carouselCard(cardWidth, cardMargin, (i == 0), (i == numCards-1))}>
{child}
</View>
))}
Expand All @@ -24,13 +25,14 @@ const Carousel = ({ children, cardWidth, cardMargin }: Props) => {
};

export const s = {
carouselScroll: {
carouselScroll: (cardMargin) => ({
// @ts-ignore, RN doesn't recognize `scrollSnapType`, but it does work on RN Web
scrollSnapType: 'x mandatory',
paddingVertical: 10,
},
carouselCard: (cardWidth, cardMargin) => ({
margin: cardMargin,
}),
carouselCard: (cardWidth, cardMargin, isFirst, isLast) => ({
marginLeft: isFirst ? cardMargin : cardMargin/2,
marginRight: isLast ? cardMargin : cardMargin/2,
width: cardWidth,
scrollSnapAlign: 'center',
scrollSnapStop: 'always',
Expand Down
2 changes: 1 addition & 1 deletion www/js/metrics/MetricsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const MetricsTab = () => {
</>);
}

export const cardMargin = 8;
export const cardMargin = 10;

export const cardStyles: any = {
card: {
Expand Down

0 comments on commit 40a9b5e

Please sign in to comment.