Skip to content

Commit 8720eac

Browse files
committed
fix: dynamic theme broken deep merge when using PlatformColor
* useInternalTheme now uses a custom deep merge that keeps PlatformColor / DynamicColorIOS / Android resource sentinels intact (upstream deepmerge corrupted them). No other behavioral change.
1 parent de574b5 commit 8720eac

2 files changed

Lines changed: 157 additions & 1 deletion

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { isPlatformColorSentinel, safeMerge } from '../provider';
2+
3+
describe('isPlatformColorSentinel', () => {
4+
it('detects iOS PlatformColor (semantic)', () => {
5+
expect(isPlatformColorSentinel({ semantic: ['label'] })).toBe(true);
6+
});
7+
8+
it('detects iOS DynamicColorIOS (dynamic)', () => {
9+
expect(
10+
isPlatformColorSentinel({ dynamic: { light: '#fff', dark: '#000' } })
11+
).toBe(true);
12+
});
13+
14+
it('detects Android PlatformColor (resource_paths)', () => {
15+
expect(
16+
isPlatformColorSentinel({ resource_paths: ['@android:color/black'] })
17+
).toBe(true);
18+
});
19+
20+
it('rejects plain objects, primitives, null, and arrays', () => {
21+
expect(isPlatformColorSentinel({ primary: '#fff' })).toBe(false);
22+
expect(isPlatformColorSentinel('#fff')).toBe(false);
23+
expect(isPlatformColorSentinel(null)).toBe(false);
24+
expect(isPlatformColorSentinel(undefined)).toBe(false);
25+
expect(isPlatformColorSentinel([1, 2, 3])).toBe(false);
26+
});
27+
});
28+
29+
describe('safeMerge', () => {
30+
it('deep-merges plain objects, overrides win at leaves', () => {
31+
const base = { a: 1, nested: { x: 1, y: 2 } };
32+
const overrides = { nested: { y: 20, z: 30 } };
33+
34+
expect(safeMerge(base, overrides)).toEqual({
35+
a: 1,
36+
nested: { x: 1, y: 20, z: 30 },
37+
});
38+
});
39+
40+
it('returns a new object reference (does not mutate base)', () => {
41+
const base = { nested: { x: 1 } };
42+
const overrides = { nested: { y: 2 } };
43+
const result = safeMerge(base, overrides);
44+
45+
expect(result).not.toBe(base);
46+
expect(result.nested).not.toBe(base.nested);
47+
expect(base).toEqual({ nested: { x: 1 } });
48+
});
49+
50+
it('falls back to base when overrides is null/undefined', () => {
51+
const base = { a: 1 };
52+
expect(safeMerge(base, null)).toEqual(base);
53+
expect(safeMerge(base, undefined)).toEqual(base);
54+
});
55+
56+
it('replaces arrays instead of merging', () => {
57+
const base = { list: [1, 2, 3] };
58+
const overrides = { list: [9] };
59+
expect(safeMerge(base, overrides)).toEqual({ list: [9] });
60+
});
61+
62+
it('treats iOS semantic sentinel as a leaf (no recursion into array)', () => {
63+
const sentinel = { semantic: ['label'] };
64+
const base = { colors: { primary: '#000' } };
65+
const overrides = { colors: { primary: sentinel } };
66+
67+
const result = safeMerge<typeof base & { colors: { primary: unknown } }>(
68+
base,
69+
overrides
70+
);
71+
expect(result.colors.primary).toBe(sentinel);
72+
});
73+
74+
it('treats DynamicColorIOS sentinel as a leaf', () => {
75+
const sentinel = { dynamic: { light: '#fff', dark: '#000' } };
76+
const base = { colors: { primary: sentinel } };
77+
const overrides = { colors: { primary: '#abc' } };
78+
79+
const result = safeMerge<typeof base & { colors: { primary: unknown } }>(
80+
base,
81+
overrides
82+
);
83+
expect(result.colors.primary).toBe('#abc');
84+
});
85+
86+
it('treats Android resource_paths sentinel as a leaf', () => {
87+
const sentinelBase = { resource_paths: ['@android:color/black'] };
88+
const sentinelOverride = { resource_paths: ['@android:color/white'] };
89+
const base = { colors: { primary: sentinelBase } };
90+
const overrides = { colors: { primary: sentinelOverride } };
91+
92+
const result = safeMerge<typeof base & { colors: { primary: unknown } }>(
93+
base,
94+
overrides
95+
);
96+
expect(result.colors.primary).toBe(sentinelOverride);
97+
});
98+
99+
it('preserves sentinel siblings when merging a colors map', () => {
100+
const sentinel = { semantic: ['label'] };
101+
const base = {
102+
colors: { primary: sentinel, secondary: '#111', tertiary: '#222' },
103+
};
104+
const overrides = { colors: { secondary: '#999' } };
105+
106+
const result = safeMerge<typeof base & { colors: Record<string, unknown> }>(
107+
base,
108+
overrides
109+
);
110+
expect(result.colors.primary).toBe(sentinel);
111+
expect(result.colors.secondary).toBe('#999');
112+
expect(result.colors.tertiary).toBe('#222');
113+
});
114+
});

src/theme/provider.tsx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as React from 'react';
12
import type { ComponentType } from 'react';
23

34
import { $DeepPartial, createTheming } from '@callstack/react-theme-provider';
@@ -17,9 +18,50 @@ export function useTheme<T = Theme>(overrides?: $DeepPartial<T>) {
1718
return useThemeBase<T>(overrides);
1819
}
1920

21+
// Upstream `deepmerge` corrupts PlatformColor objects, so we recurse manually
22+
// and treat sentinels as leaves. Three shapes:
23+
// `semantic` — iOS PlatformColor
24+
// `dynamic` — DynamicColorIOS
25+
// `resource_paths` — Android PlatformColor
26+
export const isPlatformColorSentinel = (v: unknown): boolean =>
27+
!!v &&
28+
typeof v === 'object' &&
29+
('resource_paths' in v || 'semantic' in v || 'dynamic' in v);
30+
31+
export const safeMerge = <T,>(base: T, overrides: unknown): T => {
32+
if (
33+
!base ||
34+
!overrides ||
35+
typeof base !== 'object' ||
36+
typeof overrides !== 'object' ||
37+
Array.isArray(base) ||
38+
Array.isArray(overrides) ||
39+
isPlatformColorSentinel(base) ||
40+
isPlatformColorSentinel(overrides)
41+
) {
42+
// leaf: override wins, fall back to base
43+
return (overrides ?? base) as T;
44+
}
45+
const out: Record<string, unknown> = { ...(base as Record<string, unknown>) };
46+
for (const key of Object.keys(overrides as Record<string, unknown>)) {
47+
out[key] = safeMerge(
48+
(base as Record<string, unknown>)[key],
49+
(overrides as Record<string, unknown>)[key]
50+
);
51+
}
52+
return out as T;
53+
};
54+
55+
/** Memoize `themeOverrides` at the call site; inline object literals defeat the memo. */
2056
export const useInternalTheme = (
2157
themeOverrides: $DeepPartial<Theme> | undefined
22-
) => useThemeBase<Theme>(themeOverrides);
58+
): Theme => {
59+
const theme = useThemeBase<Theme>();
60+
return React.useMemo(
61+
() => (themeOverrides ? safeMerge(theme, themeOverrides) : theme),
62+
[theme, themeOverrides]
63+
);
64+
};
2365

2466
export const withInternalTheme = <Props extends { theme: Theme }, C>(
2567
WrappedComponent: ComponentType<Props & { theme: Theme }> & C

0 commit comments

Comments
 (0)