Skip to content

Commit

Permalink
replace HOCs with hooks, props destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
jczekalski committed Jul 24, 2023
1 parent efce4e4 commit 58e6de5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
36 changes: 20 additions & 16 deletions src/components/Tooltip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import {Animated} from 'react-native';
import {BoundsObserver} from '@react-ng/bounds-observer';
import TooltipRenderedOnPageBody from './TooltipRenderedOnPageBody';
import Hoverable from '../Hoverable';
import withWindowDimensions from '../withWindowDimensions';
import * as tooltipPropTypes from './tooltipPropTypes';
import TooltipSense from './TooltipSense';
import * as DeviceCapabilities from '../../libs/DeviceCapabilities';
import compose from '../../libs/compose';
import withLocalize from '../withLocalize';
import usePrevious from '../../hooks/usePrevious';
import useLocalize from '../../hooks/useLocalize';
import useWindowDimensions from '../../hooks/useWindowDimensions';

const hasHoverSupport = DeviceCapabilities.hasHoverSupport();

Expand All @@ -21,6 +20,11 @@ const hasHoverSupport = DeviceCapabilities.hasHoverSupport();
* @returns {ReactNodeLike}
*/
function Tooltip(props) {
const {children, numberOfLines, maxWidth, text, renderTooltipContent, renderTooltipContentKey} = props;

const {preferredLocale} = useLocalize();
const {windowWidth} = useWindowDimensions();

// Is tooltip already rendered on the page's body? happens once.
const [isRendered, setIsRendered] = useState(false);
// Is the tooltip currently visible?
Expand All @@ -37,7 +41,7 @@ function Tooltip(props) {
const isTooltipSenseInitiator = useRef(false);
const animation = useRef(new Animated.Value(0));
const isAnimationCanceled = useRef(false);
const prevText = usePrevious(props.text);
const prevText = usePrevious(text);

/**
* Display the tooltip in an animation.
Expand Down Expand Up @@ -72,11 +76,11 @@ function Tooltip(props) {
useEffect(() => {
// if the tooltip text changed before the initial animation was finished, then the tooltip won't be shown
// we need to show the tooltip again
if (isVisible && isAnimationCanceled.current && props.text && prevText !== props.text) {
if (isVisible && isAnimationCanceled.current && text && prevText !== text) {
isAnimationCanceled.current = false;
showTooltip();
}
}, [isVisible, props.text, prevText, showTooltip]);
}, [isVisible, text, prevText, showTooltip]);

/**
* Update the tooltip bounding rectangle
Expand Down Expand Up @@ -118,29 +122,29 @@ function Tooltip(props) {

// Skip the tooltip and return the children if the text is empty,
// we don't have a render function or the device does not support hovering
if ((_.isEmpty(props.text) && props.renderTooltipContent == null) || !hasHoverSupport) {
return props.children;
if ((_.isEmpty(text) && renderTooltipContent == null) || !hasHoverSupport) {
return children;
}

return (
<>
{isRendered && (
<TooltipRenderedOnPageBody
animation={animation.current}
windowWidth={props.windowWidth}
windowWidth={windowWidth}
xOffset={xOffset}
yOffset={yOffset}
targetWidth={wrapperWidth}
targetHeight={wrapperHeight}
shiftHorizontal={_.result(props, 'shiftHorizontal')}
shiftVertical={_.result(props, 'shiftVertical')}
text={props.text}
maxWidth={props.maxWidth}
numberOfLines={props.numberOfLines}
renderTooltipContent={props.renderTooltipContent}
text={text}
maxWidth={maxWidth}
numberOfLines={numberOfLines}
renderTooltipContent={renderTooltipContent}
// We pass a key, so whenever the content changes this component will completely remount with a fresh state.
// This prevents flickering/moving while remaining performant.
key={[props.text, ...props.renderTooltipContentKey, props.preferredLocale]}
key={[text, ...renderTooltipContentKey, preferredLocale]}
/>
)}
<BoundsObserver
Expand All @@ -151,7 +155,7 @@ function Tooltip(props) {
onHoverIn={showTooltip}
onHoverOut={hideTooltip}
>
{props.children}
{children}
</Hoverable>
</BoundsObserver>
</>
Expand All @@ -160,4 +164,4 @@ function Tooltip(props) {

Tooltip.propTypes = tooltipPropTypes.propTypes;
Tooltip.defaultProps = tooltipPropTypes.defaultProps;
export default compose(withWindowDimensions, withLocalize)(memo(Tooltip));
export default memo(Tooltip);
4 changes: 0 additions & 4 deletions src/components/Tooltip/tooltipPropTypes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import PropTypes from 'prop-types';
import {windowDimensionsPropTypes} from '../withWindowDimensions';
import variables from '../../styles/variables';
import CONST from '../../CONST';

Expand All @@ -13,9 +12,6 @@ const propTypes = {
/** Children to wrap with Tooltip. */
children: PropTypes.node.isRequired,

/** Props inherited from withWindowDimensions */
...windowDimensionsPropTypes,

/** Any additional amount to manually adjust the horizontal position of the tooltip.
A positive value shifts the tooltip to the right, and a negative value shifts it to the left. */
shiftHorizontal: PropTypes.oneOfType([PropTypes.number, PropTypes.func]),
Expand Down

0 comments on commit 58e6de5

Please sign in to comment.