Skip to content
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

No need to specify height prop #668

Open
7dp opened this issue Sep 11, 2024 · 3 comments
Open

No need to specify height prop #668

7dp opened this issue Sep 11, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@7dp
Copy link

7dp commented Sep 11, 2024

Is your feature request related to a problem? Please describe.
I'm always frustrated when this lib requires the height prop.
The problem is i can't determine the height of my items. Slightly dynamic, maybe 2 px or 5 px difference between items.

Describe the solution you'd like
Just like snap-carousel, no need to specify height prop. The lib will handle it automagically.

Describe alternatives you've considered
For now i just rollback to my previous implementation using snap-carousel.

Additional context
I hope this very nice lib can handle that in the near future.
Thank you so much 🐐. 🙏🔥

@7dp 7dp added the enhancement New feature or request label Sep 11, 2024
Copy link

dosubot bot commented Sep 11, 2024

Hey @7dp! 👋 I'm here to help you with any bugs, questions, or contributions you have for the library. Let's squash those bugs together!

The react-native-reanimated-carousel currently requires the height prop, especially for vertical carousels, to avoid errors. If the height prop is omitted in vertical mode, an error will be thrown [1].

However, you can achieve dynamic height adjustments by using custom animations. Implement a callback function of type TAnimationStyle and pass it to the customAnimation property of the Carousel. This allows you to manipulate the height of items dynamically based on their position [2].

Here's an example:

const animationStyle: TAnimationStyle = React.useCallback((value: number) => {
    'worklet';

    const height = interpolate(value, [-1, 0, 1], [100, 200, 100]); // Adjust heights dynamically

    return {
        height,
    };
}, []);

<Carousel
    style={{ width: screen.width, height: 240 }}
    width={screen.width}
    data={[...new Array(6).keys()]}
    customAnimation={animationStyle}
    renderItem={({ index, animationValue }) => {
        return (
            <CustomItem
                key={index}
                index={index}
                animationValue={animationValue}
            />
        );
    }}
/>;

const CustomItem = ({ index, animationValue }) => {
    const maskStyle = useAnimatedStyle(() => {
        const backgroundColor = interpolateColor(
            animationValue.value,
            [-1, 0, 1],
            ['#000000dd', 'transparent', '#000000dd']
        );

        return {
            backgroundColor,
        };
    }, [animationValue]);

    return (
        <View style={{ flex: 1 }}>
            <SBItem key={index} index={index} style={{ borderRadius: 0 }} />
            <Animated.View
                pointerEvents="none"
                style={[
                    {
                        position: 'absolute',
                        top: 0,
                        left: 0,
                        right: 0,
                        bottom: 0,
                    },
                    maskStyle,
                ]}
            />
        </View>
    );
};

This approach allows for dynamic height adjustments without explicitly setting the height prop for each item.

To continue talking to Dosu, mention @dosu.

@7dp
Copy link
Author

7dp commented Sep 11, 2024

My use case is horizontal carousel.
Thus, i'm not able to tell explicitly which item that has more/less height than the others.
What i want is just like the snap-carousel did.

@jony89
Copy link

jony89 commented Feb 5, 2025

If I set fixed height, and users has different font size and different display size, the content can get overflow the height of the carousel.

This is forcing me to add ScrollView in each render item, or unneeded extra height, eventually I get two scrolls one for the rendered item and one of the page it self which obviously is not the expected result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants