Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect iPhone iOS Dark Mode w/ React Native

Tags:

react-native

I know this is possible using Swift:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if traitCollection.userInterfaceStyle == .light {
        print("Light mode")
    } else {
        print("Dark mode")
    }
}

However, is there a way of doing this with JavaScript ?

like image 739
joaumg Avatar asked Jun 29 '19 23:06

joaumg


People also ask

How do you check dark mode in react native?

We watch the prefers-color-scheme CSS media feature to detect dark mode on the system setting level or browser setting level: window. matchMedia('(prefers-color-scheme: dark)') . addEventListener('change', event => { const colorScheme = event.

How do I know if dark mode is enabled IOS?

Dark mode can be detected by using the userInterfaceStyle property on the current trait collection. When it's set to dark you know that the current appearance is set to dark.


2 Answers

You don't need react-native-dark-mode since 0.62.2. You can use

import { Appearance } from 'react-native'
...
Appearance.getColorScheme() === 'dark'

Here's a commit that adds it: https://github.com/facebook/react-native/commit/63fa3f21c5ab308def450bffb22054241a8842ef

like image 200
TheRusskiy Avatar answered Oct 07 '22 21:10

TheRusskiy


Yes...

https://www.npmjs.com/package/react-native-dark-mode

OR

https://github.com/codemotionapps/react-native-dark-mode

import { useDarkMode } from 'react-native-dark-mode'

function Component() {
  const isDarkMode = useDarkMode()
  return <View style={{ backgroundColor: isDarkMode ? 'black' : 'white' }} />
}
like image 38
Marco Jr Avatar answered Oct 07 '22 19:10

Marco Jr