Skip to content

Commit

Permalink
fix(mobile): icons in Modal (#10637)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodonisko committed Jan 15, 2024
1 parent be06d91 commit 11b09dc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions suite-common/icons/src/components/CryptoIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TokenAddress } from '@suite-common/wallet-types';

import { CryptoIconName, cryptoIcons } from '../icons';
import { genericTokenIcon, TokenIconName, tokenIcons } from '../tokenIcons';
import { useRerenderOnAppStateChange } from '../useRerenderOnAppState';

export type CoinSymbol = CryptoIconName | TokenAddress;

Expand All @@ -30,6 +31,8 @@ const getIconFile = (symbol: CoinSymbol) => {
};

export const CryptoIcon = ({ symbol, size = 'small' }: CryptoIconProps) => {
useRerenderOnAppStateChange();

const iconFile = getIconFile(symbol);
const svg = useSVG(iconFile);
const sizeNumber = cryptoIconSizes[size];
Expand Down
3 changes: 3 additions & 0 deletions suite-common/icons/src/components/FlagIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Canvas, ImageSVG, useSVG } from '@shopify/react-native-skia';

import { FlagIconName, flagIcons } from '../icons';
import { useRerenderOnAppStateChange } from '../useRerenderOnAppState';

type FlagIconProps = {
name: FlagIconName;
Expand All @@ -16,6 +17,8 @@ const flagIconSizes = {
type FlagIconSize = keyof typeof flagIconSizes;

export const FlagIcon = ({ name, size = 'medium' }: FlagIconProps) => {
useRerenderOnAppStateChange();

const svg = useSVG(flagIcons[name]);
const sizeNumber = flagIconSizes[size];
return (
Expand Down
5 changes: 4 additions & 1 deletion suite-common/icons/src/components/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useNativeStyles } from '@trezor/styles';
import { Color, Colors, CSSColor } from '@trezor/theme';

import { IconName, icons } from '../icons';
import { useRerenderOnAppStateChange } from '../useRerenderOnAppState';

export type IconColor = 'svgSource' | Color | CSSColor | SharedValue<CSSColor>;

Expand Down Expand Up @@ -34,7 +35,7 @@ const isReanimatedSharedValue = (value: IconColor): value is SharedValue<CSSColo
return typeof value === 'object' && 'value' in value;
};

export function isCSSColor(value: any): value is CSSColor {
function isCSSColor(value: any): value is CSSColor {
'worklet';

return (
Expand All @@ -60,6 +61,8 @@ const getColorCode = (color: Exclude<IconColor, 'svgSource'>, themeColors: Color
};

export const Icon = ({ name, customSize, size = 'large', color = 'iconDefault' }: IconProps) => {
useRerenderOnAppStateChange();

const svg = useSVG(icons[name]);
const {
utils: { colors },
Expand Down
4 changes: 4 additions & 0 deletions suite-common/icons/src/useRerenderOnAppState.ios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Hotfix is not needed for iOS.
*/
export const useRerenderOnAppStateChange = () => {};
21 changes: 21 additions & 0 deletions suite-common/icons/src/useRerenderOnAppState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useEffect, useState } from 'react';
import { AppState } from 'react-native';

/**
* It's necessary because Skia Android bug when Icon components are used in Modal component.
* After app is suspended to background and then resumed, icons are not rendered.
* This is a workaround for this issue.
* @see https://github.com/Shopify/react-native-skia/issues/2135
*/
export const useRerenderOnAppStateChange = () => {
const [_, setRerender] = useState(0);

useEffect(() => {
const handleChange = () => {
setRerender(prev => prev + 1);
};
const subscription = AppState.addEventListener('change', handleChange);

return () => subscription.remove();
}, []);
};

0 comments on commit 11b09dc

Please sign in to comment.