11'use client' ;
22
3- import { useEffect , useMemo , useRef , useState } from 'react' ;
3+ import { useEffect , useLayoutEffect , useMemo , useRef , useState } from 'react' ;
44import { loadEmojiIndex , searchEmoji , readRecent , pushRecent , emojiSrc , OPENICON_SPRITE } from '@/lib/emoji/openemoji.js' ;
5+ import { placePopover } from '@/lib/emoji/placement.js' ;
56
67/** OpenIcon glyph per OpenEmoji group, in the set's own group order. */
78const GROUP_ICONS = {
@@ -25,26 +26,71 @@ export function Icon({ name, size = 18 }) {
2526}
2627
2728const RECENT = 'recent' ;
29+ const EDGE = 8 ;
30+
31+ /** The visible viewport, less a sticky navbar across the top. */
32+ function viewportBounds ( ) {
33+ let top = EDGE ;
34+ const nav = document . querySelector ( '.navbar' ) ;
35+ if ( nav ) {
36+ const pos = getComputedStyle ( nav ) . position ;
37+ const r = nav . getBoundingClientRect ( ) ;
38+ if ( ( pos === 'sticky' || pos === 'fixed' ) && r . bottom > 0 ) top = Math . max ( top , r . bottom + EDGE ) ;
39+ }
40+ return { top, left : EDGE , right : window . innerWidth - EDGE , bottom : window . innerHeight - EDGE } ;
41+ }
2842
2943/**
3044 * The OpenEmoji picker. It only ever hands back a Unicode character through
3145 * onPick: what the message stores (and encrypts) is plain text.
46+ *
47+ * It is position: fixed next to its anchor (anchorRef, or else the element it
48+ * is rendered in), on the `prefer` side when that fits and flipped to
49+ * below/above/right/left when it does not, so a scroll container or the navbar
50+ * never cuts it off. align 'end' lines it up with the anchor's right edge.
3251 */
33- export default function EmojiPicker ( { onPick, onClose } ) {
52+ export default function EmojiPicker ( { onPick, onClose, anchorRef , prefer = 'top' , align = 'start' } ) {
3453 const [ index , setIndex ] = useState ( null ) ;
3554 const [ error , setError ] = useState ( '' ) ;
3655 const [ query , setQuery ] = useState ( '' ) ;
3756 const [ recent , setRecent ] = useState ( ( ) => readRecent ( ) ) ;
3857 const [ tab , setTab ] = useState ( ( ) => ( readRecent ( ) . length ? RECENT : 0 ) ) ;
3958 const rootRef = useRef ( null ) ;
4059 const searchRef = useRef ( null ) ;
60+ const [ place , setPlace ] = useState ( null ) ;
61+
62+ useLayoutEffect ( ( ) => {
63+ const root = rootRef . current ;
64+ if ( ! root ) return undefined ;
65+ function update ( ) {
66+ const anchor = anchorRef ?. current || root . parentElement ;
67+ if ( ! anchor ) return ;
68+ const next = placePopover ( {
69+ anchor : anchor . getBoundingClientRect ( ) ,
70+ size : { width : root . offsetWidth , height : 380 } ,
71+ bounds : viewportBounds ( ) ,
72+ prefer,
73+ align,
74+ } ) ;
75+ setPlace ( ( prev ) =>
76+ prev && prev . top === next . top && prev . left === next . left && prev . maxHeight === next . maxHeight ? prev : next ,
77+ ) ;
78+ }
79+ update ( ) ;
80+ window . addEventListener ( 'resize' , update ) ;
81+ window . addEventListener ( 'scroll' , update , true ) ;
82+ return ( ) => {
83+ window . removeEventListener ( 'resize' , update ) ;
84+ window . removeEventListener ( 'scroll' , update , true ) ;
85+ } ;
86+ } , [ anchorRef , prefer , align ] ) ;
4187
4288 useEffect ( ( ) => {
4389 let live = true ;
4490 loadEmojiIndex ( )
4591 . then ( ( i ) => live && setIndex ( i ) )
4692 . catch ( ( err ) => live && setError ( err . message ) ) ;
47- searchRef . current ?. focus ( ) ;
93+ searchRef . current ?. focus ( { preventScroll : true } ) ;
4894 return ( ) => {
4995 live = false ;
5096 } ;
@@ -80,7 +126,18 @@ export default function EmojiPicker({ onPick, onClose }) {
80126 }
81127
82128 return (
83- < div className = "emoji-picker" ref = { rootRef } role = "dialog" aria-label = "Emoji" >
129+ < div
130+ className = "emoji-picker"
131+ ref = { rootRef }
132+ role = "dialog"
133+ aria-label = "Emoji"
134+ data-side = { place ?. side }
135+ style = {
136+ place
137+ ? { top : place . top , left : place . left , height : place . maxHeight == null ? undefined : Math . min ( 380 , place . maxHeight ) }
138+ : { visibility : 'hidden' , top : 0 , left : 0 }
139+ }
140+ >
84141 < div className = "emoji-search" >
85142 < Icon name = "search" size = { 16 } />
86143 < input
@@ -122,7 +179,7 @@ export default function EmojiPicker({ onPick, onClose }) {
122179 < div className = "emoji-credit" > OpenEmoji by Profullstack, Inc. (CC BY 4.0)</ div >
123180
124181 < style > { `
125- .emoji-picker { position: absolute; bottom: calc(100% + .5rem); left: .75rem; width: min(352px, calc(100vw - 2rem)); height: 380px; display: flex; flex-direction: column; background: var(--color-bg-primary); border: 1px solid var(--color-border-primary); border-radius: .75rem; box-shadow: 0 10px 30px rgba(0,0,0,.25); z-index: 20 ; overflow: hidden; }
182+ .emoji-picker { position: fixed; width: min(352px, calc(100vw - 2rem)); height: 380px; display: flex; flex-direction: column; background: var(--color-bg-primary); border: 1px solid var(--color-border-primary); border-radius: .75rem; box-shadow: 0 10px 30px rgba(0,0,0,.25); z-index: 45 ; overflow: hidden; }
126183 .emoji-search { display: flex; align-items: center; gap: .4rem; margin: .5rem; padding: .35rem .6rem; background: var(--color-bg-secondary); border: 1px solid var(--color-border-primary); border-radius: .5rem; color: var(--color-text-muted); }
127184 .emoji-search input { flex: 1; border: none; background: transparent; outline: none; color: var(--color-text-primary); font: inherit; font-size: .875rem; }
128185 .emoji-tabs { display: flex; justify-content: space-between; padding: 0 .35rem; border-bottom: 1px solid var(--color-border-primary); }
0 commit comments