@@ -2,6 +2,10 @@ import * as React from "react";
22
33import type { EmojiSuggestion } from "@/features/messages/lib/useEmojiAutocomplete" ;
44import { cn } from "@/shared/lib/cn" ;
5+ import {
6+ type ListVirtualizer ,
7+ VirtualizedList ,
8+ } from "@/shared/ui/VirtualizedList" ;
59import {
610 POPOVER_CUSTOM_ENTER_MOTION_CLASS ,
711 POPOVER_SHADOW_STYLE ,
@@ -21,15 +25,21 @@ export const EmojiAutocomplete = React.memo(function EmojiAutocomplete({
2125 onSelect,
2226 position = "above" ,
2327} : EmojiAutocompleteProps ) {
24- const listRef = React . useRef < HTMLDivElement > ( null ) ;
28+ const listVirtualizerRef = React . useRef < ListVirtualizer | null > ( null ) ;
2529
2630 React . useEffect ( ( ) => {
27- const activeItem = listRef . current ?. children [ selectedIndex ] as
28- | HTMLElement
29- | undefined ;
30- activeItem ?. scrollIntoView ( { block : "nearest" } ) ;
31+ listVirtualizerRef . current ?. scrollToIndex ( selectedIndex , {
32+ align : "auto" ,
33+ } ) ;
3134 } , [ selectedIndex ] ) ;
3235
36+ const handleVirtualizer = React . useCallback (
37+ ( virtualizer : ListVirtualizer ) => {
38+ listVirtualizerRef . current = virtualizer ;
39+ } ,
40+ [ ] ,
41+ ) ;
42+
3343 if ( suggestions . length === 0 ) {
3444 return null ;
3545 }
@@ -43,48 +53,55 @@ export const EmojiAutocomplete = React.memo(function EmojiAutocomplete({
4353 >
4454 < div
4555 className = { cn (
46- "max-h-48 overflow-y-auto rounded-xl p-1" ,
56+ "rounded-xl p-1" ,
4757 POPOVER_CUSTOM_ENTER_MOTION_CLASS ,
4858 position === "below"
4959 ? "origin-top slide-in-from-top-1"
5060 : "origin-bottom slide-in-from-bottom-1" ,
5161 POPOVER_SURFACE_CLASS ,
5262 ) }
5363 data-testid = "emoji-autocomplete"
54- ref = { listRef }
5564 style = { POPOVER_SHADOW_STYLE }
5665 >
57- { suggestions . map ( ( suggestion , index ) => (
58- < button
59- className = { cn (
60- "flex w-full cursor-pointer items-center gap-2 rounded-lg px-3 py-1.5 text-left text-sm" ,
61- index === selectedIndex
62- ? "bg-accent text-accent-foreground"
63- : "text-popover-foreground hover:bg-accent/50" ,
64- ) }
65- key = { suggestion . id }
66- onMouseDown = { ( event ) => {
67- event . preventDefault ( ) ;
68- onSelect ( suggestion ) ;
69- } }
70- tabIndex = { - 1 }
71- type = "button"
72- >
73- { suggestion . url ? (
74- < img
75- alt = { `:${ suggestion . id } :` }
76- src = { suggestion . url }
77- className = "h-5 w-5 object-contain"
78- draggable = { false }
79- />
80- ) : (
81- < span className = "text-lg leading-none" > { suggestion . native } </ span >
82- ) }
83- < span className = "truncate text-muted-foreground" >
84- :{ suggestion . id } :
85- </ span >
86- </ button >
87- ) ) }
66+ < VirtualizedList
67+ className = "max-h-48"
68+ estimateSize = { 36 }
69+ getItemKey = { ( suggestion ) => suggestion . id }
70+ items = { suggestions }
71+ onVirtualizer = { handleVirtualizer }
72+ renderItem = { ( suggestion , index ) => (
73+ < button
74+ className = { cn (
75+ "flex w-full cursor-pointer items-center gap-2 rounded-lg px-3 py-1.5 text-left text-sm" ,
76+ index === selectedIndex
77+ ? "bg-accent text-accent-foreground"
78+ : "text-popover-foreground hover:bg-accent/50" ,
79+ ) }
80+ onMouseDown = { ( event ) => {
81+ event . preventDefault ( ) ;
82+ onSelect ( suggestion ) ;
83+ } }
84+ tabIndex = { - 1 }
85+ type = "button"
86+ >
87+ { suggestion . url ? (
88+ < img
89+ alt = { `:${ suggestion . id } :` }
90+ src = { suggestion . url }
91+ className = "h-5 w-5 object-contain"
92+ draggable = { false }
93+ />
94+ ) : (
95+ < span className = "text-lg leading-none" >
96+ { suggestion . native }
97+ </ span >
98+ ) }
99+ < span className = "truncate text-muted-foreground" >
100+ :{ suggestion . id } :
101+ </ span >
102+ </ button >
103+ ) }
104+ />
88105 </ div >
89106 </ div >
90107 ) ;
0 commit comments