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

feat: own useWindowDimensions hook #468

Merged
merged 7 commits into from
Jun 13, 2024

Conversation

kirillzyusko
Copy link
Owner

@kirillzyusko kirillzyusko commented Jun 12, 2024

📜 Description

Implemented own useWindowDimensions hook for Android.

💡 Motivation and Context

The problem with default implementation of useWindowDimensions on Android is that it doesn't work well with edge-to-edge mode and you can not retrieve an actual size of the screen. Here is a brief comparison of values captured on my device (Pixel 7 Pro).

Translucent StatusBar:

height: 867.4285888671875 <- own hook
height: 867.4285888671875, y: 0 <- useSafeAreaFrame
height: 891.4285714285714 <- Dimensions.get('screen')
height: 826.2857142857143 <- Dimensions.get('window')

Non translucent StatusBar:

height: 867.4285888671875 <- own hook
height: 826.2857055664062, y: 41.14285659790039 <- useSafeAreaFrame
height: 891.4285714285714 <- Dimensions.get('screen')
height: 826.2857142857143 <- Dimensions.get('window')

So as you can see it doesn't react properly on the case when StatusBar is translucent and reports incorrect values, which later on causes incorrect layout calculation in components like KeyboardAvoidingView or KeyboardAwareScrollView.

Theoretically we could workaround this problem by original useWindowDimensions().height + StatusBar.currentHeight, but everything become trickier when we add translucent navigationBar (+ translucent statusBar):

height: 891.4285888671875 <- own hook
height: 891.4285888671875, y: 0 <- useSafeAreaFrame
height: 891.4285714285714 <- Dimensions.get('screen')
height: 826.2857142857143 <- Dimensions.get('window')

In this case derived value useWindowDimensions().height + StatusBar.currentHeight (867.4285888671875) still will produce incorrect value and all calculations will be broken. So I decided to create own version of the hook which will cover all the cases.

Issue for reference: facebook/react-native#41918

Closes #434 #334

📢 Changelog

JS

  • export own useWindowDimensions hook;
  • started to use own useWindowDimensions in KeyboardAwareScrollView and KeyboardAvoidingView components;
  • added mock for useWindowDimensions hook as default RN implementation;

Android

  • added WindowDimensionsListener class;
  • added ThemedReactContext.content extension;
  • added ThemedReactContext.setupWindowDimensionListener extension;

🤔 How Has This Been Tested?

Tested manually on Pixel 7 Pro (API 34).

Tested on CI via e2e (API 28).

📸 Screenshots (if appropriate):

Pixel 7 Pro (Android 14), KeyboardAwareScrollView:

KeyboardAwareScrollView

Before After
telegram-cloud-photo-size-2-5429580266013318443-y telegram-cloud-photo-size-2-5429580266013318444-y

KeyboardAvoidingView

Initial Before After
telegram-cloud-photo-size-2-5429580266013318470-y telegram-cloud-photo-size-2-5429580266013318469-y telegram-cloud-photo-size-2-5429580266013318471-y

📝 Checklist

  • CI successfully passed
  • I added new mocks and corresponding unit-tests if library API was changed

@kirillzyusko kirillzyusko added enhancement New feature or request 🤖 android Android specific labels Jun 12, 2024
@kirillzyusko kirillzyusko self-assigned this Jun 12, 2024
@kirillzyusko kirillzyusko changed the title feat: ownuseWindowDimensions hook feat: own useWindowDimensions hook Jun 12, 2024
Copy link
Contributor

github-actions bot commented Jun 12, 2024

📊 Package size report

Current size Target Size Difference
144033 bytes 138194 bytes 5839 bytes 📈

@kirillzyusko kirillzyusko linked an issue Jun 12, 2024 that may be closed by this pull request
@kirillzyusko kirillzyusko force-pushed the fix/own-use-window-dimensions-hook branch from 4c47280 to 01e66b2 Compare June 12, 2024 16:33
@kirillzyusko kirillzyusko marked this pull request as ready for review June 13, 2024 09:45
@kirillzyusko kirillzyusko merged commit 817127f into main Jun 13, 2024
14 of 15 checks passed
@kirillzyusko kirillzyusko deleted the fix/own-use-window-dimensions-hook branch June 13, 2024 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 android Android specific enhancement New feature or request
Projects
None yet
1 participant