diff --git a/packages/compose/src/hooks/use-preferred-color-scheme/index.android.js b/packages/compose/src/hooks/use-preferred-color-scheme/index.android.js new file mode 100644 index 00000000000000..6d81eeca5a0852 --- /dev/null +++ b/packages/compose/src/hooks/use-preferred-color-scheme/index.android.js @@ -0,0 +1,33 @@ +/** + * External dependencies + */ +import { + subscribePreferredColorScheme, + isInitialColorSchemeDark, +} from 'react-native-gutenberg-bridge'; +/** + * WordPress dependencies + */ +import { useState, useEffect } from '@wordpress/element'; + +/** + * Returns the color scheme value when it changes. Possible values: [ 'light', 'dark' ] + * + * @return {string} return current color scheme. + */ +const usePreferredColorScheme = function() { + const [ currentColorScheme, setCurrentColorScheme ] = useState( + isInitialColorSchemeDark ? 'dark' : 'light' + ); + useEffect( () => { + subscribePreferredColorScheme( ( { isPreferredColorSchemeDark } ) => { + const colorScheme = isPreferredColorSchemeDark ? 'dark' : 'light'; + if ( colorScheme !== currentColorScheme ) { + setCurrentColorScheme( colorScheme ); + } + } ); + } ); + return currentColorScheme; +}; + +export default usePreferredColorScheme; diff --git a/packages/compose/src/hooks/use-preferred-color-scheme/index.native.js b/packages/compose/src/hooks/use-preferred-color-scheme/index.ios.js similarity index 100% rename from packages/compose/src/hooks/use-preferred-color-scheme/index.native.js rename to packages/compose/src/hooks/use-preferred-color-scheme/index.ios.js diff --git a/test/native/setup.js b/test/native/setup.js index fe55851be4827a..7d656b41eafb42 100644 --- a/test/native/setup.js +++ b/test/native/setup.js @@ -14,6 +14,7 @@ jest.mock( 'react-native-gutenberg-bridge', () => { subscribeUpdateHtml: jest.fn(), subscribeMediaAppend: jest.fn(), subscribeAndroidModalClosed: jest.fn(), + subscribePreferredColorScheme: () => 'light', editorDidMount: jest.fn(), editorDidAutosave: jest.fn(), subscribeMediaUpload: jest.fn(),