Skip to content

Commit 8ed0e1e

Browse files
andrewdacenkometa-codesync[bot]
authored andcommitted
Add Platform Color example (#53908)
Summary: Pull Request resolved: #53908 Changelog: [Internal] Add an example to show how Platform Color can react to changes in appearance. This currently works on iOS for all colors, but for Android we need to "reset" desired tree with `key`. Reviewed By: zeyap Differential Revision: D82751014 fbshipit-source-id: 3e4b2b3ddff8c003ca835d6f26dbf9f7e82d413d
1 parent 064fd59 commit 8ed0e1e

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

packages/rn-tester/js/examples/PlatformColor/PlatformColorExample.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ import type {ColorValue} from 'react-native';
1414
import RNTesterText from '../../components/RNTesterText';
1515
import React from 'react';
1616
import {
17+
Appearance,
18+
Button,
1719
DynamicColorIOS,
1820
Platform,
1921
PlatformColor,
2022
StyleSheet,
2123
View,
24+
useColorScheme,
2225
} from 'react-native';
2326

2427
function PlatformColorsExample() {
@@ -315,6 +318,48 @@ function VariantColorsExample() {
315318
);
316319
}
317320

321+
function ReactsToAppearanceChangesExample() {
322+
const theme = useColorScheme();
323+
const key = Platform.select({
324+
android: theme ?? '',
325+
default: undefined,
326+
});
327+
328+
return (
329+
// using a key here forces the component to unmount and remount
330+
// which is necessary to trigger the appearance change
331+
<View style={styles.column} key={key}>
332+
<View style={styles.row}>
333+
<RNTesterText style={styles.labelCell}>
334+
{Platform.select({
335+
ios: "DynamicColorIOS({light: 'red', dark: 'blue'})",
336+
android: "PlatformColor('?attr/colorAccent')",
337+
default: 'Unexpected Platform.OS: ' + Platform.OS,
338+
})}
339+
</RNTesterText>
340+
<View
341+
style={{
342+
...styles.colorCell,
343+
backgroundColor:
344+
Platform.OS === 'ios'
345+
? DynamicColorIOS({light: 'red', dark: 'blue'})
346+
: Platform.OS === 'android'
347+
? PlatformColor('?attr/colorAccent')
348+
: 'red',
349+
}}
350+
/>
351+
</View>
352+
<View style={styles.separator} />
353+
<Button
354+
title="Change Appearance"
355+
onPress={() => {
356+
Appearance.setColorScheme(theme === 'dark' ? 'light' : 'dark');
357+
}}
358+
/>
359+
</View>
360+
);
361+
}
362+
318363
const styles = StyleSheet.create({
319364
column: {flex: 1, flexDirection: 'column'},
320365
row: {flex: 0.75, flexDirection: 'row'},
@@ -326,6 +371,7 @@ const styles = StyleSheet.create({
326371
}),
327372
},
328373
colorCell: {flex: 0.25, alignItems: 'stretch'},
374+
separator: {height: 8},
329375
});
330376

331377
exports.title = 'PlatformColor';
@@ -358,4 +404,10 @@ exports.examples = [
358404
return <VariantColorsExample />;
359405
},
360406
},
407+
{
408+
title: 'Reacts to Appearance Changes',
409+
render(): React.MixedElement {
410+
return <ReactsToAppearanceChangesExample />;
411+
},
412+
},
361413
] as Array<RNTesterModuleExample>;

0 commit comments

Comments
 (0)