Skip to content

Commit

Permalink
perf(useDebounceHook): add new debounce functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
GaroGabrielyan committed Mar 13, 2024
1 parent 08b7f2b commit eb62c5d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
13 changes: 13 additions & 0 deletions src/hooks/useDebounceHook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { useState, useEffect } from 'react';

const useDebounce = (value, delay) => {
if (value === undefined) {
let timeoutId;

const debounceCallback = (value, delay) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(value, delay);
};

const clearDebounce = () => clearTimeout(timeoutId);

return { debounceCallback, clearDebounce };
}

const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
Expand Down
28 changes: 13 additions & 15 deletions src/hooks/useEllipsisDetection.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { useEffect, useState } from 'react';
import useDebounce from './useDebounceHook';

const EQUAL_HEIGHT_DIFF = 3;

const useEllipsisDetection = (ref, externalDependencies = []) => {
const [isTruncated, setIsTruncated] = useState(false);

useEffect(() => {
let timeoutId;
const handleResize = () => {
if (!ref.current) return;
const { scrollWidth, clientWidth, scrollHeight, clientHeight } = ref.current;
setIsTruncated(scrollWidth > clientWidth || scrollHeight > clientHeight + EQUAL_HEIGHT_DIFF);
};
const { debounceCallback, clearDebounce } = useDebounce();

const debounceResize = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(handleResize, 100);
};
const handleResize = () => {
if (!ref.current) return;
const { scrollWidth, clientWidth, scrollHeight, clientHeight } = ref.current;
setIsTruncated(scrollWidth > clientWidth || scrollHeight > clientHeight + EQUAL_HEIGHT_DIFF);
};

window.addEventListener('resize', debounceResize);
useEffect(() => handleResize(), []);

handleResize();
useEffect(() => {
const debounce = () => debounceCallback(handleResize, 100);
window.addEventListener('resize', debounce);

return () => {
window.removeEventListener('resize', debounceResize);
clearTimeout(timeoutId);
clearDebounce();
window.removeEventListener('resize', debounce);
};
}, [
ref,
Expand Down
19 changes: 9 additions & 10 deletions src/hooks/useWindowSize.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import { useState, useEffect } from 'react';
import useDebounce from './useDebounceHook';

function useWindowSize() {
const w = window;
const { innerHeight, innerWidth } = w;
const [width, setWindowWidth] = useState(innerWidth);
const [height, setWindowHeight] = useState(innerHeight);
let timeoutId;
const { debounceCallback, clearDebounce } = useDebounce();

const handleResize = () => {
const { innerHeight, innerWidth } = w;
setWindowWidth(innerWidth);
setWindowHeight(innerHeight);
};

const debounceResize = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(handleResize, 100);
};
const debounce = () => debounceCallback(handleResize, 100);

useEffect(() => {
w.addEventListener('resize', debounceResize);
w.addEventListener('orientationChange', debounceResize);
w.addEventListener('resize', debounce);
w.addEventListener('orientationChange', debounce);

return () => {
w.removeEventListener('resize', debounceResize);
w.removeEventListener('orientationChange', debounceResize);
clearTimeout(timeoutId);
w.removeEventListener('resize', debounce);
w.removeEventListener('orientationChange', debounce);
clearDebounce();
};
}, []);

Expand Down
5 changes: 5 additions & 0 deletions stories/atoms/Icon/Icon.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React, { useEffect, useState, useRef } from 'react';

// Helpers
import { args, propCategory } from '../../assets/storybook.globals';

// Components
import {
BusyLoader,
Empty,
Expand All @@ -10,6 +13,8 @@ import {
Toaster,
Icon as IconComponent
} from '../../../src';

// Styles
import './Icon.stories.scss';

const ICONS = 'https://sharedassets.namerandomness.com/betcore-icons/selection.json';
Expand Down

0 comments on commit eb62c5d

Please sign in to comment.