From 87ca7f8c1f649f43e4fc6b705a52717f5742e804 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Sat, 10 Oct 2026 09:13:42 +0200 Subject: [PATCH] Fix passing additional styles to icons on web --- src/components/icons/nano.tsx | 142 +++++++++++++++++++++------------- 1 file changed, 88 insertions(+), 54 deletions(-) diff --git a/src/components/icons/nano.tsx b/src/components/icons/nano.tsx index 3784878b8..ad6e71aed 100644 --- a/src/components/icons/nano.tsx +++ b/src/components/icons/nano.tsx @@ -1,7 +1,13 @@ import '#/components/icons/nanoFont' import {forwardRef} from 'react' -import {type ColorValue, StyleSheet} from 'react-native' +import { + type ColorValue, + type StyleProp, + StyleSheet, + View, + type ViewStyle, +} from 'react-native' import {createNanoIconSet} from 'react-native-nano-icons' import type Svg from 'react-native-svg' @@ -9,6 +15,7 @@ import {useTheme} from '#/alf' import {type Props, sizes} from '#/components/icons/common' import {nanoGlyphs} from '#/components/icons/nanoGlyphs' import {type IconWithSvgMeta} from '#/components/icons/TEMPLATE' +import {IS_WEB} from '#/env' import brandsGlyphMap from '../../../assets/nano-icons/nanoicons/icons-brands.glyphmap.json' import communityGlyphMap from '../../../assets/nano-icons/nanoicons/icons-community.glyphmap.json' import uiGlyphMap from '../../../assets/nano-icons/nanoicons/icons-ui.glyphmap.json' @@ -26,7 +33,8 @@ const glyphMaps = { /* * Each set is typed by its own glyph names, which `nanoGlyphs` is generated - * from, so widen the components to accept any name. + * from, so widen the components to accept any name. The native renderers put + * `style` in an array, so a style array is fine too. */ const iconSets = { ui: createNanoIconSet(uiGlyphMap), @@ -35,8 +43,12 @@ const iconSets = { } as unknown as Record< string, React.ComponentType< - Omit>, 'name'> & { + Omit< + React.ComponentProps>, + 'name' | 'style' + > & { name: string + style?: StyleProp } > > @@ -132,59 +144,81 @@ export function withNanoGlyph( StyleSheet.flatten(style)?.color || t.palette.primary_500) as ColorValue - return ( - /* - * The a11y rule wants an accessibilityHint alongside accessibilityLabel, - * but a hint would be exactly wrong here: the label is empty precisely - * to remove this glyph from the accessibility tree, and a hint would - * put content back into it. See the accessibilityLabel comment below. - */ - // oxlint-disable-next-line react-native-a11y/has-accessibility-hint - - color === 'currentColor' ? resolvedFill : color, - ) - : resolvedFill - } - /* - * Icons historically accept text styles (to inherit `color`), but the - * glyph container is a View. Only layout props are meaningful here. - */ - style={StyleSheet.flatten(style) ?? undefined} - testID={testID} - /* - * The SVG icons are sized in raw points and never scaled with the - * system font setting, so opt out to keep layout identical. - */ - allowFontScaling={false} + /* + * Icons historically accept text styles (to inherit `color`), but the + * glyph container is a View. Only layout props are meaningful here. + */ + const viewStyle = style as StyleProp + + const icon = + ( /* - * Also matches the SVG icons: these sit inside buttons that carry - * their own label, so the glyph itself must stay invisible to screen - * readers. - * - * The empty label is load-bearing. Nano Icons falls back to - * `accessibilityLabel ?? name`, so without it every glyph is - * announced by its icon name on top of the button's own label - * ("Reply, button" then "reply, image"). `importantForAccessibility` - * alone does not suppress it - the native view still exposes a - * contentDescription. + * The a11y rule wants an accessibilityHint alongside accessibilityLabel, + * but a hint would be exactly wrong here: the label is empty precisely + * to remove this glyph from the accessibility tree, and a hint would + * put content back into it. See the accessibilityLabel comment below. */ - accessibilityLabel="" - accessible={false} - accessibilityElementsHidden - importantForAccessibility="no-hide-descendants" - /> - ) + // oxlint-disable-next-line react-native-a11y/has-accessibility-hint + + color === 'currentColor' ? resolvedFill : color, + ) + : resolvedFill + } + /* + * Passed through unflattened, so that a stable style keeps nano's + * memo comparator effective. + */ + style={IS_WEB ? undefined : viewStyle} + testID={testID} + /* + * The SVG icons are sized in raw points and never scaled with the + * system font setting, so opt out to keep layout identical. + */ + allowFontScaling={false} + /* + * Also matches the SVG icons: these sit inside buttons that carry + * their own label, so the glyph itself must stay invisible to screen + * readers. + * + * The empty label is load-bearing. Nano Icons falls back to + * `accessibilityLabel ?? name`, so without it every glyph is + * announced by its icon name on top of the button's own label + * ("Reply, button" then "reply, image"). `importantForAccessibility` + * alone does not suppress it - the native view still exposes a + * contentDescription. + */ + accessibilityLabel="" + accessible={false} + accessibilityElementsHidden + importantForAccessibility="no-hide-descendants" + /> + ) + + /* + * On web, nano-icons spreads `style` into the inline style of a + * without passing it through react-native-web, so the browser drops + * anything in React Native syntax, e.g. `transform: [{rotate: '90deg'}]`. + * Apply the style to a View instead, sized like the SVG icon. + */ + if (IS_WEB) { + return ( + + {icon} + + ) + } + return icon }) as IconWithSvgMeta Icon.svgPaths = SvgIcon.svgPaths -- knotmirror 0.0.1