From f0de1de9628ef60a6c57ed4e8fd4f17de644bf7e Mon Sep 17 00:00:00 2001 From: Houssem Eddine BENABDALLAH Date: Fri, 16 Oct 2026 17:12:50 +0200 Subject: [PATCH 1/7] feat: pick emoji from a button in the commit and reply composers --- resources/js/components/commit-composer.tsx | 15 +- resources/js/components/commit-thread.tsx | 14 ++ resources/js/components/emoji-picker.tsx | 182 ++++++++++++++++++++ resources/js/hooks/use-emoji-insertion.ts | 50 ++++++ resources/js/lib/emoji-picker.ts | 67 +++++++ resources/js/lib/text-selection.ts | 25 +++ tests/Unit/TextSelection.test.ts | 42 +++++ 7 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 resources/js/components/emoji-picker.tsx create mode 100644 resources/js/hooks/use-emoji-insertion.ts create mode 100644 resources/js/lib/emoji-picker.ts create mode 100644 resources/js/lib/text-selection.ts create mode 100644 tests/Unit/TextSelection.test.ts diff --git a/resources/js/components/commit-composer.tsx b/resources/js/components/commit-composer.tsx index a8898760d7..d1ef9c8a31 100644 --- a/resources/js/components/commit-composer.tsx +++ b/resources/js/components/commit-composer.tsx @@ -19,6 +19,7 @@ import { PreviewToggle, useComposerPreview, } from '@/components/composer-preview'; +import { EmojiPickerButton } from '@/components/emoji-picker'; import { GraphRail, RailAvatar } from '@/components/graph-rail'; import InputError from '@/components/input-error'; import type { UseIssueCommandReturn } from '@/components/issue-command'; @@ -38,6 +39,7 @@ import { } from '@/components/schedule-picker'; import { StashLabel } from '@/components/stash-label'; import { Button } from '@/components/ui/button'; +import { useEmojiInsertion } from '@/hooks/use-emoji-insertion'; import { useImageAttachments } from '@/hooks/use-image-attachments'; import { useLinkCard } from '@/hooks/use-link-card'; import type { UseLinkCardReturn } from '@/hooks/use-link-card'; @@ -126,7 +128,7 @@ function CommitComposerBody({ drop: () => void; }) { const keys = useKeyLabels(); - const { body, isRestored } = stash; + const { body, isRestored, updateBody } = stash; const { auth } = usePage().props; const { isScheduling, @@ -177,6 +179,13 @@ function CommitComposerBody({ return () => observer.disconnect(); }, [isInDialog]); + const insertEmoji = useEmojiInsertion({ + body, + updateBody, + textareaRef, + maxLength: isIssue ? maxBodyLength : maxRawBodyLength, + }); + return (
+ { const commit = textareaRef.current?.closest('[data-commit]'); @@ -1589,6 +1598,11 @@ function ReplyComposer({ onPick={attachments.pick} className="size-8" /> + void; + className?: string; +}) { + const textareaRef = useRef(null); + const [query, setQuery] = useState(''); + const results = query.trim() === '' ? null : searchEmoji(query); + + return ( + + + event.preventDefault()} + onClick={() => { + const active = document.activeElement; + + textareaRef.current = + active instanceof HTMLTextAreaElement + ? active + : null; + setQuery(''); + }} + aria-label="Add emoji" + className={cn( + buttonVariants({ variant: 'ghost', size: 'icon' }), + 'text-muted-foreground', + className, + )} + data-test="emoji-picker-button" + > + + + + + + {({ close }) => { + const pick = (emoji: string) => { + onPick(emoji, textareaRef.current); + }; + + const pickAndClose = (emoji: string) => { + const textarea = textareaRef.current; + + onPick(emoji, textarea); + close(textarea ?? undefined); + }; + + const pickFirst = (event: KeyboardEvent) => { + if (event.key !== 'Enter') { + return; + } + + event.preventDefault(); + + const first = (results ?? popularEmoji)[0]; + + if (first) { + pickAndClose(first.emoji); + } + }; + + return ( + <> +
+ + setQuery(event.target.value) + } + onKeyDown={pickFirst} + placeholder="Search emoji" + aria-label="Search emoji" + autoComplete="off" + className="h-8 w-full rounded-md border bg-background px-2 text-base outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 md:text-sm" + /> +
+ +
+ {results === null ? ( + <> + + + + ) : results.length === 0 ? ( +

+ No emoji found. +

+ ) : ( + + )} +
+ + ); + }} +
+
+ ); +} + +function EmojiSection({ + title, + emoji, + onPick, + className, +}: { + title: string; + emoji: PickableEmoji[]; + onPick: (emoji: string) => void; + className?: string; +}) { + return ( +
+

+ {title} +

+ +
+ ); +} + +function EmojiGrid({ + emoji, + onPick, +}: { + emoji: PickableEmoji[]; + onPick: (emoji: string) => void; +}) { + return ( +
+ {emoji.map(({ name, emoji: character }) => ( + + ))} +
+ ); +} diff --git a/resources/js/hooks/use-emoji-insertion.ts b/resources/js/hooks/use-emoji-insertion.ts new file mode 100644 index 0000000000..2367231f5c --- /dev/null +++ b/resources/js/hooks/use-emoji-insertion.ts @@ -0,0 +1,50 @@ +import type { RefObject } from 'react'; +import { useLayoutEffect, useRef } from 'react'; +import { insertAtSelection } from '@/lib/text-selection'; + +/** + * Inserts a picked emoji at the caret of the composer's textarea. The caret is put back once React has written the + * new body, since writing a long value can land after an animation frame and would leave the caret at the end. Focus + * is left alone so the picker can stay open for the next emoji. + */ +export function useEmojiInsertion({ + body, + updateBody, + textareaRef, + maxLength, +}: { + body: string; + updateBody: (body: string) => void; + textareaRef: RefObject; + maxLength: number; +}): (emoji: string, textarea: HTMLTextAreaElement | null) => void { + const caret = useRef(null); + + useLayoutEffect(() => { + const element = textareaRef.current; + + if (caret.current === null || element === null) { + return; + } + + element.setSelectionRange(caret.current, caret.current); + caret.current = null; + }, [body, textareaRef]); + + return (emoji, textarea) => { + const element = textareaRef.current; + const inserted = insertAtSelection( + body, + textarea !== null && textarea === element ? element : null, + emoji, + maxLength, + ); + + if (inserted === null) { + return; + } + + caret.current = inserted.caret; + updateBody(inserted.value); + }; +} diff --git a/resources/js/lib/emoji-picker.ts b/resources/js/lib/emoji-picker.ts new file mode 100644 index 0000000000..50f76767b4 --- /dev/null +++ b/resources/js/lib/emoji-picker.ts @@ -0,0 +1,67 @@ +import emojiShortcodes from '@/lib/emoji-shortcodes.json?raw'; + +export type PickableEmoji = { + name: string; + emoji: string; +}; + +const popularNames = [ + '+1', + 'heart', + 'joy', + 'fire', + 'rocket', + 'tada', + 'eyes', + '100', + 'pray', + 'thinking', + 'sweat_smile', + 'clap', + 'sparkles', + 'white_check_mark', + 'bug', + 'coffee', +]; + +const shortcodeEmoji = JSON.parse(emojiShortcodes) as Record; + +const seenEmoji = new Set(); + +/** + * Every emoji once, under the first of its shortcodes, so aliases like :+1: and :thumbsup: share one button. + */ +const pickableEmoji: PickableEmoji[] = Object.entries(shortcodeEmoji).flatMap( + ([name, emoji]) => { + if (seenEmoji.has(emoji)) { + return []; + } + + seenEmoji.add(emoji); + + return [{ name, emoji }]; + }, +); + +export const popularEmoji: PickableEmoji[] = popularNames.flatMap((name) => + shortcodeEmoji[name] ? [{ name, emoji: shortcodeEmoji[name] }] : [], +); + +/** + * Matches the search against every shortcode, aliases included, so :thumbsup finds πŸ‘ too. + */ +export function searchEmoji(query: string): PickableEmoji[] { + const term = query.trim().replace(/^:|:$/g, '').toLowerCase(); + + if (term === '') { + return pickableEmoji; + } + + const matching = new Set( + Object.entries(shortcodeEmoji) + .filter(([name]) => name.includes(term)) + .map(([, emoji]) => emoji), + ); + + return pickableEmoji.filter(({ emoji }) => matching.has(emoji)); +} diff --git a/resources/js/lib/text-selection.ts b/resources/js/lib/text-selection.ts new file mode 100644 index 0000000000..9e00401eb1 --- /dev/null +++ b/resources/js/lib/text-selection.ts @@ -0,0 +1,25 @@ +export type TextSelection = Pick< + HTMLTextAreaElement, + 'selectionStart' | 'selectionEnd' +>; + +/** + * Inserts the text over the selection, or at the end without one. Null when the result would be longer than + * `maxLength` characters, the same cap the textarea puts on typing. + */ +export function insertAtSelection( + value: string, + selection: TextSelection | null, + text: string, + maxLength: number, +): { value: string; caret: number } | null { + const start = selection?.selectionStart ?? value.length; + const end = selection?.selectionEnd ?? value.length; + const next = value.slice(0, start) + text + value.slice(end); + + if (Array.from(next).length > maxLength) { + return null; + } + + return { value: next, caret: start + text.length }; +} diff --git a/tests/Unit/TextSelection.test.ts b/tests/Unit/TextSelection.test.ts new file mode 100644 index 0000000000..1b325a7161 --- /dev/null +++ b/tests/Unit/TextSelection.test.ts @@ -0,0 +1,42 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { insertAtSelection } from '../../resources/js/lib/text-selection.ts'; + +void test('inserts at the caret and moves the caret after the insertion', () => { + assert.deepEqual( + insertAtSelection( + 'shipped it', + { selectionStart: 8, selectionEnd: 8 }, + 'πŸš€ ', + 280, + ), + { value: 'shipped πŸš€ it', caret: 11 }, + ); +}); + +void test('replaces the selected text', () => { + assert.deepEqual( + insertAtSelection( + 'shipped it', + { selectionStart: 8, selectionEnd: 10 }, + 'πŸŽ‰', + 280, + ), + { value: 'shipped πŸŽ‰', caret: 10 }, + ); +}); + +void test('appends without a selection', () => { + assert.deepEqual(insertAtSelection('shipped', null, ' πŸ”₯', 280), { + value: 'shipped πŸ”₯', + caret: 10, + }); +}); + +void test('refuses an insertion that would not fit, counting characters like the composer', () => { + assert.deepEqual(insertAtSelection('a'.repeat(279), null, 'πŸ”₯', 280), { + value: `${'a'.repeat(279)}πŸ”₯`, + caret: 281, + }); + assert.equal(insertAtSelection('a'.repeat(280), null, 'πŸ”₯', 280), null); +}); From 1c36d347a6bc574e9c020012c502c12f0fb7c0d3 Mon Sep 17 00:00:00 2001 From: Houssem Eddine BENABDALLAH Date: Sun, 25 Oct 2026 13:55:42 +0100 Subject: [PATCH 2/7] feat: reuse the reactions emoji picker in composers and insert where the caret was --- KNOWLEDGE_BASE.md | 4 +- resources/js/components/emoji-picker.tsx | 181 +++++----------------- resources/js/hooks/use-emoji-insertion.ts | 20 ++- resources/js/lib/emoji-picker.ts | 67 -------- tests/Unit/EmojiInsertion.test.tsx | 105 +++++++++++++ 5 files changed, 153 insertions(+), 224 deletions(-) delete mode 100644 resources/js/lib/emoji-picker.ts create mode 100644 tests/Unit/EmojiInsertion.test.tsx diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index be110dcc12..06f36a2743 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -222,6 +222,8 @@ Sources: [translation endpoint](app/Http/Controllers/CommitTranslationController Commit and reply text supports inline code in backticks and fenced code blocks with syntax highlighting, a **Wrap lines** toggle, and a copy control. A title after the language, as in ```` ```php title="app/Models/User.php" ````, labels the block in place of the language. Text outside code supports emoji shortcodes, clickable URLs, and `@username` mentions. This is developer-oriented text formatting, not a full rich-text editor. +The smiley button in the commit and reply composers opens the same searchable emoji picker as reactions in remotes. Picking an emoji inserts it where the caret was, or over the selected text, and puts the caret right after it. + In commits, replies, and remotes, start a line with `>` to render a quote block. Consecutive quoted lines stay together; an unquoted line ends the block. Quotes keep inline code, links, mentions, and emoji formatting. A `>` inside code or in the middle of a line stays literal. Typing **@** in a composer opens developer suggestions. Use ↑/↓ to move, Enter or Tab to select, and Esc to dismiss. A bare @ suggests people you follow; typing more searches names and usernames. In replies, matching suggestions put the direct recipient first, then the root commit author, then other visible participants in that thread, followed by ordinary suggestions. Quotes prioritize only the quoted commit author. These priorities also apply to a bare @; typing a term still filters everyone, and yourself, muted developers, and blocked developers are excluded. In an org's messages, suggestions search the loaded member list instantly and update as membership refreshes. Suggestions are limited to the org's members, but you can still type anyone's handle yourself. Mentions link to profiles and can notify the mentioned developer after approval. Mentioning a GitHub organization, like `@laravel`, links to its [organization page](#organization-pages) and notifies nobody: a known organization links right away, one mentioned for the first time once the commit is approved and GitHub confirms it is an organization. Typing @ followed by a few letters also suggests organizations that already have a page, after developers. Organization mentions only link in commits and replies, not in remotes. Email addresses and scoped packages such as `@inertiajs/react` are not treated as mentions. @@ -230,7 +232,7 @@ Typing **#123** in a composer looks up that exact issue or pull request in the / A hex color such as `#ff8a4c` or `#fff` shows with a small swatch of the color, outside code. A color written only with digits counts when it has 6 or 8 digits or starts with a zero, which issue numbers here don't: `#000`, `#000000` and `#333333` are colors, while `#123` and `#333` stay plain text. -Sources: [body renderer](resources/js/components/commit-body.tsx), [colors](resources/js/lib/colors.ts), [code blocks](resources/js/components/code-block.tsx), [mention suggestions](app/Http/Controllers/MentionSuggestionController.php), [mention parsing](app/Services/Mentions.php). +Sources: [body renderer](resources/js/components/commit-body.tsx), [emoji picker button](resources/js/components/emoji-picker.tsx), [colors](resources/js/lib/colors.ts), [code blocks](resources/js/components/code-block.tsx), [mention suggestions](app/Http/Controllers/MentionSuggestionController.php), [mention parsing](app/Services/Mentions.php). ### Draft previews diff --git a/resources/js/components/emoji-picker.tsx b/resources/js/components/emoji-picker.tsx index b3e4215442..608363cb58 100644 --- a/resources/js/components/emoji-picker.tsx +++ b/resources/js/components/emoji-picker.tsx @@ -1,17 +1,20 @@ -import { Popover, PopoverButton, PopoverPanel } from '@headlessui/react'; import { Smile } from 'lucide-react'; -import type { KeyboardEvent } from 'react'; import { useRef, useState } from 'react'; import { ActionTooltip } from '@/components/action-tooltip'; +import { EmojiPicker } from '@/components/chat/message-reactions'; import { buttonVariants } from '@/components/ui/button'; -import type { PickableEmoji } from '@/lib/emoji-picker'; -import { popularEmoji, searchEmoji } from '@/lib/emoji-picker'; +import { + Popover, + PopoverContent, + PopoverTrigger, +} from '@/components/ui/popover'; import { cn } from '@/lib/utils'; +const noReactions = new Set(); + /** - * Pressing the button keeps the focus, and so the caret, in the textarea being written, which is handed to `onPick` - * along with the emoji. It is null when the picker was opened from the keyboard. Clicking an emoji leaves the picker - * open to add more; picking the first result with Enter closes it and returns to the textarea. + * The reactions' emoji picker, for composers. The emoji is handed over once the picker has closed, so `onPick` can put + * the focus back in the textarea instead of on this button. */ export function EmojiPickerButton({ disabled, @@ -19,29 +22,18 @@ export function EmojiPickerButton({ className, }: { disabled?: boolean; - onPick: (emoji: string, textarea: HTMLTextAreaElement | null) => void; + onPick: (emoji: string) => void; className?: string; }) { - const textareaRef = useRef(null); - const [query, setQuery] = useState(''); - const results = query.trim() === '' ? null : searchEmoji(query); + const [isOpen, setIsOpen] = useState(false); + const pickedEmoji = useRef(null); return ( - + - event.preventDefault()} - onClick={() => { - const active = document.activeElement; - - textareaRef.current = - active instanceof HTMLTextAreaElement - ? active - : null; - setQuery(''); - }} aria-label="Add emoji" className={cn( buttonVariants({ variant: 'ghost', size: 'icon' }), @@ -51,132 +43,31 @@ export function EmojiPickerButton({ data-test="emoji-picker-button" > - + + { + const emoji = pickedEmoji.current; - - {({ close }) => { - const pick = (emoji: string) => { - onPick(emoji, textareaRef.current); - }; - - const pickAndClose = (emoji: string) => { - const textarea = textareaRef.current; - - onPick(emoji, textarea); - close(textarea ?? undefined); - }; - - const pickFirst = (event: KeyboardEvent) => { - if (event.key !== 'Enter') { - return; - } - + if (emoji !== null) { event.preventDefault(); - - const first = (results ?? popularEmoji)[0]; - - if (first) { - pickAndClose(first.emoji); - } - }; - - return ( - <> -
- - setQuery(event.target.value) - } - onKeyDown={pickFirst} - placeholder="Search emoji" - aria-label="Search emoji" - autoComplete="off" - className="h-8 w-full rounded-md border bg-background px-2 text-base outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 md:text-sm" - /> -
- -
- {results === null ? ( - <> - - - - ) : results.length === 0 ? ( -

- No emoji found. -

- ) : ( - - )} -
- - ); + pickedEmoji.current = null; + onPick(emoji); + } }} -
+ > + { + pickedEmoji.current = emoji; + setIsOpen(false); + }} + /> +
); } - -function EmojiSection({ - title, - emoji, - onPick, - className, -}: { - title: string; - emoji: PickableEmoji[]; - onPick: (emoji: string) => void; - className?: string; -}) { - return ( -
-

- {title} -

- -
- ); -} - -function EmojiGrid({ - emoji, - onPick, -}: { - emoji: PickableEmoji[]; - onPick: (emoji: string) => void; -}) { - return ( -
- {emoji.map(({ name, emoji: character }) => ( - - ))} -
- ); -} diff --git a/resources/js/hooks/use-emoji-insertion.ts b/resources/js/hooks/use-emoji-insertion.ts index 2367231f5c..65b77dca10 100644 --- a/resources/js/hooks/use-emoji-insertion.ts +++ b/resources/js/hooks/use-emoji-insertion.ts @@ -3,9 +3,9 @@ import { useLayoutEffect, useRef } from 'react'; import { insertAtSelection } from '@/lib/text-selection'; /** - * Inserts a picked emoji at the caret of the composer's textarea. The caret is put back once React has written the - * new body, since writing a long value can land after an animation frame and would leave the caret at the end. Focus - * is left alone so the picker can stay open for the next emoji. + * Inserts a picked emoji where the caret was in the composer's textarea. A textarea keeps its selection while the + * picker has the focus, so this works whether the picker was opened with the pointer or the keyboard. Once React has + * written the new body, the focus goes back to the textarea with the caret right after the emoji. */ export function useEmojiInsertion({ body, @@ -17,7 +17,7 @@ export function useEmojiInsertion({ updateBody: (body: string) => void; textareaRef: RefObject; maxLength: number; -}): (emoji: string, textarea: HTMLTextAreaElement | null) => void { +}): (emoji: string) => void { const caret = useRef(null); useLayoutEffect(() => { @@ -27,20 +27,18 @@ export function useEmojiInsertion({ return; } + element.focus(); element.setSelectionRange(caret.current, caret.current); caret.current = null; }, [body, textareaRef]); - return (emoji, textarea) => { + return (emoji) => { const element = textareaRef.current; - const inserted = insertAtSelection( - body, - textarea !== null && textarea === element ? element : null, - emoji, - maxLength, - ); + const inserted = insertAtSelection(body, element, emoji, maxLength); if (inserted === null) { + element?.focus(); + return; } diff --git a/resources/js/lib/emoji-picker.ts b/resources/js/lib/emoji-picker.ts deleted file mode 100644 index 50f76767b4..0000000000 --- a/resources/js/lib/emoji-picker.ts +++ /dev/null @@ -1,67 +0,0 @@ -import emojiShortcodes from '@/lib/emoji-shortcodes.json?raw'; - -export type PickableEmoji = { - name: string; - emoji: string; -}; - -const popularNames = [ - '+1', - 'heart', - 'joy', - 'fire', - 'rocket', - 'tada', - 'eyes', - '100', - 'pray', - 'thinking', - 'sweat_smile', - 'clap', - 'sparkles', - 'white_check_mark', - 'bug', - 'coffee', -]; - -const shortcodeEmoji = JSON.parse(emojiShortcodes) as Record; - -const seenEmoji = new Set(); - -/** - * Every emoji once, under the first of its shortcodes, so aliases like :+1: and :thumbsup: share one button. - */ -const pickableEmoji: PickableEmoji[] = Object.entries(shortcodeEmoji).flatMap( - ([name, emoji]) => { - if (seenEmoji.has(emoji)) { - return []; - } - - seenEmoji.add(emoji); - - return [{ name, emoji }]; - }, -); - -export const popularEmoji: PickableEmoji[] = popularNames.flatMap((name) => - shortcodeEmoji[name] ? [{ name, emoji: shortcodeEmoji[name] }] : [], -); - -/** - * Matches the search against every shortcode, aliases included, so :thumbsup finds πŸ‘ too. - */ -export function searchEmoji(query: string): PickableEmoji[] { - const term = query.trim().replace(/^:|:$/g, '').toLowerCase(); - - if (term === '') { - return pickableEmoji; - } - - const matching = new Set( - Object.entries(shortcodeEmoji) - .filter(([name]) => name.includes(term)) - .map(([, emoji]) => emoji), - ); - - return pickableEmoji.filter(({ emoji }) => matching.has(emoji)); -} diff --git a/tests/Unit/EmojiInsertion.test.tsx b/tests/Unit/EmojiInsertion.test.tsx new file mode 100644 index 0000000000..89486d2c11 --- /dev/null +++ b/tests/Unit/EmojiInsertion.test.tsx @@ -0,0 +1,105 @@ +/// +import assert from 'node:assert/strict'; +import * as React from 'react'; +import { afterAll, beforeEach, test, vi } from 'vite-plus/test'; + +let layoutEffect: () => void = () => {}; + +// The layout effect runs when the test says React has written the new body. +vi.doMock('react', () => ({ + ...React, + useLayoutEffect: (effect: () => void) => { + layoutEffect = effect; + }, + useRef: (initial: unknown) => ({ current: initial }), +})); + +afterAll(() => { + vi.doUnmock('react'); +}); + +const { useEmojiInsertion } = + await import('../../resources/js/hooks/use-emoji-insertion'); + +/** + * A textarea that kept its selection after the picker took the focus, as browsers do. + */ +function blurredTextarea( + selectionStart: number, + selectionEnd = selectionStart, +) { + const focus = vi.fn(); + const setSelectionRange = vi.fn(); + const textarea = { + selectionStart, + selectionEnd, + focus, + setSelectionRange, + } as unknown as HTMLTextAreaElement; + + return { textarea, focus, setSelectionRange }; +} + +const updateBody = vi.fn(); + +beforeEach(() => { + vi.clearAllMocks(); +}); + +test('inserts where the caret was and puts the focus back right after the emoji', () => { + const { textarea, focus, setSelectionRange } = blurredTextarea(8); + const insertEmoji = useEmojiInsertion({ + body: 'shipped it', + updateBody, + textareaRef: { current: textarea }, + maxLength: 280, + }); + + insertEmoji('πŸš€'); + layoutEffect(); + + assert.deepEqual(updateBody.mock.calls, [['shipped πŸš€it']]); + assert.equal(focus.mock.calls.length, 1); + assert.deepEqual(setSelectionRange.mock.calls, [[10, 10]]); +}); + +test('replaces the text that was selected', () => { + const { textarea } = blurredTextarea(8, 10); + + useEmojiInsertion({ + body: 'shipped it', + updateBody, + textareaRef: { current: textarea }, + maxLength: 280, + })('πŸŽ‰'); + + assert.deepEqual(updateBody.mock.calls, [['shipped πŸŽ‰']]); +}); + +test('leaves a full composer as it is and returns to it', () => { + const { textarea, focus, setSelectionRange } = blurredTextarea(3); + + useEmojiInsertion({ + body: 'abc', + updateBody, + textareaRef: { current: textarea }, + maxLength: 3, + })('πŸ”₯'); + layoutEffect(); + + assert.equal(updateBody.mock.calls.length, 0); + assert.equal(focus.mock.calls.length, 1); + assert.equal(setSelectionRange.mock.calls.length, 0); +}); + +test('appends when the textarea is gone', () => { + useEmojiInsertion({ + body: 'shipped', + updateBody, + textareaRef: { current: null }, + maxLength: 280, + })(' πŸ”₯'); + layoutEffect(); + + assert.deepEqual(updateBody.mock.calls, [['shipped πŸ”₯']]); +}); From 5b98325db8b5790d63739d632e382d9ba1ed79a1 Mon Sep 17 00:00:00 2001 From: Houssem Eddine BENABDALLAH Date: Sun, 25 Oct 2026 14:40:22 +0100 Subject: [PATCH 3/7] fix: let Enter confirm an input method candidate in the emoji search --- .../js/components/chat/message-reactions.tsx | 5 ++ tests/Unit/EmojiPickerKeys.test.tsx | 73 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/Unit/EmojiPickerKeys.test.tsx diff --git a/resources/js/components/chat/message-reactions.tsx b/resources/js/components/chat/message-reactions.tsx index 71e53be27e..70734a9804 100644 --- a/resources/js/components/chat/message-reactions.tsx +++ b/resources/js/components/chat/message-reactions.tsx @@ -565,6 +565,11 @@ export function EmojiPicker({ }; const onKeyDown = (event: KeyboardEvent) => { + // Enter also confirms an input method's candidate, which is not a pick. + if (event.nativeEvent.isComposing) { + return; + } + const moves: Record = { ArrowRight: 1, ArrowLeft: -1, diff --git a/tests/Unit/EmojiPickerKeys.test.tsx b/tests/Unit/EmojiPickerKeys.test.tsx new file mode 100644 index 0000000000..dea76a411f --- /dev/null +++ b/tests/Unit/EmojiPickerKeys.test.tsx @@ -0,0 +1,73 @@ +/// +import assert from 'node:assert/strict'; +import * as React from 'react'; +import type { ReactElement, ReactNode } from 'react'; +import { afterAll, test, vi } from 'vite-plus/test'; + +// Renders the picker once, as a plain function, with the groups still loading. +vi.doMock('react', () => ({ + ...React, + useEffect: () => {}, + useId: () => 'picker', + useMemo: (factory: () => unknown) => factory(), + useRef: (initial: unknown) => ({ current: initial }), + useState: (initial: unknown) => [ + typeof initial === 'function' ? (initial as () => unknown)() : initial, + vi.fn(), + ], +})); + +afterAll(() => { + vi.doUnmock('react'); +}); + +const { EmojiPicker } = + await import('../../resources/js/components/chat/message-reactions'); + +type Element = ReactElement>; + +function elements(node: ReactNode): Element[] { + if (!React.isValidElement(node)) return []; + const element = node as Element; + + return [ + element, + ...React.Children.toArray(element.props.children as ReactNode).flatMap( + elements, + ), + ]; +} + +function pressEnter(isComposing: boolean) { + const onPick = vi.fn(); + const preventDefault = vi.fn(); + const search = elements( + EmojiPicker({ + usage: [{ emoji: 'πŸš€', count: 2 }], + reacted: new Set(), + onPick, + }), + ).find((element) => element.props['data-test'] === 'emoji-search')!; + + (search.props.onKeyDown as (event: unknown) => void)({ + key: 'Enter', + nativeEvent: { isComposing }, + preventDefault, + }); + + return { onPick, preventDefault }; +} + +test('Enter picks the highlighted emoji', () => { + const { onPick, preventDefault } = pressEnter(false); + + assert.deepEqual(onPick.mock.calls, [['πŸš€']]); + assert.equal(preventDefault.mock.calls.length, 1); +}); + +test('Enter that confirms an input method candidate picks nothing', () => { + const { onPick, preventDefault } = pressEnter(true); + + assert.equal(onPick.mock.calls.length, 0); + assert.equal(preventDefault.mock.calls.length, 0); +}); From 4cbe1b636f39bd4a75910a31be7313e7f166ff0a Mon Sep 17 00:00:00 2001 From: Houssem Eddine BENABDALLAH Date: Sun, 25 Oct 2026 15:03:55 +0100 Subject: [PATCH 4/7] fix: ignore Safari's Enter that confirms an input method candidate in the emoji search --- resources/js/components/chat/message-reactions.tsx | 5 +++-- tests/Unit/EmojiPickerKeys.test.tsx | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/resources/js/components/chat/message-reactions.tsx b/resources/js/components/chat/message-reactions.tsx index 70734a9804..3dec867d0c 100644 --- a/resources/js/components/chat/message-reactions.tsx +++ b/resources/js/components/chat/message-reactions.tsx @@ -565,8 +565,9 @@ export function EmojiPicker({ }; const onKeyDown = (event: KeyboardEvent) => { - // Enter also confirms an input method's candidate, which is not a pick. - if (event.nativeEvent.isComposing) { + // Enter also confirms an input method's candidate, which is not a pick. Safari reports that Enter with keyCode + // 229 instead of isComposing. + if (event.nativeEvent.isComposing || event.keyCode === 229) { return; } diff --git a/tests/Unit/EmojiPickerKeys.test.tsx b/tests/Unit/EmojiPickerKeys.test.tsx index dea76a411f..c5d33da531 100644 --- a/tests/Unit/EmojiPickerKeys.test.tsx +++ b/tests/Unit/EmojiPickerKeys.test.tsx @@ -38,7 +38,7 @@ function elements(node: ReactNode): Element[] { ]; } -function pressEnter(isComposing: boolean) { +function pressEnter(isComposing: boolean, keyCode = 13) { const onPick = vi.fn(); const preventDefault = vi.fn(); const search = elements( @@ -51,6 +51,7 @@ function pressEnter(isComposing: boolean) { (search.props.onKeyDown as (event: unknown) => void)({ key: 'Enter', + keyCode, nativeEvent: { isComposing }, preventDefault, }); @@ -71,3 +72,10 @@ test('Enter that confirms an input method candidate picks nothing', () => { assert.equal(onPick.mock.calls.length, 0); assert.equal(preventDefault.mock.calls.length, 0); }); + +test("Safari's Enter that confirms an input method candidate picks nothing", () => { + const { onPick, preventDefault } = pressEnter(false, 229); + + assert.equal(onPick.mock.calls.length, 0); + assert.equal(preventDefault.mock.calls.length, 0); +}); From 58182b9deb4d64afa226f7e80aa8f557ab49db3a Mon Sep 17 00:00:00 2001 From: Houssem Eddine BENABDALLAH Date: Mon, 26 Oct 2026 14:18:46 +0100 Subject: [PATCH 5/7] fix: keep the caret where it was when an emoji does not fit --- resources/js/hooks/use-emoji-insertion.ts | 8 +++++++- tests/Unit/EmojiInsertion.test.tsx | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/resources/js/hooks/use-emoji-insertion.ts b/resources/js/hooks/use-emoji-insertion.ts index 65b77dca10..1f2b18fff5 100644 --- a/resources/js/hooks/use-emoji-insertion.ts +++ b/resources/js/hooks/use-emoji-insertion.ts @@ -37,7 +37,13 @@ export function useEmojiInsertion({ const inserted = insertAtSelection(body, element, emoji, maxLength); if (inserted === null) { - element?.focus(); + // Focusing a restored draft moves a caret at its start to the end, so the selection is put back after. + if (element !== null) { + const { selectionStart, selectionEnd } = element; + + element.focus(); + element.setSelectionRange(selectionStart, selectionEnd); + } return; } diff --git a/tests/Unit/EmojiInsertion.test.tsx b/tests/Unit/EmojiInsertion.test.tsx index 89486d2c11..fd1cd612a4 100644 --- a/tests/Unit/EmojiInsertion.test.tsx +++ b/tests/Unit/EmojiInsertion.test.tsx @@ -89,7 +89,22 @@ test('leaves a full composer as it is and returns to it', () => { assert.equal(updateBody.mock.calls.length, 0); assert.equal(focus.mock.calls.length, 1); - assert.equal(setSelectionRange.mock.calls.length, 0); + assert.deepEqual(setSelectionRange.mock.calls, [[3, 3]]); +}); + +test('keeps the caret at the start of a full restored draft', () => { + const { textarea, focus, setSelectionRange } = blurredTextarea(0); + + useEmojiInsertion({ + body: 'abc', + updateBody, + textareaRef: { current: textarea }, + maxLength: 3, + })('πŸ”₯'); + + assert.equal(updateBody.mock.calls.length, 0); + assert.equal(focus.mock.calls.length, 1); + assert.deepEqual(setSelectionRange.mock.calls, [[0, 0]]); }); test('appends when the textarea is gone', () => { From b076b12fd3ede6c4b900a68fb91175f2c0185007 Mon Sep 17 00:00:00 2001 From: Houssem Eddine BENABDALLAH Date: Thu, 29 Oct 2026 19:24:01 +0100 Subject: [PATCH 6/7] feat: put the emoji button inside the text box, bottom right --- resources/js/components/commit-composer.tsx | 15 ++++++++++----- resources/js/components/commit-thread.tsx | 16 ++++++++++------ resources/js/components/emoji-picker.tsx | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/resources/js/components/commit-composer.tsx b/resources/js/components/commit-composer.tsx index d1ef9c8a31..7f0ae2e568 100644 --- a/resources/js/components/commit-composer.tsx +++ b/resources/js/components/commit-composer.tsx @@ -285,13 +285,22 @@ function CommitComposerBody({ aria-invalid={error ? true : undefined} aria-describedby={`${id}-count`} className={cn( - 'col-start-1 row-start-1 block field-sizing-content max-h-[60dvh] min-h-10 w-full resize-none overflow-x-hidden overflow-y-auto bg-transparent py-1.5 text-[17px] leading-relaxed outline-none placeholder:text-muted-foreground focus-visible:outline-none', + 'col-start-1 row-start-1 block field-sizing-content max-h-[60dvh] min-h-10 w-full resize-none overflow-x-hidden overflow-y-auto bg-transparent py-1.5 pr-9 text-[17px] leading-relaxed outline-none placeholder:text-muted-foreground focus-visible:outline-none', { invisible: preview.isPreviewing, 'max-h-none': isInDialog, }, )} /> + {!preview.isPreviewing && ( +
+ +
+ )} {preview.isPreviewing && ( - + {!preview.isPreviewing && ( +
+ +
+ )} {preview.isPreviewing && ( -
{ From d23e8c6b45153025eb9234982293149cc88be6bf Mon Sep 17 00:00:00 2001 From: Houssem Eddine BENABDALLAH Date: Sat, 31 Oct 2026 12:55:54 +0100 Subject: [PATCH 7/7] fix: put the caret back when an emoji replaces the same emoji --- resources/js/hooks/use-emoji-insertion.ts | 12 +++ tests/Unit/EmojiInsertionLifecycle.test.tsx | 109 ++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 tests/Unit/EmojiInsertionLifecycle.test.tsx diff --git a/resources/js/hooks/use-emoji-insertion.ts b/resources/js/hooks/use-emoji-insertion.ts index 1f2b18fff5..491e66f8da 100644 --- a/resources/js/hooks/use-emoji-insertion.ts +++ b/resources/js/hooks/use-emoji-insertion.ts @@ -48,6 +48,18 @@ export function useEmojiInsertion({ return; } + // An emoji picked over the same emoji leaves the body as it was, so React skips the effect: return here. + if (inserted.value === body) { + caret.current = null; + + if (element !== null) { + element.focus(); + element.setSelectionRange(inserted.caret, inserted.caret); + } + + return; + } + caret.current = inserted.caret; updateBody(inserted.value); }; diff --git a/tests/Unit/EmojiInsertionLifecycle.test.tsx b/tests/Unit/EmojiInsertionLifecycle.test.tsx new file mode 100644 index 0000000000..7859ab55c0 --- /dev/null +++ b/tests/Unit/EmojiInsertionLifecycle.test.tsx @@ -0,0 +1,109 @@ +/// +import assert from 'node:assert/strict'; +import type { Ref } from 'react'; +import { act, createRef, useImperativeHandle, useRef, useState } from 'react'; +import { createRoot } from 'react-dom/client'; +import { test, vi } from 'vite-plus/test'; +import { useEmojiInsertion } from '../../resources/js/hooks/use-emoji-insertion'; + +// React DOM reads `window.event` to pick an update's priority; nothing else of the DOM is needed here. +vi.stubGlobal('window', globalThis); +vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true); + +/** + * The bare minimum React DOM needs to mount a component that renders nothing and commit its updates. + */ +function container(): Element { + const view = { + document: { activeElement: null }, + HTMLIFrameElement: class {}, + }; + const node = { + nodeType: 1, + nodeName: 'DIV', + tagName: 'DIV', + addEventListener: () => {}, + removeEventListener: () => {}, + }; + + return { + ...node, + ownerDocument: { ...node, defaultView: view }, + } as unknown as Element; +} + +type Composer = { + body: string; + type: (body: string) => void; + insertEmoji: (emoji: string) => void; +}; + +/** + * Mounts a composer with React's real effect lifecycle, around a textarea that keeps its selection. + */ +function mountComposer( + body: string, + selectionStart: number, + selectionEnd: number, +) { + const textarea = { + selectionStart, + selectionEnd, + focus: vi.fn(), + setSelectionRange: vi.fn(), + }; + const composer = createRef(); + + function Composer({ handle }: { handle: Ref }) { + const [value, setValue] = useState(body); + const textareaRef = useRef(textarea as unknown as HTMLTextAreaElement); + const insertEmoji = useEmojiInsertion({ + body: value, + updateBody: setValue, + textareaRef, + maxLength: 280, + }); + + useImperativeHandle(handle, () => ({ + body: value, + type: setValue, + insertEmoji, + })); + + return null; + } + + act(() => createRoot(container()).render()); + + return { composer, textarea }; +} + +test('puts the caret back after the emoji when it replaces the same emoji', () => { + const { composer, textarea } = mountComposer('aπŸš€b', 1, 3); + + act(() => composer.current?.insertEmoji('πŸš€')); + + assert.equal(composer.current?.body, 'aπŸš€b'); + assert.equal(textarea.focus.mock.calls.length, 1); + assert.deepEqual(textarea.setSelectionRange.mock.calls, [[3, 3]]); +}); + +test('leaves the caret alone on the next edit after replacing the same emoji', () => { + const { composer, textarea } = mountComposer('aπŸš€b', 1, 3); + + act(() => composer.current?.insertEmoji('πŸš€')); + textarea.setSelectionRange.mockClear(); + act(() => composer.current?.type('aπŸš€bx')); + + assert.equal(composer.current?.body, 'aπŸš€bx'); + assert.equal(textarea.setSelectionRange.mock.calls.length, 0); +}); + +test('puts the caret right after a new emoji once React has written it', () => { + const { composer, textarea } = mountComposer('shipped it', 8, 8); + + act(() => composer.current?.insertEmoji('πŸš€')); + + assert.equal(composer.current?.body, 'shipped πŸš€it'); + assert.deepEqual(textarea.setSelectionRange.mock.calls, [[10, 10]]); +});