Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Fix color scheme light mode flash when app comes to foreground (#771)

Leendert de Borstd4a39caa22c8
diff --git a/mobile-app/hooks/useColorScheme.ts b/mobile-app/hooks/useColorScheme.ts
index 2d0dd4ae1..1adbe3f04 100644
--- a/mobile-app/hooks/useColorScheme.ts
+++ b/mobile-app/hooks/useColorScheme.ts
@@ -1,11 +1,45 @@
-import { useColorScheme as _useColorScheme } from 'react-native';
+import { Appearance } from 'react-native';
+import React from 'react';
+import _ from 'lodash';
import { Colors } from '@/constants/Colors';
export type ColorSchemeName = 'light' | 'dark';
-// Re-export the basic color scheme hook
-export const useColorScheme = () => {
- return _useColorScheme() as ColorSchemeName;
+/**
+ * Get the color scheme of the device.
+ *
+ * @param delay - The delay in milliseconds between color scheme changes. This is to prevent a bug where
+ * when app goes to background/foreground, the color scheme can return "light" for a split second amongst
+ * many "dark" calls (if theme is set to dark). Throttling the calls to once per 150ms prevents this.
+ *
+ * @returns The color scheme of the device.
+ */
+export const useColorScheme = (delay = 150) => {
+ const [colorScheme, setColorScheme] = React.useState<ColorSchemeName>(
+ Appearance.getColorScheme() as ColorSchemeName
+ );
+ const onColorSchemeChange = React.useCallback(
+ _.throttle(
+ (preferences: { colorScheme: string | null | undefined }) => {
+ if (preferences.colorScheme) {
+ setColorScheme(preferences.colorScheme as ColorSchemeName);
+ }
+ },
+ delay,
+ {
+ leading: false,
+ }
+ ),
+ []
+ );
+ React.useEffect(() => {
+ const subscription = Appearance.addChangeListener(onColorSchemeChange);
+ return () => {
+ onColorSchemeChange.cancel();
+ subscription.remove();
+ };
+ }, []);
+ return colorScheme;
};
// Simple hook that returns the current theme's colors
diff --git a/mobile-app/package-lock.json b/mobile-app/package-lock.json
index b1cba9a95..2c622785a 100644
--- a/mobile-app/package-lock.json
+++ b/mobile-app/package-lock.json
@@ -54,6 +54,7 @@
"@types/argon2-browser": "^1.18.4",
"@types/fbemitter": "^2.0.35",
"@types/jest": "^29.5.12",
+ "@types/lodash": "^4.17.16",
"@types/react": "~18.3.12",
"@types/react-test-renderer": "^18.3.0",
"@types/sql.js": "^1.4.9",
@@ -4706,6 +4707,13 @@
"integrity": "sha512-3stUTaSRtN09PPzVWR6aySD9gNnuymz+WviNHoTb85dKu+BjaV4uBbWWGykBBJkfwPtcNZVfTn2lbX00U+yhpQ==",
"license": "MIT"
},
+ "node_modules/@types/lodash": {
+ "version": "4.17.16",
+ "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.16.tgz",
+ "integrity": "sha512-HX7Em5NYQAXKW+1T+FiuG27NGwzJfCX3s1GjOa7ujxZa52kjJLOr4FUxT+giF6Tgxv1e+/czV/iTtBw27WTU9g==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/@types/node": {
"version": "22.14.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.14.0.tgz",
@@ -6976,6 +6984,17 @@
"node": ">= 0.8"
}
},
+ "node_modules/encoding": {
+ "version": "0.1.13",
+ "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.13.tgz",
+ "integrity": "sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==",
+ "license": "MIT",
+ "optional": true,
+ "peer": true,
+ "dependencies": {
+ "iconv-lite": "^0.6.2"
+ }
+ },
"node_modules/end-of-stream": {
"version": "1.4.4",
"resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz",
@@ -8677,7 +8696,7 @@
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
- "dev": true,
+ "devOptional": true,
"license": "MIT",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
@@ -13538,7 +13557,7 @@
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
- "dev": true,
+ "devOptional": true,
"license": "MIT"
},
"node_modules/sax": {
diff --git a/mobile-app/package.json b/mobile-app/package.json
index 72e2ad226..8cbfb2d88 100644
--- a/mobile-app/package.json
+++ b/mobile-app/package.json
@@ -61,6 +61,7 @@
"@types/argon2-browser": "^1.18.4",
"@types/fbemitter": "^2.0.35",
"@types/jest": "^29.5.12",
+ "@types/lodash": "^4.17.16",
"@types/react": "~18.3.12",
"@types/react-test-renderer": "^18.3.0",
"@types/sql.js": "^1.4.9",