@@ -14,11 +14,14 @@ import type {ColorValue} from 'react-native';
1414import RNTesterText from '../../components/RNTesterText' ;
1515import React from 'react' ;
1616import {
17+ Appearance ,
18+ Button ,
1719 DynamicColorIOS ,
1820 Platform ,
1921 PlatformColor ,
2022 StyleSheet ,
2123 View ,
24+ useColorScheme ,
2225} from 'react-native' ;
2326
2427function 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+
318363const 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
331377exports . 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