Skip to content

Commit

Permalink
fix(web): broken at dropping (#3125)
Browse files Browse the repository at this point in the history
## Description
After upgraded RN from  0.74 to 0.75 ( follow [expo changelog](https://expo.dev/changelog/2024/08-14-react-native-0.75)), web page is broken when I do a drag and drop.

console log: https://gist.github.com/MJRT/149969001b9dd853e6b7d0d413afcea7
expo-device-info: https://gist.github.com/MJRT/2b772c0879f0a867ffb6bcf4a18d8637

It return to normal after adding this check.

<!--
Description and motivation for this PR.

Include 'Fixes #<number>' if this is fixing some issue.
-->

## Test plan

I use `pnpm patch` in my project, and it's all ok.
Since this is a simple and non-invasive change, I didn't write additional test. 

<!--
Describe how did you test this change here.
-->
  • Loading branch information
MJRT authored Oct 8, 2024
1 parent 5846054 commit 671b66f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
6 changes: 5 additions & 1 deletion example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ const EXAMPLES: ExamplesSection[] = [
{ name: 'Double draggable', component: DoubleDraggable },
{ name: 'Rows', component: Rows },
{ name: 'Nested Fling', component: NestedFling },
{ name: 'Combo', component: ComboWithGHScroll },
{
name: 'Combo',
component: ComboWithGHScroll,
unsupportedPlatforms: new Set(['web']),
},
{ name: 'Touchables', component: TouchablesIndex as React.ComponentType },
{ name: 'MouseButtons', component: MouseButtons },
{
Expand Down
27 changes: 23 additions & 4 deletions src/RNGestureHandlerModule.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import NodeManager from './web/tools/NodeManager';
import * as HammerNodeManager from './web_hammer/NodeManager';
import { GestureHandlerWebDelegate } from './web/tools/GestureHandlerWebDelegate';

// init method is called inside attachGestureHandler function. However, this function may
// fail when received view is not valid HTML element. On the other hand, dropGestureHandler
// will be called even if attach failed, which will result in crash.
//
// We use this flag to check whether or not dropGestureHandler should be called.
let shouldPreventDrop = false;

export default {
handleSetJSResponder(tag: number, blockNativeResponder: boolean) {
console.warn('handleSetJSResponder: ', tag, blockNativeResponder);
Expand Down Expand Up @@ -60,10 +67,18 @@ export default {
_actionType: ActionType,
propsRef: React.RefObject<unknown>
) {
if (
!(newView instanceof HTMLElement || newView instanceof React.Component)
) {
return;
if (!(newView instanceof Element || newView instanceof React.Component)) {
shouldPreventDrop = true;

const handler = isNewWebImplementationEnabled()

Check warning on line 73 in src/RNGestureHandlerModule.web.ts

View workflow job for this annotation

GitHub Actions / check

Unsafe assignment of an `any` value
? NodeManager.getHandler(handlerTag)
: HammerNodeManager.getHandler(handlerTag);

const handlerName = handler.constructor.name;

Check warning on line 77 in src/RNGestureHandlerModule.web.ts

View workflow job for this annotation

GitHub Actions / check

Unsafe assignment of an `any` value

Check warning on line 77 in src/RNGestureHandlerModule.web.ts

View workflow job for this annotation

GitHub Actions / check

Unsafe member access .constructor on an `any` value

throw new Error(
`${handlerName} with tag ${handlerTag} received child that is not valid HTML element.`

Check warning on line 80 in src/RNGestureHandlerModule.web.ts

View workflow job for this annotation

GitHub Actions / check

Invalid type "any" of template literal expression
);
}

if (isNewWebImplementationEnabled()) {
Expand Down Expand Up @@ -94,6 +109,10 @@ export default {
}
},
dropGestureHandler(handlerTag: number) {
if (shouldPreventDrop) {
return;
}

if (isNewWebImplementationEnabled()) {
NodeManager.dropGestureHandler(handlerTag);
} else {
Expand Down

0 comments on commit 671b66f

Please sign in to comment.