Skip to content

Commit f420d03

Browse files
authored
Design Kits issues (#2836)
* Add proper cache resolver to our memoize generatePalette method * Fix tests * Fix issue with undefined value being destructed * Regenerate TabBar when colors change * Revert fix, move to another PR * Fix PR comment
1 parent c419edd commit f420d03

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/components/tabController/TabBar.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {Colors, Spacings, Typography} from '../../style';
1616
import FadedScrollView from '../fadedScrollView';
1717
import {FaderProps} from '../fader';
1818
import useScrollToItem from './useScrollToItem';
19-
import {orientations} from '../../commons/Constants';
2019
import {useDidUpdate} from 'hooks';
2120

2221
const FIX_RTL = Constants.isRTL && Constants.isAndroid;
@@ -164,7 +163,7 @@ const TabBar = (props: Props) => {
164163
} = props;
165164

166165
const tabBar = useRef<typeof FadedScrollView>();
167-
const [key, setKey] = useState<orientations>(Constants.orientation);
166+
const [key, setKey] = useState<string>(generateKey(Constants.orientation, labelColor, selectedLabelColor));
168167
const context = useContext(TabBarContext);
169168
const {items: contextItems, currentPage, targetPage, containerWidth: contextContainerWidth} = context;
170169
const containerWidth: number = useMemo(() => {
@@ -286,10 +285,14 @@ const TabBar = (props: Props) => {
286285
focusIndex(currentPage.value);
287286
} else {
288287
reset();
289-
setKey(Constants.orientation);
288+
setKey(generateKey(Constants.orientation, labelColor, selectedLabelColor));
290289
}
291290
}, [containerWidth]);
292291

292+
useDidUpdate(() => {
293+
setKey(generateKey(Constants.orientation, labelColor, selectedLabelColor));
294+
}, [labelColor, selectedLabelColor]);
295+
293296
return (
294297
<View style={_containerStyle} key={key} bg-$backgroundElevated>
295298
<FadedScrollView
@@ -365,4 +368,7 @@ const styles = StyleSheet.create({
365368
}
366369
});
367370

371+
const generateKey = (orientation: string, labelColor = '', selectedLabelColor = '') =>
372+
`${orientation}_${labelColor}_${selectedLabelColor}`;
373+
368374
export default asBaseComponent<TabControllerBarProps>(forwardRef<Props>(TabBar));

src/style/colors.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@ export class Colors {
247247
const sliced = tints.slice(0, size);
248248
const adjusted = options?.adjustSaturation && adjustSaturation(sliced, color);
249249
return adjusted || sliced;
250-
});
250+
}, generatePaletteCacheResolver);
251251

252252
defaultOptions = {adjustLightness: true, adjustSaturation: true, addDarkestTints: false, avoidReverseOnDark: false};
253253

254254
generateColorPalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
255255
const _options = {...this.defaultOptions, ...options};
256256
const palette = this.generatePalette(color, _options);
257257
return this.shouldReverseOnDark(_options?.avoidReverseOnDark) ? _.reverse(palette) : palette;
258-
});
258+
}, generatePaletteCacheResolver);
259259

260260
private generateDesignTokens(primaryColor: string, dark?: boolean) {
261261
let colorPalette: string[] = this.generatePalette(primaryColor);
@@ -371,6 +371,10 @@ function threeDigitHexToSix(value: string) {
371371
return value.replace(/./g, '$&$&');
372372
}
373373

374+
function generatePaletteCacheResolver(color: string, options?: GeneratePaletteOptions) {
375+
return `${color}${options ? '_' + JSON.stringify(options) : ''}`;
376+
}
377+
374378
const TypedColors = Colors as ExtendTypeWith<typeof Colors, typeof colorsPalette & typeof DesignTokens>;
375379
const colorObject = new TypedColors();
376380
colorObject.loadColors(colorsPalette);

0 commit comments

Comments
 (0)