Fix useHorizontalScroll to accept ref and not element

This commit is contained in:
Alexander Zinchuk 2023-02-08 00:43:32 +01:00
parent d40a2a6240
commit ba63b8c2c6
10 changed files with 31 additions and 22 deletions

View File

@ -70,8 +70,6 @@ const ChatResults: FC<OwnProps & StateProps> = ({
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
const chatSelectionRef = useRef<HTMLDivElement>(null); const chatSelectionRef = useRef<HTMLDivElement>(null);
useHorizontalScroll(chatSelectionRef.current, undefined, true);
const lang = useLang(); const lang = useLang();
const { isMobile } = useAppLayout(); const { isMobile } = useAppLayout();
@ -132,6 +130,8 @@ const ChatResults: FC<OwnProps & StateProps> = ({
]; ];
}, [searchQuery, currentUserId, localContactIds, lang, localChatIds, localUserIds, chatsById]); }, [searchQuery, currentUserId, localContactIds, lang, localChatIds, localUserIds, chatsById]);
useHorizontalScroll(chatSelectionRef, !localResults.length, true);
const globalResults = useMemo(() => { const globalResults = useMemo(() => {
if (!searchQuery || searchQuery.length < MIN_QUERY_LENGTH_FOR_GLOBAL_SEARCH || !globalChatIds || !globalUserIds) { if (!searchQuery || searchQuery.length < MIN_QUERY_LENGTH_FOR_GLOBAL_SEARCH || !globalChatIds || !globalUserIds) {
return MEMO_EMPTY_ARRAY; return MEMO_EMPTY_ARRAY;

View File

@ -58,7 +58,7 @@ const RecentContacts: FC<OwnProps & StateProps> = ({
}); });
}, [loadTopUsers]); }, [loadTopUsers]);
useHorizontalScroll(topUsersRef.current, !topUserIds); useHorizontalScroll(topUsersRef, !topUserIds);
const handleClick = useCallback((id: string) => { const handleClick = useCallback((id: string) => {
openChat({ id, shouldReplaceHistory: true }); openChat({ id, shouldReplaceHistory: true });

View File

@ -171,7 +171,10 @@ const CustomEmojiPicker: FC<OwnProps & StateProps> = ({
&& allSets.filter((set) => set.stickers?.length).length === 0 && allSets.filter((set) => set.stickers?.length).length === 0
), [allSets, areAddedLoaded]); ), [allSets, areAddedLoaded]);
useHorizontalScroll(headerRef.current); const canRenderContents = useAsyncRendering([], SLIDE_TRANSITION_DURATION);
const shouldRenderContents = areAddedLoaded && canRenderContents && !noPopulatedSets;
useHorizontalScroll(headerRef, !shouldRenderContents);
// Scroll container and header when active set changes // Scroll container and header when active set changes
useEffect(() => { useEffect(() => {
@ -199,8 +202,6 @@ const CustomEmojiPicker: FC<OwnProps & StateProps> = ({
onCustomEmojiSelect(emoji); onCustomEmojiSelect(emoji);
}, [onCustomEmojiSelect]); }, [onCustomEmojiSelect]);
const canRenderContents = useAsyncRendering([], SLIDE_TRANSITION_DURATION);
function renderCover(stickerSet: StickerSetOrRecent, index: number) { function renderCover(stickerSet: StickerSetOrRecent, index: number) {
const firstSticker = stickerSet.stickers?.[0]; const firstSticker = stickerSet.stickers?.[0];
const buttonClassName = buildClassName( const buttonClassName = buildClassName(
@ -263,7 +264,7 @@ const CustomEmojiPicker: FC<OwnProps & StateProps> = ({
const fullClassName = buildClassName('StickerPicker', 'CustomEmojiPicker', className); const fullClassName = buildClassName('StickerPicker', 'CustomEmojiPicker', className);
if (!areAddedLoaded || !canRenderContents || noPopulatedSets) { if (!shouldRenderContents) {
return ( return (
<div className={fullClassName}> <div className={fullClassName}>
{noPopulatedSets ? ( {noPopulatedSets ? (

View File

@ -53,7 +53,7 @@ const CustomEmojiTooltip: FC<OwnProps & StateProps> = ({
const prevStickers = usePrevious(customEmoji, true); const prevStickers = usePrevious(customEmoji, true);
const displayedStickers = customEmoji || prevStickers; const displayedStickers = customEmoji || prevStickers;
useHorizontalScroll(containerRef.current); useHorizontalScroll(containerRef);
const { const {
observe: observeIntersection, observe: observeIntersection,

View File

@ -105,7 +105,10 @@ const EmojiPicker: FC<OwnProps & StateProps> = ({
setActiveCategoryIndex(intersectingWithIndexes[Math.floor(intersectingWithIndexes.length / 2)].index); setActiveCategoryIndex(intersectingWithIndexes[Math.floor(intersectingWithIndexes.length / 2)].index);
}); });
useHorizontalScroll(headerRef.current, !isMobile); const canRenderContents = useAsyncRendering([], MENU_TRANSITION_DURATION);
const shouldRenderContent = emojis && canRenderContents;
useHorizontalScroll(headerRef, !(isMobile && shouldRenderContent));
// Scroll header when active set updates // Scroll header when active set updates
useEffect(() => { useEffect(() => {
@ -169,8 +172,6 @@ const EmojiPicker: FC<OwnProps & StateProps> = ({
onEmojiSelect(emoji, name); onEmojiSelect(emoji, name);
}, [onEmojiSelect]); }, [onEmojiSelect]);
const canRenderContents = useAsyncRendering([], MENU_TRANSITION_DURATION);
function renderCategoryButton(category: EmojiCategoryData, index: number) { function renderCategoryButton(category: EmojiCategoryData, index: number) {
const icon = ICONS_BY_CATEGORY[category.id]; const icon = ICONS_BY_CATEGORY[category.id];
@ -191,7 +192,7 @@ const EmojiPicker: FC<OwnProps & StateProps> = ({
const containerClassName = buildClassName('EmojiPicker', className); const containerClassName = buildClassName('EmojiPicker', className);
if (!emojis || !canRenderContents) { if (!shouldRenderContent) {
return ( return (
<div className={containerClassName}> <div className={containerClassName}>
<Loading /> <Loading />

View File

@ -81,7 +81,7 @@ const EmojiTooltip: FC<OwnProps> = ({
emojis.length ? [...customEmojis, ...emojis] : undefined, CLOSE_DURATION, emojis.length ? [...customEmojis, ...emojis] : undefined, CLOSE_DURATION,
) || []; ) || [];
useHorizontalScroll(containerRef.current); useHorizontalScroll(containerRef);
const handleSelectEmoji = useCallback((emoji: Emoji) => { const handleSelectEmoji = useCallback((emoji: Emoji) => {
onEmojiSelect(emoji.native); onEmojiSelect(emoji.native);

View File

@ -214,7 +214,10 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
sendMessageAction({ type: 'chooseSticker' }); sendMessageAction({ type: 'chooseSticker' });
}, [canSendStickers, loadAndPlay, loadRecentStickers, sendMessageAction]); }, [canSendStickers, loadAndPlay, loadRecentStickers, sendMessageAction]);
useHorizontalScroll(headerRef.current); const canRenderContents = useAsyncRendering([], SLIDE_TRANSITION_DURATION);
const shouldRenderContents = areAddedLoaded && canRenderContents && !noPopulatedSets && canSendStickers;
useHorizontalScroll(headerRef, !shouldRenderContents);
// Scroll container and header when active set changes // Scroll container and header when active set changes
useEffect(() => { useEffect(() => {
@ -260,8 +263,6 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
removeRecentSticker({ sticker }); removeRecentSticker({ sticker });
}, [removeRecentSticker]); }, [removeRecentSticker]);
const canRenderContents = useAsyncRendering([], SLIDE_TRANSITION_DURATION);
function renderCover(stickerSet: StickerSetOrRecent, index: number) { function renderCover(stickerSet: StickerSetOrRecent, index: number) {
const firstSticker = stickerSet.stickers?.[0]; const firstSticker = stickerSet.stickers?.[0];
const buttonClassName = buildClassName( const buttonClassName = buildClassName(
@ -329,7 +330,7 @@ const StickerPicker: FC<OwnProps & StateProps> = ({
const fullClassName = buildClassName('StickerPicker', className); const fullClassName = buildClassName('StickerPicker', className);
if (!areAddedLoaded || !canRenderContents || noPopulatedSets || !canSendStickers) { if (!shouldRenderContents) {
return ( return (
<div className={fullClassName}> <div className={fullClassName}>
{!canSendStickers ? ( {!canSendStickers ? (

View File

@ -45,7 +45,7 @@ const ReactionSelector: FC<OwnProps> = ({
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
const itemsScrollRef = useRef<HTMLDivElement>(null); const itemsScrollRef = useRef<HTMLDivElement>(null);
const [isHorizontalScrollEnabled, enableHorizontalScroll] = useFlag(false); const [isHorizontalScrollEnabled, enableHorizontalScroll] = useFlag(false);
useHorizontalScroll(itemsScrollRef.current, !isHorizontalScrollEnabled); useHorizontalScroll(itemsScrollRef, !isHorizontalScrollEnabled);
useLayoutEffect(() => { useLayoutEffect(() => {
enableHorizontalScroll(); enableHorizontalScroll();

View File

@ -40,7 +40,7 @@ const TabList: FC<OwnProps> = ({
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const previousActiveTab = usePrevious(activeTab); const previousActiveTab = usePrevious(activeTab);
useHorizontalScroll(containerRef.current, undefined, true); useHorizontalScroll(containerRef, undefined, true);
// Scroll container to place active tab in the center // Scroll container to place active tab in the center
useEffect(() => { useEffect(() => {

View File

@ -1,11 +1,17 @@
import { useEffect } from '../lib/teact/teact'; import { useEffect } from '../lib/teact/teact';
const useHorizontalScroll = (container: HTMLElement | null, isDisabled?: boolean, shouldPreventDefault = false) => { const useHorizontalScroll = (
containerRef: React.RefObject<HTMLDivElement>,
isDisabled?: boolean,
shouldPreventDefault = false,
) => {
useEffect(() => { useEffect(() => {
if (!container || isDisabled) { if (isDisabled) {
return undefined; return undefined;
} }
const container = containerRef.current!;
function handleScroll(e: WheelEvent) { function handleScroll(e: WheelEvent) {
// Ignore horizontal scroll and let it work natively (e.g. on touchpad) // Ignore horizontal scroll and let it work natively (e.g. on touchpad)
if (!e.deltaX) { if (!e.deltaX) {
@ -19,7 +25,7 @@ const useHorizontalScroll = (container: HTMLElement | null, isDisabled?: boolean
return () => { return () => {
container.removeEventListener('wheel', handleScroll); container.removeEventListener('wheel', handleScroll);
}; };
}, [container, isDisabled, shouldPreventDefault]); }, [containerRef, isDisabled, shouldPreventDefault]);
}; };
export default useHorizontalScroll; export default useHorizontalScroll;