fix: Highlight the current selection in the icon picker (#14059)

* fix: Highlight the current selection in the icon picker

* fix: Highlight the selected icon in only one category
This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-12 23:50:13 -04:00
1 parent 4f4fdbaf36
commit c2750d51ea
5 files changed
+55 -3

No files matched your search

@@ -26,6 +26,7 @@ const GRID_HEIGHT = 410;
type Props = {
panelWidth: number;
icon?: string | null;
query: string;
panelActive: boolean;
height?: number;
@@ -35,6 +36,7 @@ type Props = {
const EmojiPanel = ({
panelWidth,
icon,
query,
panelActive,
onEmojiChange,
@@ -188,6 +190,7 @@ const EmojiPanel = ({
width={panelWidth}
height={height - 48 - PREVIEW_HEIGHT}
data={templateData}
selected={icon}
onIconSelect={handleEmojiSelection}
onIconActive={handleEmojiActive}
onOverflowChange={setHasMoreBelow}
@@ -50,6 +50,8 @@ type Props = {
height: number;
/** Data to be displayed in the grid */
data: DataNode[];
/** Value of the currently selected icon or emoji */
selected?: string | null;
/** Content to display when search results are empty */
empty?: React.ReactNode;
/** Callback when an icon is selected */
@@ -66,6 +68,7 @@ function GridTemplate({
width,
height,
data,
selected,
empty,
onIconSelect,
onIconActive,
@@ -78,6 +81,26 @@ function GridTemplate({
// 24px padding for the Grid Container
const itemsPerRow = Math.max(1, Math.floor((width - 24) / buttonSize));
const [lastClicked, setLastClicked] = React.useState<{
category: DataNode["category"];
value: string;
}>();
// Highlight the selection in only one category: the one it was clicked in,
// otherwise the first category that contains it.
const containsSelected = (node: DataNode) =>
node.icons.some((icon) => valueOf(icon) === selected);
const selectedCategory = selected
? (
data.find(
(node) =>
lastClicked?.value === selected &&
node.category === lastClicked.category &&
containsSelected(node)
) ?? data.find(containsSelected)
)?.category
: undefined;
const gridItems = compact(
data.flatMap((node) => {
const category = (
@@ -100,12 +123,19 @@ function GridTemplate({
const items = node.icons.map((item) => {
const handleActive = () => onIconActive?.(item);
const handleSelect = (id: string, value: string) => {
setLastClicked({ category: node.category, value });
onIconSelect({ id, value });
};
const isSelected =
node.category === selectedCategory && valueOf(item) === selected;
if (item.type === IconType.SVG) {
return (
<IconButton
key={item.name}
onClick={() => onIconSelect({ id: item.name, value: item.name })}
onClick={() => handleSelect(item.name, item.name)}
aria-current={isSelected ? "true" : undefined}
onMouseEnter={handleActive}
onFocus={handleActive}
style={{ "--delay": `${item.delay}ms` } as React.CSSProperties}
@@ -124,7 +154,8 @@ function GridTemplate({
return (
<IconButton
key={item.id}
onClick={() => onIconSelect({ id: item.id, value: item.value })}
onClick={() => handleSelect(item.id, item.value)}
aria-current={isSelected ? "true" : undefined}
onMouseEnter={handleActive}
onFocus={handleActive}
>
@@ -161,6 +192,9 @@ function GridTemplate({
);
}
const valueOf = (icon: IconNode) =>
icon.type === IconType.SVG ? icon.name : icon.value;
const CategoryName = styled(Text)`
grid-column: 1 / -1;
padding-left: 6px;
@@ -11,6 +11,11 @@ export const IconButton = styled(NudeButton)<{ delay?: number }>`
background: ${s("listItemHoverBackground")};
}
&[aria-current="true"] {
background: ${s("listItemHoverBackground")};
box-shadow: inset 0 0 0 1px ${s("inputBorderFocused")};
}
@media (max-width: ${breakpoints.tablet - 1}px) {
width: 40px;
height: 40px;
@@ -24,6 +24,7 @@ const GRID_HEIGHT = 314 - PREVIEW_HEIGHT;
type Props = {
panelWidth: number;
initial: string;
icon: string | null;
color: string;
query: string;
panelActive: boolean;
@@ -35,6 +36,7 @@ type Props = {
const IconPanel = ({
panelWidth,
initial,
icon,
color,
query,
panelActive,
@@ -153,6 +155,7 @@ const IconPanel = ({
width={panelWidth}
height={GRID_HEIGHT}
data={templateData}
selected={icon}
onIconSelect={handleIconSelection}
onIconActive={handleIconActive}
onOverflowChange={setHasMoreBelow}
+8 -1
View File
@@ -74,7 +74,9 @@ const IconPicker = ({
const iconType = determineIconType(icon);
const defaultTab = React.useMemo(
() =>
iconType === IconType.Emoji ? TAB_NAMES["Emoji"] : TAB_NAMES["Icon"],
iconType === IconType.Emoji || iconType === IconType.Custom
? TAB_NAMES["Emoji"]
: TAB_NAMES["Icon"],
[iconType]
);
@@ -153,6 +155,7 @@ const IconPicker = ({
<Content
open={open}
activeTab={activeTab}
icon={icon}
iconColor={chosenColor}
iconInitial={initial ?? ""}
query={query}
@@ -208,6 +211,7 @@ const IconPicker = ({
type ContentProps = {
open: boolean;
activeTab: TabName;
icon: string | null;
query: string;
iconColor: string;
iconInitial: string;
@@ -223,6 +227,7 @@ type ContentProps = {
const Content = ({
open,
activeTab,
icon,
iconColor,
iconInitial,
query,
@@ -267,6 +272,7 @@ const Content = ({
<IconPanel
panelWidth={panelWidth}
initial={iconInitial}
icon={icon}
color={iconColor}
query={query}
panelActive={open && activeTab === TAB_NAMES["Icon"]}
@@ -278,6 +284,7 @@ const Content = ({
<StyledTabContent value={TAB_NAMES["Emoji"]}>
<EmojiPanel
panelWidth={panelWidth}
icon={icon}
query={query}
panelActive={open && activeTab === TAB_NAMES["Emoji"]}
onEmojiChange={onIconChange}