Skip to content

fix: wrong useWindowDimensions on iPad#957

Merged
kirillzyusko merged 1 commit intomainfrom
fix/wrong-use-window-dimensions-on-ipad
May 25, 2025
Merged

fix: wrong useWindowDimensions on iPad#957
kirillzyusko merged 1 commit intomainfrom
fix/wrong-use-window-dimensions-on-ipad

Conversation

@kirillzyusko
Copy link
Copy Markdown
Owner

@kirillzyusko kirillzyusko commented May 25, 2025

📜 Description

Add support for windowDidResize event on iOS.

💡 Motivation and Context

In #948 I changed implementation and added subscription to screen values instead of window.

While it fixes some issues it doesn't work on iPad 🤯 This is very sad that such basic functionality can not be handled by RN out-of-the-box. However we already use own useWindowDimensions hook on Android. So in this PR I decided to replicate that behavior on iOS.

react-native-safe-area-context doesn't have that problem, but react-native-safe-area-context handles that event in layoutSubviews. After thinking a while I decided to use the same approach and emit event windowDidResize. And this approach actually works 🙂 I also added a check to not emit events too frequently if dimensions weren't changed.

The PR #956 also aims to solve that problem, but useWindowDimensions is used by other components, such as KeyboardAvoidingView/KeyboardAwareScrollView, so the problem should be fixed in hook itself.

Closes #955

📢 Changelog

iOS

  • emit windowDidResize event with device screen dimensions;

🤔 How Has This Been Tested?

Tested manually in reproduction example + by e2e tests.

📸 Screenshots (if appropriate):

image

📝 Checklist

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

@kirillzyusko kirillzyusko self-assigned this May 25, 2025
@kirillzyusko kirillzyusko added 🐛 bug Something isn't working 🍎 iOS iOS specific labels May 25, 2025
@github-actions
Copy link
Copy Markdown
Contributor

📊 Package size report

Current size Target Size Difference
197415 bytes 199639 bytes -2224 bytes 📉

@kirillzyusko kirillzyusko marked this pull request as ready for review May 25, 2025 18:22
@kirillzyusko kirillzyusko merged commit 427374f into main May 25, 2025
18 checks passed
@kirillzyusko kirillzyusko deleted the fix/wrong-use-window-dimensions-on-ipad branch May 25, 2025 19:57
kirillzyusko added a commit that referenced this pull request Sep 8, 2025
## 📜 Description

Use `window` size instead of `screen`.

## 💡 Motivation and Context

Originally I added code in
#948
(used subscription on `screen` instead of `window`). In
#957
I realized that new approach has downsides and started to use
`layoutSubviews` method instead of subscription to events that apple
provides (and I kept using `screen` size as main source of truth).
However on iPad in multi-task/stage-manager mode screen size may not
reflect a real window size (because if window is floating it can be much
smaller than the screen).

So in this PR I'm refining solutions made in
#948
and
#957.
We still need to use `window` as main source of truth and we need to
read it in `layoutSubviews` because new dimensions may not be available
in events that we receive from notification center.

I tested repo provided in
#948
and
#957
and with new code they still seem to work well:

|948|957|
|----|----|
|<video
src="https://github.com/user-attachments/assets/80755323-dad0-49ef-8cde-916e1f3c525a">|<video
src="https://github.com/user-attachments/assets/72b7b9e9-f385-45df-824e-c392f60f1af7">|

Closes
#1045

## 📢 Changelog

<!-- High level overview of important changes -->
<!-- For example: fixed status bar manipulation; added new types
declarations; -->
<!-- If your changes don't affect one of platform/language below - then
remove this platform/language -->

### JS

- read `window` dimensions instead of `screen`;

### iOS

- use `self.window` instead of `UIScreen.main`;

## 🤔 How Has This Been Tested?

Tested manually on iPad 11 Pro (m4, 16GB, iOS 18.5).

## 📸 Screenshots (if appropriate):

|Before|After|
|-------|-----|
|<img width="1668" height="2420" alt="image"
src="https://github.com/user-attachments/assets/d5ffe241-285a-44f5-869a-ac399308d281"
/>|<img width="1668" height="2420" alt="image"
src="https://github.com/user-attachments/assets/59e73c7d-0618-4140-bec7-4547d9d29a2a"
/>|

## 📝 Checklist

- [x] CI successfully passed
- [x] I added new mocks and corresponding unit-tests if library API was
changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working 🍎 iOS iOS specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new useWindowDimensions on ios has values swapped

1 participant