Skip to content

Commit 9935463

Browse files
committed
add: wrap isTouchDevice in useMemo and use debounce
Signed-off-by: M-DEV-1 <mahadevankizhakkedathu@gmail.com>
1 parent ea18550 commit 9935463

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/custom/CustomTooltip/customTooltip.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from 'lodash';
2-
import React, { useState } from 'react';
2+
import React, { useMemo, useState } from 'react';
33
import { Tooltip, TooltipProps } from '../../base';
44
import { WHITE } from '../../theme';
55
import { RenderMarkdownTooltip } from '../Markdown';
@@ -29,18 +29,22 @@ function CustomTooltip({
2929
}: CustomTooltipProps): JSX.Element {
3030
const [isOpen, setIsOpen] = useState(false);
3131

32-
const isTouchDevice = () => {
32+
const isTouchDevice = useMemo(() => {
3333
return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
34-
};
34+
}, []);
3535

3636
const handleTouchStart = (event: React.TouchEvent<HTMLDivElement>) => {
3737
event.preventDefault();
3838
setIsOpen(true);
3939
};
4040

41-
const handleTouchEnd = () => {
42-
setTimeout(() => setIsOpen(false), 1500);
43-
};
41+
const handleTouchEnd = useMemo(
42+
() =>
43+
_.debounce(() => {
44+
setIsOpen(false);
45+
}, 1500),
46+
[setIsOpen]
47+
);
4448

4549
return (
4650
<Tooltip
@@ -72,7 +76,7 @@ function CustomTooltip({
7276
},
7377
componentsProps
7478
)}
75-
open={isTouchDevice() ? isOpen : undefined}
79+
open={isTouchDevice ? isOpen : undefined}
7680
title={typeof title === 'string' ? <RenderMarkdownTooltip content={title} /> : title}
7781
placement={placement}
7882
arrow

0 commit comments

Comments
 (0)