From 2c91ca86aa0782bb7477c12325407c34e858b3fe Mon Sep 17 00:00:00 2001 From: zubiden <19638254+zubiden@users.noreply.github.com> Date: Fri, 6 Sep 2024 15:43:39 +0200 Subject: [PATCH] Reaction Picker: Fix stuck state (#4946) --- src/components/middle/composer/SymbolMenu.tsx | 6 +- src/components/ui/Menu.tsx | 5 +- src/hooks/useMediaTransition.ts | 4 +- src/hooks/useShowTransition.ts | 77 ++++++++++++++----- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/components/middle/composer/SymbolMenu.tsx b/src/components/middle/composer/SymbolMenu.tsx index 0748a3432..6118e234d 100644 --- a/src/components/middle/composer/SymbolMenu.tsx +++ b/src/components/middle/composer/SymbolMenu.tsx @@ -22,7 +22,7 @@ import useShowTransitionDeprecated from '../../../hooks/useShowTransitionDepreca import CustomEmojiPicker from '../../common/CustomEmojiPicker'; import Button from '../../ui/Button'; -import { UnfreezableMenu } from '../../ui/Menu'; +import Menu from '../../ui/Menu'; import Portal from '../../ui/Portal'; import Transition from '../../ui/Transition'; import EmojiPicker from './EmojiPicker'; @@ -316,7 +316,7 @@ const SymbolMenu: FC = ({ } return ( - = ({ })} > {content} - + ); }; diff --git a/src/components/ui/Menu.tsx b/src/components/ui/Menu.tsx index 4de6b9764..7b1e37d0b 100644 --- a/src/components/ui/Menu.tsx +++ b/src/components/ui/Menu.tsx @@ -6,7 +6,6 @@ import type { MenuPositionOptions } from '../../hooks/useMenuPosition'; import buildClassName from '../../util/buildClassName'; import captureEscKeyListener from '../../util/captureEscKeyListener'; -import freezeWhenClosed from '../../util/hoc/freezeWhenClosed'; import { IS_BACKDROP_BLUR_SUPPORTED } from '../../util/windowEnvironment'; import { preventMessageInputBlurWithBubbling } from '../middle/helpers/preventMessageInputBlur'; @@ -173,6 +172,4 @@ const Menu: FC = ({ return menu; }; -export const UnfreezableMenu = memo(Menu); - -export default memo(freezeWhenClosed(Menu)); +export default memo(Menu); diff --git a/src/hooks/useMediaTransition.ts b/src/hooks/useMediaTransition.ts index 045a25e5f..8349f273f 100644 --- a/src/hooks/useMediaTransition.ts +++ b/src/hooks/useMediaTransition.ts @@ -1,8 +1,8 @@ -import useShowTransition from './useShowTransition'; +import useShowTransition, { type HookParams } from './useShowTransition'; export default function useMediaTransition( mediaData?: unknown, - options?: Partial>[0]>, + options?: Partial>, ) { const isMediaReady = Boolean(mediaData); diff --git a/src/hooks/useShowTransition.ts b/src/hooks/useShowTransition.ts index 938652eb1..c68b77323 100644 --- a/src/hooks/useShowTransition.ts +++ b/src/hooks/useShowTransition.ts @@ -2,6 +2,8 @@ import type { RefObject } from 'react'; import { useLayoutEffect, useRef, useSignal } from '../lib/teact/teact'; import { addExtraClass, toggleExtraClass } from '../lib/teact/teact-dom'; +import type { Signal } from '../util/signals'; + import { requestMeasure } from '../lib/fasterdom/fasterdom'; import useDerivedSignal from './useDerivedSignal'; import useDerivedState from './useDerivedState'; @@ -11,24 +13,7 @@ import useSyncEffectWithPrevDeps from './useSyncEffectWithPrevDeps'; const CLOSE_DURATION = 350; -type State = - 'closed' - | 'scheduled-open' - | 'open' - | 'closing'; - -export default function useShowTransition({ - isOpen, - ref, - noMountTransition = false, - noOpenTransition = false, - noCloseTransition = false, - closeDuration = CLOSE_DURATION, - className = 'fast', - prefix = '', - onCloseAnimationEnd, - withShouldRender, -}: { +type BaseHookParams = { isOpen: boolean | undefined; ref?: RefObject; noMountTransition?: boolean; @@ -37,9 +22,55 @@ export default function useShowTransition = BaseHookParams & { + withShouldRender?: never; +}; + +type HookParamsWithShouldRender = BaseHookParams & { + withShouldRender: true; +}; + +type HookResult = { + ref: RefObject; + getIsClosing: Signal; +}; + +type HookResultWithShouldRender = HookResult & { + shouldRender: boolean; +}; + +type State = + 'closed' + | 'scheduled-open' + | 'open' + | 'closing'; + +export default function useShowTransition( + params: HookParams +): HookResult; +export default function useShowTransition( + params: HookParamsWithShouldRender +): HookResultWithShouldRender; +export default function useShowTransition( + params: HookParams | HookParamsWithShouldRender, +): HookResult | HookResultWithShouldRender { + const { + isOpen, + noMountTransition = false, + noOpenTransition = false, + noCloseTransition = false, + closeDuration = CLOSE_DURATION, + className = 'fast', + prefix = '', + onCloseAnimationEnd, + } = params; + let ref = params.ref; + + const withShouldRender = 'withShouldRender' in params && params.withShouldRender; + // eslint-disable-next-line no-null/no-null const localRef = useRef(null); ref ||= localRef; @@ -106,5 +137,9 @@ export default function useShowTransition getState() === 'closing', [getState]); - return { ref, shouldRender, getIsClosing }; + if (withShouldRender) { + return { ref, shouldRender, getIsClosing }; + } + + return { ref, getIsClosing }; }