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

ScrollView's centerContent prop doesn't work #2331

Closed
1 task done
nandorojo opened this issue Jul 6, 2022 · 5 comments
Closed
1 task done

ScrollView's centerContent prop doesn't work #2331

nandorojo opened this issue Jul 6, 2022 · 5 comments
Labels

Comments

@nandorojo
Copy link
Contributor

nandorojo commented Jul 6, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the issue

As always, thanks for the incredible work of RNW. Onto the issue...

<ScrollView centerContent={true} /> doesn't center the content. Here is a comparison of iOS and Web:

iOS

Screen Shot 2022-07-06 at 5 04 19 PM

Web

Screen Shot 2022-07-06 at 5 04 35 PM

Expected behavior

centerContent should align content to the center.

Steps to reproduce

  1. Add a <ScrollView centerContent>
  2. Put a view inside of it
  3. Compare native and Web behavior

Test case

https://snack.expo.dev/@beatgig/rnw-centercontent-broken

Potential solution

Adjusting contentContainerStyle seems to have fixed it:

import { View, StyleSheet, ScrollView } from 'react-native';

export default function App() {
  return (
    <ScrollView centerContent contentContainerStyle={styles.fix}>
      <View style={{ height: 50, backgroundColor: 'blue' }} />
    </ScrollView>
  );
}

const styles = StyleSheet.create({
  fix: {
    justifyContent: 'center',
    flexGrow: 1,
  },
});

Screen Shot 2022-07-06 at 5 05 12 PM

For additional context: if I didn't add flexGrow: 1, then the content wasn't properly scrollable when it overflowed the size of its container.

If that's the correct solution, please let me know and I'm happy to submit a PR. Thanks!

@nandorojo nandorojo added the bug label Jul 6, 2022
@necolas
Copy link
Owner

necolas commented Jul 6, 2022

If that's the correct solution, please let me know and I'm happy to submit a PR. Thanks!

Thanks! Looks good to me.

…if I didn't add flexGrow: 1, then the content wasn't properly scrollable when it overflowed the size of its container.

I didn't understand this. Is this a different bug? Please can you elaborate?

@nandorojo
Copy link
Contributor Author

Thanks for the quick reply.

I didn't understand this. Is this a different bug? Please can you elaborate?

Yeah happy to elaborate, sorry for the confusion. It's not a separate bug. I just wanted to explain the reasoning behind the solution.

I first tried this:

<ScrollView contentContainerStyle={{ justifyContent: 'center' }} />

This made the content stay up at the top.

Screen Shot 2022-07-06 at 5 49 03 PM

This made the content go up to the top.

I then tried adding flex: 1:

<ScrollView contentContainerStyle={{ justifyContent: 'center', flex: 1 }} />

That appeared to have fixed it.

image

However, as the scrollable content got long, an issue arose:

Screen Shot 2022-07-06 at 5 51 02 PM

^ This screenshot shows the content scrolled all the way to the top. I can't go up more, so the content is essentially getting cut off. Here's a video for clarity.

Once I changed flex: 1flexGrow: 1, the cut-off content got fixed:

image

Here is a video of the working behavior (flexGrow: 1, justifyContent: 'center').

nandorojo added a commit to nandorojo/react-native-web that referenced this issue Jul 6, 2022
@necolas
Copy link
Owner

necolas commented Jul 6, 2022

Worth noting that this prop is not supported on Android

@necolas necolas closed this as completed in 80ef32f Jul 6, 2022
@necolas
Copy link
Owner

necolas commented Jul 6, 2022

Should be available in 0.18.6

@nandorojo
Copy link
Contributor Author

Great, thanks so much for the quick fix.

rnike pushed a commit to VeryBuy/react-native-web that referenced this issue Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants