diff --git a/src/components/icons/nano.test.tsx b/src/components/icons/nano.test.tsx new file mode 100644 index 000000000..3ee0f0bf2 --- /dev/null +++ b/src/components/icons/nano.test.tsx @@ -0,0 +1,80 @@ +import {act, render, screen} from '@testing-library/react-native' + +import {glyphMaps} from '#/components/icons/glyphMaps' +import {createSinglePathSVG} from '#/components/icons/TEMPLATE' + +type IntegrityListener = ( + issue: {family: string; fontFamily: string}, + status: 'found' | 'resolved', +) => void + +let mockIntegrityListener: IntegrityListener | undefined + +jest.mock('#/alf', () => ({ + tokens: {gradients: {}}, + useTheme: () => ({palette: {primary_500: '#0085ff'}}), +})) +jest.mock('react-native-nano-icons', () => { + const {createElement} = jest.requireActual('react') + const {Text} = + jest.requireActual('react-native') + return { + createNanoIconSet: () => + function MockNanoIcon({name}: {name: string}) { + return createElement(Text, null, `glyph:${name}`) + }, + addFontIntegrityListener: (listener: IntegrityListener) => { + mockIntegrityListener = listener + return () => {} + }, + } +}) + +const glyph = 'Accessibility_Stroke2_Corner2_Rounded' +const Icon = createSinglePathSVG({path: 'M0 0h24v24H0z', glyph}) + +/* + * Icons are hidden from screen readers, so every query has to opt into hidden + * elements. + */ +const hidden = {includeHiddenElements: true} + +function reportFont(family: string, status: 'found' | 'resolved') { + act(() => { + mockIntegrityListener!({family, fontFamily: 'icons-test'}, status) + }) +} + +beforeEach(() => { + jest.spyOn(console, 'warn').mockImplementation(() => {}) +}) + +afterEach(() => { + jest.restoreAllMocks() +}) + +describe('withNanoGlyph', () => { + it('renders the SVG icon while its font is missing from the binary', () => { + render() + expect(screen.getByText(`glyph:${glyph}`, hidden)).toBeTruthy() + + reportFont(glyphMaps.ui.m.f, 'found') + expect(screen.queryByText(`glyph:${glyph}`, hidden)).toBeNull() + expect(screen.getByTestId('icon', hidden)).toBeTruthy() + expect(console.warn).toHaveBeenCalledWith( + expect.stringContaining('render with react-native-svg'), + ) + + reportFont(glyphMaps.ui.m.f, 'resolved') + expect(screen.getByText(`glyph:${glyph}`, hidden)).toBeTruthy() + }) + + it('keeps the glyph when a different font is missing', () => { + render() + + reportFont(glyphMaps.brands.m.f, 'found') + expect(screen.getByText(`glyph:${glyph}`, hidden)).toBeTruthy() + + reportFont(glyphMaps.brands.m.f, 'resolved') + }) +}) diff --git a/src/components/icons/nano.tsx b/src/components/icons/nano.tsx index c2e59debd..808aca66e 100644 --- a/src/components/icons/nano.tsx +++ b/src/components/icons/nano.tsx @@ -1,11 +1,14 @@ -import {forwardRef} from 'react' +import {forwardRef, useSyncExternalStore} from 'react' import { type ColorValue, type StyleProp, StyleSheet, type ViewStyle, } from 'react-native' -import {createNanoIconSet} from 'react-native-nano-icons' +import { + addFontIntegrityListener, + createNanoIconSet, +} from 'react-native-nano-icons' import type Svg from 'react-native-svg' import {useTheme} from '#/alf' @@ -16,6 +19,7 @@ import { type NanoGlyphName, } from '#/components/icons/glyphMaps' import {type IconWithSvgMeta} from '#/components/icons/TEMPLATE' +import {IS_INTERNAL} from '#/env' /* * Each set is typed by its own glyph names, so widen the components to accept * any name. The native renderers put `style` in an array, so a style array is @@ -44,6 +48,59 @@ const iconSets = { */ type GlyphEntry = [number, [number, string][]] +/** + * Icon font families that nano-icons reports missing from the native binary, + * so their glyphs would render blank. The binary was built with different + * icon fonts than the running JS: a dev client built from an older commit, or + * a TestFlight build running a per-PR OTA update. Those updates only compare + * the fingerprint against the PR's base commit, but install on any TestFlight + * binary with the same app version. + */ +const missingFonts = new Set() +const missingFontListeners = new Set<() => void>() + +if (IS_INTERNAL) { + addFontIntegrityListener((issue, status) => { + if (status === 'found') { + missingFonts.add(issue.family) + console.warn( + `Icon font "${issue.fontFamily}" doesn't match this dev client, so its icons render with react-native-svg. ` + + 'Rebuild the dev client to render them as glyphs.', + ) + } else { + missingFonts.delete(issue.family) + } + for (const listener of missingFontListeners) listener() + }) +} + +function subscribeToMissingFonts(listener: () => void) { + missingFontListeners.add(listener) + return () => { + missingFontListeners.delete(listener) + } +} + +function useIsFontMissingInternal(family: string) { + return useSyncExternalStore(subscribeToMissingFonts, () => + missingFonts.has(family), + ) +} + +function useIsFontMissingProduction(_family: string) { + return false +} + +/** + * Whether an icon font is missing from the native binary, so that its icons + * should render with react-native-svg. Always false in production, where + * `fingerprint.config.js` makes any font change a native build, which also + * keeps the subscription out of production renders. + */ +const useIsFontMissing = IS_INTERNAL + ? useIsFontMissingInternal + : useIsFontMissingProduction + const reportedFallbacks = new Set() /** @@ -70,7 +127,8 @@ function warnFallback(name: string, prop: string) { * cannot reproduce - a `gradient`, a `ref` (which must point at the `Svg`), a * `height` that differs from the width, or any other SVG prop - renders the * SVG icon instead, so the prop contract and the `svgPaths` metadata read by - * `@bsky.app/peek-menu` stay exactly those of the SVG icon. + * `@bsky.app/peek-menu` stay exactly those of the SVG icon. In development + * and TestFlight, so does every icon of a font that the binary lacks. */ export function withNanoGlyph( SvgIcon: IconWithSvgMeta, @@ -93,11 +151,13 @@ export function withNanoGlyph( set => name in glyphMaps[set].i, )! const NanoIconSet = iconSets[iconSet] + const fontFamily = glyphMaps[iconSet].m.f const glyphMap = glyphMaps[iconSet].i as unknown as Record const layers = glyphMap[name][1].map(([, color]) => color) const Icon = forwardRef(function NanoIcon(props, ref) { const t = useTheme() + const fontMissing = useIsFontMissing(fontFamily) const { fill, size, @@ -120,7 +180,7 @@ export function withNanoGlyph( */ const resolvedSize = Number(size ? sizes[size] : width || sizes.md) - if (gradient) { + if (gradient || fontMissing) { return } const unsupported =