Skip to content

Commit 371b852

Browse files
ralyodioclaude
andauthored
fix: emoji picker places itself on screen (flips down/up/right/left), keycap emoji images (0.9.1) (#309)
On /settings the picker kept the composer's "open upward" CSS inside a zero-height wrapper, so it opened above the button and ran under the sticky navbar: search and category tabs hidden, the grid looking empty. The reaction picker had the same problem near the top of the message list, clipped by the scroller. EmojiPicker is now position: fixed next to an anchor (anchorRef, or the element it is rendered in). placePopover() tries the preferred side, then the opposite, then right, then left, clamps inside the viewport below the navbar, and shrinks to the taller side when nothing fits whole. Settings anchors to the emoji button and prefers below; reactions end-align on your own messages; the composer still opens above. emojiKey() now pads codepoints to four hex digits like the OpenEmoji files, so #️⃣ 0️⃣-9️⃣ *️⃣ ©️ ®️ (14 emoji) stop 404ing in the picker and in messages. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent aa0b676 commit 371b852

8 files changed

Lines changed: 199 additions & 17 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "qryptchat-web",
33
"private": true,
4-
"version": "0.9.0",
4+
"version": "0.9.1",
55
"type": "module",
66
"bin": {
77
"qc": "./bin/qc.js",

‎src/lib/components/chat/EmojiPicker.jsx‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use client';
22

3-
import { useEffect, useMemo, useRef, useState } from 'react';
3+
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
44
import { 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. */
78
const GROUP_ICONS = {
@@ -25,26 +26,71 @@ export function Icon({ name, size = 18 }) {
2526
}
2627

2728
const 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); }

‎src/lib/components/chat/MessageItem.jsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default function MessageItem({ message, showAvatar = true, showTimestamp
159159
</div>
160160
)}
161161

162-
{pickerOpen && <EmojiPicker onPick={react} onClose={() => setPickerOpen(false)} />}
162+
{pickerOpen && <EmojiPicker onPick={react} onClose={() => setPickerOpen(false)} align={isOwn ? 'end' : 'start'} />}
163163
</div>
164164

165165
<div className="message-actions" role="toolbar" aria-label="Message actions">
@@ -234,8 +234,6 @@ export default function MessageItem({ message, showAvatar = true, showTimestamp
234234
.quick-reaction.mine { background: var(--color-bg-tertiary); }
235235
.quick-reaction img.openemoji { display: block; transition: transform .1s; }
236236
.quick-reaction:hover img.openemoji { transform: scale(1.2); }
237-
.message-content-wrapper .emoji-picker { left: 0; right: auto; }
238-
.message-wrapper.own .message-content-wrapper .emoji-picker { left: auto; right: 0; }
239237
@media (max-width: 640px) { .message-actions { position: absolute; bottom: 100%; z-index: 5; } .message-wrapper.own .message-actions { right: 0; } }
240238
`}</style>
241239
</div>

‎src/lib/components/settings/ProfileSettings.jsx‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useEffect, useState } from 'react';
3+
import { useEffect, useRef, useState } from 'react';
44
import { useAuthStore } from '@/lib/stores/auth.js';
55
import EmojiPicker from '@/lib/components/chat/EmojiPicker.jsx';
66
import OpenEmoji from '@/lib/components/OpenEmoji.jsx';
@@ -14,6 +14,7 @@ export default function ProfileSettings() {
1414
const [form, setForm] = useState({ emoji: '', pronouns: '', website: '', bio: '' });
1515
const [loaded, setLoaded] = useState(false);
1616
const [picking, setPicking] = useState(false);
17+
const emojiBtnRef = useRef(null);
1718
const [saving, setSaving] = useState(false);
1819
const [message, setMessage] = useState(null);
1920

@@ -59,17 +60,20 @@ export default function ProfileSettings() {
5960
<div className="ps-row">
6061
<label htmlFor="ps-emoji">Emoji</label>
6162
<div className="ps-emoji">
62-
<button type="button" id="ps-emoji" className="ps-emoji-btn emoji-btn" onClick={() => setPicking((p) => !p)} aria-label="Pick your emoji">
63+
<button type="button" id="ps-emoji" ref={emojiBtnRef} className="ps-emoji-btn emoji-btn" onClick={() => setPicking((p) => !p)} aria-label="Pick your emoji">
6364
{form.emoji ? <OpenEmoji char={form.emoji} size="1.6rem" /> : '+'}
6465
</button>
6566
{form.emoji && (
6667
<button type="button" className="btn btn-secondary btn-sm" onClick={() => setForm((f) => ({ ...f, emoji: '' }))}>Clear</button>
6768
)}
6869
<span className="ps-hint">Shown next to your name.</span>
6970
{picking && (
70-
<div className="ps-picker">
71-
<EmojiPicker onPick={(char) => { setForm((f) => ({ ...f, emoji: char })); setPicking(false); }} onClose={() => setPicking(false)} />
72-
</div>
71+
<EmojiPicker
72+
anchorRef={emojiBtnRef}
73+
prefer="bottom"
74+
onPick={(char) => { setForm((f) => ({ ...f, emoji: char })); setPicking(false); }}
75+
onClose={() => setPicking(false)}
76+
/>
7377
)}
7478
</div>
7579
</div>
@@ -104,7 +108,6 @@ export default function ProfileSettings() {
104108
.ps-input { width: 100%; padding: .5rem .75rem; border: 1px solid var(--color-border-primary); border-radius: .375rem; background: var(--color-bg-primary); color: var(--color-text-primary); font-size: .875rem; font-family: inherit; }
105109
.ps-emoji { position: relative; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
106110
.ps-emoji-btn { width: 2.75rem; height: 2.75rem; font-size: 1.6rem; line-height: 1; border: 1px solid var(--color-border-primary); border-radius: .5rem; background: var(--color-bg-primary); cursor: pointer; }
107-
.ps-picker { position: absolute; top: 3rem; left: 0; z-index: 20; }
108111
.ps-hint { font-size: .8rem; color: var(--color-text-secondary); }
109112
.ps-alert { padding: .5rem .75rem; border-radius: .375rem; font-size: .85rem; }
110113
.ps-ok { background: rgba(16,185,129,.1); color: #065f46; }

‎src/lib/emoji/openemoji.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ const PICTOGRAPHIC = /\p{Extended_Pictographic}|\p{Regional_Indicator}|⃣/u;
1414
const segmenter =
1515
typeof Intl !== 'undefined' && Intl.Segmenter ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null;
1616

17-
/** OpenEmoji key of a character sequence: its codepoints, lowercase hex, hyphen-joined. */
17+
/**
18+
* OpenEmoji key of a character sequence: its codepoints, lowercase hex padded
19+
* to four digits ("0023-fe0f-20e3" for #️⃣, "00a9-fe0f" for ©️), hyphen-joined.
20+
*/
1821
export function emojiKey(char) {
19-
return [...char].map((c) => c.codePointAt(0).toString(16)).join('-');
22+
return [...char].map((c) => c.codePointAt(0).toString(16).padStart(4, '0')).join('-');
2023
}
2124

2225
/**

‎src/lib/emoji/openemoji.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { readFileSync, existsSync } from 'node:fs';
3+
import { emojiKey, artworkKey } from './openemoji.js';
4+
5+
describe('emojiKey', () => {
6+
it('pads codepoints to four hex digits like the OpenEmoji files', () => {
7+
expect(emojiKey('😀')).toBe('1f600');
8+
expect(emojiKey('#️⃣')).toBe('0023-fe0f-20e3');
9+
expect(emojiKey('©️')).toBe('00a9-fe0f');
10+
expect(artworkKey('©')).toBe('00a9-fe0f');
11+
});
12+
13+
it('has an image for every emoji the picker lists', () => {
14+
const index = JSON.parse(readFileSync('public/openemoji/index.json', 'utf8'));
15+
const missing = index.emoji.filter(([char]) => !existsSync(`public/openemoji/64/${emojiKey(char)}.webp`));
16+
expect(missing.map(([c]) => c)).toEqual([]);
17+
});
18+
});

‎src/lib/emoji/placement.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Where to put a popover (the emoji picker) next to its anchor so it stays on
3+
* screen: try the preferred side, then the opposite one, then right and left.
4+
* Coordinates are viewport pixels, ready for position: fixed.
5+
*
6+
* anchor and bounds are { top, right, bottom, left }; size is { width, height }.
7+
* align 'start' lines the popover up with the anchor's left (or top) edge,
8+
* 'end' with its right (or bottom) edge; either way it is then nudged back
9+
* inside bounds. When no side has room for the full height, it goes on the
10+
* taller of above/below and maxHeight says how much it may use.
11+
*/
12+
const OPPOSITE = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' };
13+
14+
export function placePopover({ anchor, size, bounds, prefer = 'bottom', align = 'start', gap = 8 }) {
15+
const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, Math.max(lo, hi)));
16+
const room = {
17+
top: anchor.top - gap - bounds.top,
18+
bottom: bounds.bottom - anchor.bottom - gap,
19+
left: anchor.left - gap - bounds.left,
20+
right: bounds.right - anchor.right - gap,
21+
};
22+
const fits = (side) =>
23+
side === 'top' || side === 'bottom'
24+
? room[side] >= size.height && bounds.right - bounds.left >= size.width
25+
: room[side] >= size.width && bounds.bottom - bounds.top >= size.height;
26+
27+
const horizontal = (width) =>
28+
clamp(align === 'end' ? anchor.right - width : anchor.left, bounds.left, bounds.right - width);
29+
const vertical = (height) =>
30+
clamp(align === 'end' ? anchor.bottom - height : anchor.top, bounds.top, bounds.bottom - height);
31+
32+
const order = [prefer, OPPOSITE[prefer], 'right', 'left'].filter((s, i, a) => a.indexOf(s) === i);
33+
const side = order.find(fits);
34+
35+
if (side === 'top') return { side, top: anchor.top - gap - size.height, left: horizontal(size.width), maxHeight: null };
36+
if (side === 'bottom') return { side, top: anchor.bottom + gap, left: horizontal(size.width), maxHeight: null };
37+
if (side === 'right') return { side, top: vertical(size.height), left: anchor.right + gap, maxHeight: null };
38+
if (side === 'left') return { side, top: vertical(size.height), left: anchor.left - gap - size.width, maxHeight: null };
39+
40+
// Nothing fits whole (a short window): use the taller of above and below.
41+
const up = room.top > room.bottom;
42+
const maxHeight = Math.max(0, up ? room.top : room.bottom);
43+
const height = Math.min(size.height, maxHeight);
44+
return {
45+
side: up ? 'top' : 'bottom',
46+
top: up ? anchor.top - gap - height : anchor.bottom + gap,
47+
left: horizontal(size.width),
48+
maxHeight,
49+
};
50+
}

‎src/lib/emoji/placement.test.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { placePopover } from './placement.js';
3+
4+
const bounds = { top: 64, left: 8, right: 1272, bottom: 792 }; // below a 64px navbar
5+
const size = { width: 352, height: 380 };
6+
const at = (top, left, h = 44, w = 44) => ({ top, left, bottom: top + h, right: left + w });
7+
8+
describe('placePopover', () => {
9+
it('opens below when preferred and there is room', () => {
10+
const p = placePopover({ anchor: at(277, 81), size, bounds, prefer: 'bottom' });
11+
expect(p).toMatchObject({ side: 'bottom', top: 277 + 44 + 8, left: 81, maxHeight: null });
12+
});
13+
14+
it('flips below when above would run under the navbar (the settings bug)', () => {
15+
const p = placePopover({ anchor: at(277, 81), size, bounds, prefer: 'top' });
16+
expect(p.side).toBe('bottom');
17+
expect(p.top).toBeGreaterThanOrEqual(bounds.top);
18+
});
19+
20+
it('flips above near the bottom of the window (the composer)', () => {
21+
const p = placePopover({ anchor: at(700, 300, 60, 600), size, bounds, prefer: 'bottom' });
22+
expect(p).toMatchObject({ side: 'top', top: 700 - 8 - 380 });
23+
});
24+
25+
it('goes to the side when neither above nor below has room', () => {
26+
const p = placePopover({ anchor: at(300, 100, 300, 200), size, bounds, prefer: 'bottom' });
27+
expect(p.side).toBe('right');
28+
expect(p.left).toBe(100 + 200 + 8);
29+
expect(p.top).toBeGreaterThanOrEqual(bounds.top);
30+
expect(p.top + size.height).toBeLessThanOrEqual(bounds.bottom);
31+
});
32+
33+
it('uses the left side when the anchor hugs the right edge', () => {
34+
const p = placePopover({ anchor: at(300, 1000, 300, 260), size, bounds, prefer: 'bottom' });
35+
expect(p.side).toBe('left');
36+
expect(p.left).toBe(1000 - 8 - 352);
37+
});
38+
39+
it('end-aligns and clamps horizontally inside the window', () => {
40+
const p = placePopover({ anchor: at(100, 1200, 30, 60), size, bounds, prefer: 'bottom', align: 'end' });
41+
expect(p.left + size.width).toBeLessThanOrEqual(bounds.right);
42+
const q = placePopover({ anchor: at(100, 0, 30, 60), size, bounds, prefer: 'bottom', align: 'end' });
43+
expect(q.left).toBe(bounds.left);
44+
});
45+
46+
it('shrinks into the taller space when nothing fits whole', () => {
47+
const short = { top: 0, left: 0, right: 360, bottom: 500 };
48+
const p = placePopover({ anchor: at(150, 4, 44, 352), size, bounds: short, prefer: 'top' });
49+
expect(p.side).toBe('bottom');
50+
expect(p.maxHeight).toBe(500 - 194 - 8);
51+
expect(p.top).toBe(194 + 8);
52+
});
53+
});

0 commit comments

Comments
 (0)