From c2750d51ea167eee51f1d081d31b01ac562e4bc7 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 12 Oct 2026 23:50:13 -0400 Subject: [PATCH] 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 --- .../IconPicker/components/EmojiPanel.tsx | 3 ++ .../IconPicker/components/GridTemplate.tsx | 38 ++++++++++++++++++- .../IconPicker/components/IconButton.tsx | 5 +++ .../IconPicker/components/IconPanel.tsx | 3 ++ app/components/IconPicker/index.tsx | 9 ++++- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/app/components/IconPicker/components/EmojiPanel.tsx b/app/components/IconPicker/components/EmojiPanel.tsx index fc6cd16521..800d826db5 100644 --- a/app/components/IconPicker/components/EmojiPanel.tsx +++ b/app/components/IconPicker/components/EmojiPanel.tsx @@ -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} diff --git a/app/components/IconPicker/components/GridTemplate.tsx b/app/components/IconPicker/components/GridTemplate.tsx index 2d0238a9b2..486e41ca05 100644 --- a/app/components/IconPicker/components/GridTemplate.tsx +++ b/app/components/IconPicker/components/GridTemplate.tsx @@ -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 ( 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 ( 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; diff --git a/app/components/IconPicker/components/IconButton.tsx b/app/components/IconPicker/components/IconButton.tsx index 0f662f8724..666fdc8db9 100644 --- a/app/components/IconPicker/components/IconButton.tsx +++ b/app/components/IconPicker/components/IconButton.tsx @@ -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; diff --git a/app/components/IconPicker/components/IconPanel.tsx b/app/components/IconPicker/components/IconPanel.tsx index 56f34016e7..f9eb4f78b6 100644 --- a/app/components/IconPicker/components/IconPanel.tsx +++ b/app/components/IconPicker/components/IconPanel.tsx @@ -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} diff --git a/app/components/IconPicker/index.tsx b/app/components/IconPicker/index.tsx index 8799a558fd..b3a22c741f 100644 --- a/app/components/IconPicker/index.tsx +++ b/app/components/IconPicker/index.tsx @@ -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 = ({