Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/react-strict-dom/src/native/css/customProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ export function stringContainsVariables(input: string): boolean {
return input.includes('var(');
}

function stringContainsLightDark(input: string): boolean {
return input.includes('light-dark(');
}

const RE_LIGHT_DARK = /^light-dark\(\s*(.+?)\s*,\s*(.+?)\s*\)$/i;

function resolveLightDarkValue(
variableValue: string,
colorScheme: 'light' | 'dark'
) {
const match = RE_LIGHT_DARK.exec(variableValue);

if (match == null) {
warnMsg(`Invalid light-dark syntax: "${variableValue}"`);
return null;
}

const [, lightStr, darkStr] = match;

return colorScheme === 'dark' ? darkStr.trim() : lightStr.trim();
}

function resolveVariableReferenceValue(
propName: string,
variable: CSSVariableReferenceValue,
Expand All @@ -66,7 +88,15 @@ function resolveVariableReferenceValue(
}

if (variableValue != null) {
if (typeof variableValue === 'object' && variableValue.default != null) {
if (
typeof variableValue === 'string' &&
stringContainsLightDark(variableValue)
) {
variableValue = resolveLightDarkValue(variableValue, colorScheme);
} else if (
typeof variableValue === 'object' &&
variableValue.default != null
) {
let defaultValue = variableValue.default;
if (colorScheme === 'dark') {
defaultValue = variableValue['@media (prefers-color-scheme: dark)'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`css.* themes css.createTheme: theme 1`] = `
{
"$$theme": "theme",
"rootColor__id__3": "green",
"rootColor__id__4": "green",
}
`;

Expand All @@ -16,6 +16,7 @@ exports[`css.* themes css.defineConsts: constants 1`] = `

exports[`css.* themes css.defineVars: tokens 1`] = `
{
"lightDarkColor": "var(--lightDarkColor__id__3)",
"rootColor": "var(--rootColor__id__1)",
"themeAwareColor": "var(--themeAwareColor__id__2)",
}
Expand Down
14 changes: 13 additions & 1 deletion packages/react-strict-dom/tests/css/css-themes-test.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe('css.* themes', () => {
themeAwareColor: {
default: 'blue',
'@media (prefers-color-scheme: dark)': 'green'
}
},
lightDarkColor: 'light-dark(blue, green)'
});

expect(tokens).toMatchSnapshot('tokens');
Expand All @@ -54,6 +55,9 @@ describe('css.* themes', () => {
},
themeAwareColor: {
color: tokens.themeAwareColor
},
lightDarkColor: {
color: tokens.lightDarkColor
}
});

Expand All @@ -66,13 +70,21 @@ describe('css.* themes', () => {
root = create(<html.span style={styles.themeAwareColor} />);
});
expect(root.toJSON().props.style.color).toBe('blue');
act(() => {
root = create(<html.span style={styles.lightDarkColor} />);
});
expect(root.toJSON().props.style.color).toBe('blue');

// dark theme
ReactNative.useColorScheme.mockReturnValue('dark');
act(() => {
root = create(<html.span style={styles.themeAwareColor} />);
});
expect(root.toJSON().props.style.color).toBe('green');
act(() => {
root = create(<html.span style={styles.lightDarkColor} />);
});
expect(root.toJSON().props.style.color).toBe('green');
});

test('css.createTheme', () => {
Expand Down