-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Populate list of suggestions matching @mentions
- Loading branch information
Showing
10 changed files
with
625 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,71 @@ | ||
import { Popover } from '@hypothesis/frontend-shared'; | ||
import type { PopoverProps } from '@hypothesis/frontend-shared/lib/components/feedback/Popover'; | ||
import classnames from 'classnames'; | ||
|
||
export type UserItem = { | ||
username: string; | ||
displayName: string | null; | ||
}; | ||
|
||
export type MentionSuggestionsPopoverProps = Pick< | ||
PopoverProps, | ||
'open' | 'onClose' | 'anchorElementRef' | ||
> & { | ||
/** List of suggestions to display */ | ||
suggestions: UserItem[]; | ||
/** Index for currently highlighted suggestion */ | ||
highlightedSuggestion: number; | ||
/** Invoked when a suggestion is selected */ | ||
onSelectSuggestion: (selectedSuggestion: UserItem) => void; | ||
}; | ||
|
||
/** | ||
* A Popover component that displays a list of user suggestions. | ||
*/ | ||
export default function MentionSuggestionsPopover({ | ||
suggestions, | ||
onSelectSuggestion, | ||
highlightedSuggestion, | ||
...popoverProps | ||
}: MentionSuggestionsPopoverProps) { | ||
return ( | ||
<Popover {...popoverProps} classes="p-1"> | ||
<ul | ||
className="flex-col gap-y-0.5" | ||
role="listbox" | ||
aria-orientation="vertical" | ||
> | ||
{suggestions.map((s, index) => ( | ||
// These options are indirectly handled via keyboard event | ||
// handlers in the textarea, hence, we don't want to add keyboard | ||
// event handlers here, but we want to handle click events. | ||
// eslint-disable-next-line jsx-a11y/click-events-have-key-events | ||
<li | ||
key={s.username} | ||
className={classnames( | ||
'flex justify-between items-center', | ||
'rounded p-2 hover:bg-grey-2', | ||
{ | ||
'bg-grey-2': highlightedSuggestion === index, | ||
}, | ||
)} | ||
onClick={e => { | ||
e.stopPropagation(); | ||
onSelectSuggestion(s); | ||
}} | ||
role="option" | ||
aria-selected={highlightedSuggestion === index} | ||
> | ||
<span className="truncate">{s.username}</span> | ||
<span className="text-color-text-light">{s.displayName}</span> | ||
</li> | ||
))} | ||
{suggestions.length === 0 && ( | ||
<li className="italic p-2" data-testid="suggestions-fallback"> | ||
No matches. You can still write the username | ||
</li> | ||
)} | ||
</ul> | ||
</Popover> | ||
); | ||
} |
Oops, something went wrong.