Skip to content

Commit

Permalink
Merge pull request #5659 from matuzalemsteles/dropdown-beta-1
Browse files Browse the repository at this point in the history
fix(@clayui/drop-down): adds throttle to handle contextual menu visibility
  • Loading branch information
matuzalemsteles authored Aug 11, 2023
2 parents 30607d0 + 4ed8469 commit 1702ee5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ module.exports = {
},
'./packages/clay-drop-down/src/': {
branches: 57,
functions: 52,
functions: 51,
lines: 70,
statements: 70,
statements: 69,
},
'./packages/clay-empty-state/src/': {
branches: 75,
Expand Down
27 changes: 18 additions & 9 deletions packages/clay-drop-down/src/DropDownWithItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
InternalDispatch,
Keys,
MouseSafeArea,
throttle,
useControlledState,
useNavigation,
} from '@clayui/shared';
import classNames from 'classnames';
import React, {useContext, useRef, useState} from 'react';
import React, {useCallback, useContext, useRef, useState} from 'react';
import warning from 'warning';

import Caption from './Caption';
Expand Down Expand Up @@ -305,6 +306,13 @@ const OFFSET_MAP = {
trtl: LEFT_OFFSET,
};

function offsetFn(points: any) {
return OFFSET_MAP[points.join('') as keyof typeof OFFSET_MAP] as [
number,
number
];
}

const Contextual = ({
items,
label,
Expand Down Expand Up @@ -338,6 +346,11 @@ const Contextual = ({
visible,
});

const setThrottleVisible = useCallback(
throttle((value: boolean) => setVisible(value), 100),
[]
);

return (
<ClayDropDown.Item
{...otherProps}
Expand Down Expand Up @@ -370,14 +383,14 @@ const Contextual = ({
if (!visible) {
keyboardRef.current = false;
timeoutHandleRef.current = setTimeout(
() => setVisible(true),
500
() => setThrottleVisible(true),
400
);
}
}}
onMouseLeave={() => {
keyboardRef.current = false;
setVisible(false);
setThrottleVisible(false);

clearTimeout(timeoutHandleRef.current);
timeoutHandleRef.current = null;
Expand All @@ -395,11 +408,7 @@ const Contextual = ({
alignmentPosition={8}
hasLeftSymbols={hasLeftSymbols}
hasRightSymbols={hasRightSymbols}
offsetFn={(points) =>
OFFSET_MAP[
points.join('') as keyof typeof OFFSET_MAP
] as [number, number]
}
offsetFn={offsetFn}
onActiveChange={setVisible}
onKeyDown={navigationProps.onKeyDown}
ref={menuElementRef}
Expand Down
1 change: 1 addition & 0 deletions packages/clay-shared/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {useHover} from './useHover';
export {useIsMobileDevice} from './useIsMobileDevice';
export {useIsFirstRender} from './useIsFirstRender';
export {isMac, isIPhone, isIPad, isIOS, isAppleDevice} from './platform';
export {throttle} from './throttle';
export type {AlignPoints} from './useOverlayPositon';
export type {IBaseProps as IPortalBaseProps} from './Portal';
export type {InternalDispatch} from './useControlledState';
19 changes: 19 additions & 0 deletions packages/clay-shared/src/throttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* SPDX-FileCopyrightText: © 2023 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/

export function throttle<T>(callback: Function, limit: number) {
var waiting = false;

return (...args: Array<T>) => {
if (!waiting) {
callback(...args);
waiting = true;

setTimeout(() => {
waiting = false;
}, limit);
}
};
}
17 changes: 2 additions & 15 deletions packages/clay-shared/src/useMousePosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@

import {useEffect, useState} from 'react';

type MousePosition = [number, number];

function throttle<T>(callback: Function, limit: number) {
var waiting = false;
import {throttle} from './throttle';

return (...args: Array<T>) => {
if (!waiting) {
callback(...args);
waiting = true;

setTimeout(() => {
waiting = false;
}, limit);
}
};
}
type MousePosition = [number, number];

/**
* Hook to get the current mouse position
Expand Down

0 comments on commit 1702ee5

Please sign in to comment.