-
-
Notifications
You must be signed in to change notification settings - Fork 1k
[Web] Assign role for Native gesture in detector
#4130
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
base: main
Are you sure you want to change the base?
Changes from all commits
bd5e26a
5512416
bbb0720
c8ee392
0919334
289c922
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,8 @@ import type { | |
| } from '../../handlers/gestureHandlerCommon'; | ||
| import RNGestureHandlerModule from '../../RNGestureHandlerModule.web'; | ||
| import { tagMessage } from '../../utils'; | ||
| import type { PropsRef } from '../../web/interfaces'; | ||
| import { NATIVE_GESTURE_ROLE_ATTRIBUTE } from '../../web/constants'; | ||
| import { NativeGestureRole, type PropsRef } from '../../web/interfaces'; | ||
|
|
||
| export interface GestureHandlerDetectorProps extends PropsRef { | ||
| handlerTags: number[]; | ||
|
|
@@ -103,6 +104,39 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => { | |
| }); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any chance to unify this with the one in virtual detector? |
||
| // @ts-ignore This exists on React.ReactNode | ||
| const displayName = children?.type?.displayName as string; | ||
|
|
||
| // In case of Native gesture detector will have only one child | ||
| const child = viewRef.current?.firstChild as HTMLElement | undefined; | ||
|
|
||
| if (!child) { | ||
| return; | ||
| } | ||
|
|
||
| if (displayName === 'ScrollView') { | ||
| child.setAttribute( | ||
| NATIVE_GESTURE_ROLE_ATTRIBUTE, | ||
| NativeGestureRole.ScrollView | ||
| ); | ||
| } else if (displayName === 'Switch') { | ||
|
m-bert marked this conversation as resolved.
|
||
| child.setAttribute( | ||
| NATIVE_GESTURE_ROLE_ATTRIBUTE, | ||
| NativeGestureRole.Switch | ||
| ); | ||
| } else if (displayName === 'Button') { | ||
| child.setAttribute( | ||
| NATIVE_GESTURE_ROLE_ATTRIBUTE, | ||
| NativeGestureRole.Button | ||
| ); | ||
| } | ||
|
|
||
| return () => { | ||
| child.removeAttribute(NATIVE_GESTURE_ROLE_ATTRIBUTE); | ||
| }; | ||
| }, [children]); | ||
|
|
||
| useEffect(() => { | ||
| const shouldUpdateDOMProps = | ||
| propsRef.current.userSelect !== props.userSelect || | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ import { findNodeHandle, Platform } from 'react-native'; | |
|
|
||
| import { Wrap } from '../../../handlers/gestures/GestureDetector/Wrap'; | ||
| import { tagMessage } from '../../../utils'; | ||
| import { NATIVE_GESTURE_ROLE_ATTRIBUTE } from '../../../web/constants'; | ||
| import { NativeGestureRole } from '../../../web/interfaces'; | ||
| import { isComposedGesture } from '../../hooks/utils/relationUtils'; | ||
| import type { DetectorCallbacks, VirtualChild } from '../../types'; | ||
| import type { VirtualDetectorProps } from '../common'; | ||
|
|
@@ -36,7 +38,7 @@ export function VirtualDetector< | |
| const { register, unregister, setMode } = | ||
| useRequiredInterceptingDetectorContext(); | ||
|
|
||
| const viewRef = useRef(null); | ||
| const viewRef = useRef<HTMLElement | null>(null); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be part of the new hook as well |
||
| const [viewTag, setViewTag] = useState<number>(-1); | ||
|
|
||
| const handleRef = useCallback( | ||
|
|
@@ -54,6 +56,34 @@ export function VirtualDetector< | |
| [props.children] | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be a custom hook in its own file, which will no-op on native and contain the logic for web ( |
||
| if (Platform.OS !== 'web' || !viewRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| // @ts-ignore This exists on React.ReactNode | ||
| const displayName = props.children?.type?.displayName as string; | ||
| // On web we need to use `firstChild` as `current` will refer to `Wrap` | ||
| const element = viewRef.current.firstChild as HTMLElement; | ||
|
|
||
|
m-bert marked this conversation as resolved.
|
||
| if (displayName === 'ScrollView') { | ||
| element.setAttribute( | ||
| NATIVE_GESTURE_ROLE_ATTRIBUTE, | ||
| NativeGestureRole.ScrollView | ||
| ); | ||
| } else if (displayName === 'Switch') { | ||
| element.setAttribute( | ||
| NATIVE_GESTURE_ROLE_ATTRIBUTE, | ||
| NativeGestureRole.Switch | ||
| ); | ||
| } else if (displayName === 'Button') { | ||
| element.setAttribute( | ||
| NATIVE_GESTURE_ROLE_ATTRIBUTE, | ||
| NativeGestureRole.Button | ||
| ); | ||
| } | ||
|
m-bert marked this conversation as resolved.
|
||
| }, [props.children, viewTag]); | ||
|
|
||
| useEffect(() => { | ||
| if (viewTag === -1) { | ||
| return; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export const DEFAULT_TOUCH_SLOP = 15; | ||
| export const MINIMAL_RECOGNIZABLE_MAGNITUDE = 0.1; | ||
| export const NATIVE_GESTURE_ROLE_ATTRIBUTE = 'rngh-role'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to change that to
GestureHandlerButtonor do we want to keep it generic?I'm not sure whether this will behave well with other components, given the integration between native gesture and button.