-
Notifications
You must be signed in to change notification settings - Fork 3.9k
reset tooltip position on scroll #56209
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
Changes from all commits
a6b5f75
e04e6da
b5957f6
8aa3e13
66a337f
9591074
d694671
ae23518
8dd063b
61008a6
97ba896
039ff0e
008d4f3
0aa4535
14cfc5a
51ce273
d384480
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 |
|---|---|---|
| @@ -1,27 +1,84 @@ | ||
| import {NavigationContext} from '@react-navigation/native'; | ||
| import React, {memo, useContext, useEffect, useRef, useState} from 'react'; | ||
| import type {LayoutRectangle, NativeSyntheticEvent} from 'react-native'; | ||
| import React, {memo, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState} from 'react'; | ||
| import type {LayoutRectangle, NativeMethods, NativeSyntheticEvent} from 'react-native'; | ||
| import {DeviceEventEmitter, Dimensions} from 'react-native'; | ||
| import GenericTooltip from '@components/Tooltip/GenericTooltip'; | ||
| import type {EducationalTooltipProps} from '@components/Tooltip/types'; | ||
| import measureTooltipCoordinate from './measureTooltipCoordinate'; | ||
| import type {EducationalTooltipProps, GenericTooltipState} from '@components/Tooltip/types'; | ||
| import useSafeAreaInsets from '@hooks/useSafeAreaInsets'; | ||
| import variables from '@styles/variables'; | ||
| import CONST from '@src/CONST'; | ||
| import measureTooltipCoordinate, {getTooltipCoordinates} from './measureTooltipCoordinate'; | ||
|
|
||
| type LayoutChangeEventWithTarget = NativeSyntheticEvent<{layout: LayoutRectangle; target: HTMLElement}>; | ||
|
|
||
| type ScrollingEventData = { | ||
| isScrolling: boolean; | ||
| }; | ||
| /** | ||
| * A component used to wrap an element intended for displaying a tooltip. | ||
| * This tooltip would show immediately without user's interaction and hide after 5 seconds. | ||
| */ | ||
| function BaseEducationalTooltip({children, shouldRender = false, shouldHideOnNavigate = true, ...props}: EducationalTooltipProps) { | ||
| const hideTooltipRef = useRef<() => void>(); | ||
| function BaseEducationalTooltip({children, shouldRender = false, shouldHideOnNavigate = true, shouldHideOnScroll = false, ...props}: EducationalTooltipProps) { | ||
| const genericTooltipStateRef = useRef<GenericTooltipState>(); | ||
| const tooltipElementRef = useRef<React.Component & Readonly<NativeMethods>>(); | ||
|
|
||
| const [shouldMeasure, setShouldMeasure] = useState(false); | ||
| const show = useRef<() => void>(); | ||
|
|
||
| const navigator = useContext(NavigationContext); | ||
| const insets = useSafeAreaInsets(); | ||
|
|
||
| const setTooltipPosition = useCallback( | ||
| (isScrolling: boolean) => { | ||
| if (!shouldHideOnScroll || !genericTooltipStateRef.current || !tooltipElementRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| const {hideTooltip, showTooltip, updateTargetBounds} = genericTooltipStateRef.current; | ||
| if (isScrolling) { | ||
| hideTooltip(); | ||
| } else { | ||
| getTooltipCoordinates(tooltipElementRef.current, (bounds) => { | ||
| updateTargetBounds(bounds); | ||
| const {y, height} = bounds; | ||
|
|
||
| const offset = 10; // Tooltip hides when content moves 10px past header/footer. | ||
| const dimensions = Dimensions.get('screen'); | ||
|
Contributor
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. 👋 |
||
| const top = y - (insets.top || 0); | ||
| const bottom = y + height + insets.bottom || 0; | ||
|
|
||
| // Calculate the available space at the top, considering the header height and offset | ||
| const availableHeightForTop = top - (variables.contentHeaderHeight - offset); | ||
|
|
||
| // Calculate the total height available after accounting for the bottom tab and offset | ||
| const availableHeightForBottom = dimensions.height - (bottom + variables.bottomTabHeight - offset); | ||
|
|
||
| if (availableHeightForTop < 0 || availableHeightForBottom < 0) { | ||
| hideTooltip(); | ||
| } else { | ||
| showTooltip(); | ||
| } | ||
| }); | ||
| } | ||
| }, | ||
| [insets, shouldHideOnScroll], | ||
| ); | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (!shouldRender || !shouldHideOnScroll) { | ||
| return; | ||
| } | ||
| setTooltipPosition(false); | ||
| const scrollingListener = DeviceEventEmitter.addListener(CONST.EVENTS.SCROLLING, ({isScrolling}: ScrollingEventData = {isScrolling: false}) => { | ||
| setTooltipPosition(isScrolling); | ||
| }); | ||
|
|
||
| return () => scrollingListener.remove(); | ||
| }, [shouldRender, shouldHideOnScroll, setTooltipPosition]); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| hideTooltipRef.current?.(); | ||
| genericTooltipStateRef.current?.hideTooltip(); | ||
| }; | ||
| }, []); | ||
|
|
||
|
|
@@ -30,7 +87,7 @@ function BaseEducationalTooltip({children, shouldRender = false, shouldHideOnNav | |
| return; | ||
| } | ||
| if (!shouldRender) { | ||
| hideTooltipRef.current?.(); | ||
| genericTooltipStateRef.current?.hideTooltip(); | ||
| return; | ||
| } | ||
| // When tooltip is used inside an animated view (e.g. popover), we need to wait for the animation to finish before measuring content. | ||
|
|
@@ -50,7 +107,7 @@ function BaseEducationalTooltip({children, shouldRender = false, shouldHideOnNav | |
| if (!shouldHideOnNavigate) { | ||
| return; | ||
| } | ||
| hideTooltipRef.current?.(); | ||
| genericTooltipStateRef.current?.hideTooltip(); | ||
| }); | ||
| return unsubscribe; | ||
| }, [navigator, shouldHideOnNavigate]); | ||
|
|
@@ -63,16 +120,18 @@ function BaseEducationalTooltip({children, shouldRender = false, shouldHideOnNav | |
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| > | ||
| {({showTooltip, hideTooltip, updateTargetBounds}) => { | ||
| {(genericTooltipState) => { | ||
| const {updateTargetBounds, showTooltip} = genericTooltipState; | ||
| // eslint-disable-next-line react-compiler/react-compiler | ||
| hideTooltipRef.current = hideTooltip; | ||
| genericTooltipStateRef.current = genericTooltipState; | ||
| return React.cloneElement(children as React.ReactElement, { | ||
| onLayout: (e: LayoutChangeEventWithTarget) => { | ||
| if (!shouldMeasure) { | ||
| setShouldMeasure(true); | ||
| } | ||
| // e.target is specific to native, use e.nativeEvent.target on web instead | ||
| const target = e.target || e.nativeEvent.target; | ||
| tooltipElementRef.current = target; | ||
| show.current = () => measureTooltipCoordinate(target, updateTargetBounds, showTooltip); | ||
| }, | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import {useCallback, useEffect, useRef} from 'react'; | ||
| import {DeviceEventEmitter} from 'react-native'; | ||
| import CONST from '@src/CONST'; | ||
|
|
||
| /** | ||
| * This hook tracks scroll events and emits a "scrolling" event when scrolling starts and ends. | ||
| */ | ||
| const useScrollEventEmitter = () => { | ||
| const isScrollingRef = useRef<boolean>(false); | ||
| const timeoutRef = useRef<NodeJS.Timeout | null>(null); | ||
|
|
||
| const triggerScrollEvent = useCallback(() => { | ||
| const emitScrolling = (isScrolling: boolean) => { | ||
| DeviceEventEmitter.emit(CONST.EVENTS.SCROLLING, { | ||
| isScrolling, | ||
| }); | ||
| }; | ||
|
|
||
| // Start emitting the scrolling event when the scroll begins | ||
| if (!isScrollingRef.current) { | ||
| emitScrolling(true); | ||
| isScrollingRef.current = true; | ||
| } | ||
|
|
||
| // End the scroll and emit after a brief timeout to detect the end of scrolling | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current); | ||
| } | ||
|
|
||
| timeoutRef.current = setTimeout(() => { | ||
| emitScrolling(false); | ||
| isScrollingRef.current = false; | ||
| }, 250); | ||
| }, []); | ||
|
|
||
| // Cleanup timeout on unmount | ||
| useEffect(() => { | ||
| return () => { | ||
| if (!timeoutRef.current) { | ||
| return; | ||
| } | ||
| clearTimeout(timeoutRef.current); | ||
| }; | ||
| }, []); | ||
|
|
||
| return triggerScrollEvent; | ||
| }; | ||
|
|
||
| export default useScrollEventEmitter; |
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.
In theory, we only display 1 educational tooltip at a time. Is it easier if we can reuse the existing scrolling event here
App/src/components/InvertedFlatList/index.tsx
Lines 33 to 48 in 0856182
And we also introduce additional prop to EducationalTooltip (i.e shouldHideOnScroll). Then we check if it's displayed and shouldHideOnScroll, we will hide/display tooltip. Wdyt?
Uh oh!
There was an error while loading. Please reload this page.
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.
We need show/hide tooltip in scrolling for SearchPage as well so I have created new hook. And in future we need to add more we can reuse same hook. So as per me current implementation can be reused in other places as well. Let me know if you have any other opinion.
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.
The problem with this approach:
SelectionList.LHNOptionsList, we probably forgot to remove this scroll event emitter as well.The new hook is a good idea, I like it. It helps reuse. But if we can also have a way to avoid above concerns, that should be great. Any thoughts? Something like put this new hook into
SelectionListThere 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.
@hoangzinh I did not understand what you are trying to say. Can you please provide code snippet about your thoughts?
What I understand is, we need to call triggerScrollEvent() whenever scrolling happened, and also this is not limited to SelectionList right? We can have different types of wrapper which can scroll, so I am not sure if we can make changes in one places and it will be applied to all the places.
Uh oh!
There was an error while loading. Please reload this page.
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.
In
LHNOptionsListwe usedFlashListcomponent while inSearchwe useSelectionListWithModalwhich internally useSelectionListso I think it is better idea to use the new hook and trigger the event whenever necessary.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.
Hmm I see the problem. Basically, what I meant is putting useScrollEventEmitter in SelectionList, so other components using SelectionList, we don't need to initialize
useScrollEventEmitteranymore. Anyways, I'm good with current, but 2 things:Can you add a comment above this line, to explain why do we need this line? It will help to remove/improve later.
App/src/components/LHNOptionsList/LHNOptionsList.tsx
Line 70 in ae23518
I'm still thinking that we don't need to inject tooltipName into
useScrollEventEmitterand simply calluseScrollEventEmitter(). Reason: In some components, we have 2-3 kinds of EducationalTooltip. For example, inOptionRowLHN, it has 2 kinds of tooltipApp/src/components/LHNOptionsList/OptionRowLHN.tsx
Line 73 in fc643d5
If we go with current approach, do we need to setup 2 useScrollEventEmitter?
My thoughts here is we can introduce an additional prop
shouldHideOnScrollin BaseEducationalTooltip, then only setup 1useScrollEventEmitter()inOptionRowLHNand passshouldHideOnScroll={true}hereApp/src/components/LHNOptionsList/OptionRowLHN.tsx
Lines 189 to 201 in fc643d5
wdyt? @mohit6789
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.
Let me try to implement it, and see if this will work or not
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.
Changes pushed