From 7d6563905dee2de70fb158d78f5e82333830ecdd Mon Sep 17 00:00:00 2001 From: Arjan Zuidema Date: Fri, 8 Sep 2023 15:47:35 +0200 Subject: [PATCH] chore(example): Add support for dark mode (Fabric only) (#534) --- FabricExample/js/ExampleContainer.tsx | 6 ++- FabricExample/js/GetAlbumsExample.tsx | 28 +++++++++-- .../js/GetPhotosPerformanceExample.tsx | 48 +++++++++++++++---- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/FabricExample/js/ExampleContainer.tsx b/FabricExample/js/ExampleContainer.tsx index e518911..ae05815 100644 --- a/FabricExample/js/ExampleContainer.tsx +++ b/FabricExample/js/ExampleContainer.tsx @@ -7,6 +7,7 @@ import { Button, Modal, TouchableWithoutFeedback, + Appearance, } from 'react-native'; // @ts-ignore: CameraRollExample has no typings in same folder import CameraRollExample from './CameraRollExample'; @@ -98,13 +99,14 @@ export default class ExamplesContainer extends Component { const styles = StyleSheet.create({ modalScrim: { flex: 1, - backgroundColor: '#00000080', + backgroundColor: + Appearance.getColorScheme() === 'light' ? '#00000080' : '#ffffff80', }, flex1: { flex: 1, }, modalInner: { margin: 20, - backgroundColor: '#fff', + backgroundColor: Appearance.getColorScheme() === 'light' ? '#fff' : '#000', }, }); diff --git a/FabricExample/js/GetAlbumsExample.tsx b/FabricExample/js/GetAlbumsExample.tsx index d86c2dd..00c1028 100644 --- a/FabricExample/js/GetAlbumsExample.tsx +++ b/FabricExample/js/GetAlbumsExample.tsx @@ -6,6 +6,7 @@ import { Text, TextInput, Keyboard, + Appearance, } from 'react-native'; import {CameraRoll} from '@react-native-camera-roll/camera-roll'; import type { @@ -14,6 +15,7 @@ import type { } from '@react-native-camera-roll/camera-roll'; interface State { + colorScheme: 'light' | 'dark'; fetchingAlbums: boolean; timeTakenMillis: number | null; output: Album[] | null; @@ -28,6 +30,7 @@ export default class GetPhotosPerformanceExample extends React.PureComponent< State > { state: State = { + colorScheme: Appearance.getColorScheme() === 'light' ? 'light' : 'dark', fetchingAlbums: false, timeTakenMillis: null, output: null, @@ -47,27 +50,44 @@ export default class GetPhotosPerformanceExample extends React.PureComponent< }); }; + componentDidMount(): void { + Appearance.addChangeListener(({colorScheme}) => { + this.setState({colorScheme: colorScheme === 'light' ? 'light' : 'dark'}); + }); + } + + get styles() { + return { + container: { + backgroundColor: this.state.colorScheme === 'light' ? '#fff' : '#000', + }, + text: { + color: this.state.colorScheme === 'light' ? '#000' : '#fff', + }, + }; + } + render() { const {fetchingAlbums, timeTakenMillis, output} = this.state; return ( - +