Skip to content

Commit 283e81a

Browse files
committed
Overwrite highlighted suggestion when hovering over another one
1 parent 3a978ed commit 283e81a

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/sidebar/components/MarkdownEditor.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ function TextArea({
383383
loadingUsers={usersForMentions.status === 'loading'}
384384
users={userSuggestions}
385385
highlightedSuggestion={highlightedSuggestion}
386+
onHighlightSuggestion={setHighlightedSuggestion}
386387
onSelectUser={insertMention}
387388
usersListboxId={usersListboxId}
388389
mentionMode={mentionMode}

src/sidebar/components/MentionSuggestionsPopover.tsx

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ type SuggestionItemProps = {
1010
usersListboxId: string;
1111
highlighted: boolean;
1212
mentionMode: MentionMode;
13-
onSelectUser: (user: UserItem) => void;
13+
onSelect: () => void;
14+
onHighlight: () => void;
1415
};
1516

1617
function SuggestionItem({
1718
user,
1819
usersListboxId,
1920
highlighted,
2021
mentionMode,
21-
onSelectUser,
22+
onSelect,
23+
onHighlight,
2224
}: SuggestionItemProps) {
2325
const showUsername = mentionMode === 'username';
2426

@@ -31,8 +33,7 @@ function SuggestionItem({
3133
key={user.username}
3234
id={`${usersListboxId}-${user.username}`}
3335
className={classnames(
34-
'flex justify-between items-center gap-x-2',
35-
'rounded p-2 hover:bg-grey-2',
36+
'flex justify-between items-center gap-x-2 rounded p-2',
3637
// Adjust line height relative to the font size. This avoids
3738
// vertically cropped usernames due to the use of `truncate`.
3839
'leading-tight',
@@ -42,8 +43,13 @@ function SuggestionItem({
4243
)}
4344
onClick={e => {
4445
e.stopPropagation();
45-
onSelectUser(user);
46+
onSelect();
4647
}}
48+
// Using onMouseMove instead of onMouseEnter, so that if the mouse is
49+
// already hovering this item, and then we change the highlighted
50+
// suggestion via arrow keys, when the mouse is moved again, this item
51+
// becomes highlighted without having to first leave it and enter again.
52+
onMouseMove={onHighlight}
4753
role="option"
4854
aria-selected={highlighted}
4955
>
@@ -69,6 +75,8 @@ export type MentionSuggestionsPopoverProps = Pick<
6975
users: UserItem[];
7076
/** Index for currently highlighted suggestion */
7177
highlightedSuggestion: number;
78+
/** Invoked when currently highlighted suggestion index is changed */
79+
onHighlightSuggestion: (index: number) => void;
7280
/** Invoked when a user is selected */
7381
onSelectUser: (selectedSuggestion: UserItem) => void;
7482
/** Element ID for the user suggestions listbox */
@@ -85,6 +93,7 @@ export default function MentionSuggestionsPopover({
8593
users,
8694
onSelectUser,
8795
highlightedSuggestion,
96+
onHighlightSuggestion,
8897
usersListboxId,
8998
mentionMode,
9099
...popoverProps
@@ -113,7 +122,8 @@ export default function MentionSuggestionsPopover({
113122
highlighted={highlightedSuggestion === index}
114123
mentionMode={mentionMode}
115124
usersListboxId={usersListboxId}
116-
onSelectUser={onSelectUser}
125+
onSelect={() => onSelectUser(u)}
126+
onHighlight={() => onHighlightSuggestion(index)}
117127
/>
118128
))}
119129
{users.length === 0 && (

src/sidebar/components/test/MentionSuggestionsPopover-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ describe('MentionSuggestionsPopover', () => {
9090

9191
assert.calledWith(onSelectUser, defaultUsers[index]);
9292
});
93+
94+
it('sets suggestion as highlighted when hovering over it', () => {
95+
const onHighlightSuggestion = sinon.stub();
96+
const wrapper = createComponent({ onHighlightSuggestion });
97+
const hoverSuggestion = i =>
98+
wrapper.find('li').at(i).simulate('mousemove');
99+
100+
hoverSuggestion(index);
101+
102+
assert.calledWith(onHighlightSuggestion, index);
103+
});
93104
});
94105

95106
[

0 commit comments

Comments
 (0)