Repository navigation
fix(ui): stop capturing Platform in on-device UI worklets - #967
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
On React Native 0.87 with react-native-worklets 0.12 the on-device UI crashes as soon as MobileMenuDrawer mounts, with "[Worklets] Trying to convert a cyclic object to a serializable" or errors thrown from native module getters while Worklets walks the captured values. The menu backdrop and the addons panel both read `Platform.OS` inside a `useAnimatedStyle` worklet. The worklets babel plugin captures every outer value a worklet references and Worklets copies them to the UI runtime, so these worklets carried `Platform` with them, and in the published CommonJS build, where the import compiles to `react_native.Platform.OS`, the entire `react-native` module. Read `Platform.OS` once at module level into `isAndroid` and use that in the worklets, so they only capture a boolean. The backdrop worklet also destructures `animatedIndex` instead of capturing the whole props object. Add a jest test that checks the worklets' `__closure` no longer contains `Platform`; it fails without the fix. Rendering MobileAddonsPanel under jest needs `storybook/` in transformIgnorePatterns, the same change storybookjs#948 makes. Fixes storybookjs#935
🦋 Changeset detectedLatest commit: 37054c4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue: #935
What I did
On React Native 0.87 with react-native-worklets 0.12, the on-device UI crashes as soon as
MobileMenuDrawermounts. The error is[Worklets] Trying to convert a cyclic object to a serializable, or an error thrown from a native module getter while Worklets walks the captured values.Two
useAnimatedStyleworklets readPlatform.OSinside the worklet: the menu backdrop'sandroidTouchEventFixand the addons panel'sextraPadding. The worklets Babel plugin captures every outer value a worklet references, and Worklets copies those values to the UI runtime. From source, that means these worklets carriedPlatformwith them, and its getters call into native modules. In the published CommonJS build the import compiles toreact_native.Platform.OS, so they carried the entirereact-nativemodule.Running the worklets Babel plugin over the built
dist/index.jsshows this.Before:
After:
Changes:
MobileMenuDrawer.tsxandMobileAddonsPanel.tsxreadPlatform.OS === 'android'once at module level intoisAndroid, and the worklets use that, so they only capture a boolean. The backdrop worklet also destructuresanimatedIndexinstead of capturing the whole props object. The non-workletpaddingTopcheck in the backdrop uses the same constant.src/worklets.test.tsxrenders both components and checks that the__closureof everyuseAnimatedStyleworklet is set but doesn't containPlatform. Both tests fail onnext.jest.config.js: addedstorybook/totransformIgnorePatternssoMobileAddonsPanelcan be rendered under jest. This is the same one-line change as fix(ui): stop passing useAnimatedStyle dependencies on native #948.@storybook/react-native-ui.These are the only worklets in the packages.
@storybook/react-native-ui-litedoesn't use Reanimated.Note for merging with #948: both PRs touch the
adjustedBottomSheetSizeworklet, so whichever lands second gets a small conflict there. Keep #948'suseAnimatedStyle(() => ..., Platform.OS === 'web' ? [...] : undefined)form and useisAndroidinside the worklet.How to test
pnpm -F @storybook/react-native-ui testThe new
worklets.test.tsxpasses. If you revertMobileMenuDrawer.tsxandMobileAddonsPanel.tsx, both of its tests fail, and the failure output shows thePlatformobject inside the received closure.Manually, follow the issue's reproduction: React Native 0.87, react-native-reanimated 4.6 and react-native-worklets 0.12, with the on-device UI mounted at launch. The UI should open instead of crashing. On Android, the menu backdrop should still sit behind the sheet when the menu is closed, and the addons panel's maximum height should be unchanged on both platforms.
Fixes #935