Repository navigation
feat: pick emoji from a button in the commit and reply composers #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hbenabdallah
wants to merge
5
commits into
dev-null-sh:production
Choose a base branch
from
hbenabdallah:feat/emoji-picker
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0eacbe2
feat: pick emoji from a button in the commit and reply composers
hbenabdallah 739aee3
feat: reuse the reactions emoji picker in composers and insert where …
hbenabdallah d73adc3
fix: let Enter confirm an input method candidate in the emoji search
hbenabdallah 8ceee33
fix: ignore Safari's Enter that confirms an input method candidate in…
hbenabdallah 38944b8
fix: keep the caret where it was when an emoji does not fit
hbenabdallah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { Smile } from 'lucide-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 { | ||
| Popover, | ||
| PopoverContent, | ||
| PopoverTrigger, | ||
| } from '@/components/ui/popover'; | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| const noReactions = new Set<string>(); | ||
|
|
||
| /** | ||
| * 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, | ||
| onPick, | ||
| className, | ||
| }: { | ||
| disabled?: boolean; | ||
| onPick: (emoji: string) => void; | ||
| className?: string; | ||
| }) { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const pickedEmoji = useRef<string | null>(null); | ||
|
|
||
| return ( | ||
| <Popover open={isOpen} onOpenChange={setIsOpen}> | ||
| <ActionTooltip label="Add emoji"> | ||
| <PopoverTrigger | ||
| type="button" | ||
| disabled={disabled} | ||
| aria-label="Add emoji" | ||
| className={cn( | ||
| buttonVariants({ variant: 'ghost', size: 'icon' }), | ||
| 'text-muted-foreground', | ||
| className, | ||
| )} | ||
| data-test="emoji-picker-button" | ||
| > | ||
| <Smile className="size-5" /> | ||
| </PopoverTrigger> | ||
| </ActionTooltip> | ||
| <PopoverContent | ||
| align="start" | ||
| collisionPadding={8} | ||
| className="flex h-[min(25rem,var(--radix-popover-content-available-height))] w-76 flex-col overflow-hidden rounded-lg p-0 shadow-[0_12px_32px_-12px_rgb(0_0_0/0.35)]" | ||
| onCloseAutoFocus={(event) => { | ||
| const emoji = pickedEmoji.current; | ||
|
|
||
| if (emoji !== null) { | ||
| event.preventDefault(); | ||
| pickedEmoji.current = null; | ||
| onPick(emoji); | ||
| } | ||
| }} | ||
| > | ||
| <EmojiPicker | ||
| usage={[]} | ||
| reacted={noReactions} | ||
| onPick={(emoji) => { | ||
| pickedEmoji.current = emoji; | ||
| setIsOpen(false); | ||
| }} | ||
| /> | ||
| </PopoverContent> | ||
| </Popover> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import type { RefObject } from 'react'; | ||
| import { useLayoutEffect, useRef } from 'react'; | ||
| import { insertAtSelection } from '@/lib/text-selection'; | ||
|
|
||
| /** | ||
| * 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, | ||
| updateBody, | ||
| textareaRef, | ||
| maxLength, | ||
| }: { | ||
| body: string; | ||
| updateBody: (body: string) => void; | ||
| textareaRef: RefObject<HTMLTextAreaElement | null>; | ||
| maxLength: number; | ||
| }): (emoji: string) => void { | ||
| const caret = useRef<number | null>(null); | ||
|
|
||
| useLayoutEffect(() => { | ||
| const element = textareaRef.current; | ||
|
|
||
| if (caret.current === null || element === null) { | ||
| return; | ||
| } | ||
|
|
||
| element.focus(); | ||
| element.setSelectionRange(caret.current, caret.current); | ||
| caret.current = null; | ||
| }, [body, textareaRef]); | ||
|
|
||
| return (emoji) => { | ||
| const element = textareaRef.current; | ||
| const inserted = insertAtSelection(body, element, emoji, maxLength); | ||
|
|
||
| if (inserted === null) { | ||
| // 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; | ||
| } | ||
|
|
||
| caret.current = inserted.caret; | ||
| updateBody(inserted.value); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /// <reference types="node" /> | ||
| 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.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', () => { | ||
| useEmojiInsertion({ | ||
| body: 'shipped', | ||
| updateBody, | ||
| textareaRef: { current: null }, | ||
| maxLength: 280, | ||
| })(' 🔥'); | ||
| layoutEffect(); | ||
|
|
||
| assert.deepEqual(updateBody.mock.calls, [['shipped 🔥']]); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.