Skip to content

Commit

Permalink
add delay for highlighing the address
Browse files Browse the repository at this point in the history
  • Loading branch information
tom2drum committed Dec 27, 2023
1 parent fad7516 commit 31c0bfb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/contexts/addressHighlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ export const AddressHighlightContext = React.createContext<TAddressHighlightCont

export function AddressHighlightProvider({ children }: AddressHighlightProviderProps) {
const [ highlightedAddress, setHighlightedAddress ] = React.useState<string | null>(null);
const timeoutId = React.useRef<number | null>(null);

const onMouseEnter = React.useCallback((event: React.MouseEvent) => {
// TODO @tom2drum add throttling
const hash = event.currentTarget.getAttribute('data-hash');
hash && setHighlightedAddress(hash);
if (hash) {
timeoutId.current = window.setTimeout(() => {
setHighlightedAddress(hash);
}, 100);
}
}, []);

const onMouseLeave = React.useCallback(() => {
setHighlightedAddress(null);
typeof timeoutId.current === 'number' && window.clearTimeout(timeoutId.current);
}, []);

const value = React.useMemo(() => {
Expand All @@ -33,6 +38,12 @@ export function AddressHighlightProvider({ children }: AddressHighlightProviderP
};
}, [ highlightedAddress, onMouseEnter, onMouseLeave ]);

React.useEffect(() => {
return () => {
typeof timeoutId.current === 'number' && window.clearTimeout(timeoutId.current);
};
}, []);

return (
<AddressHighlightContext.Provider value={ value }>
{ children }
Expand Down

0 comments on commit 31c0bfb

Please sign in to comment.