Skip to content

Commit 6d953d2

Browse files
authored
Merge pull request #324 from GeekyAnts/fix/colormode-flicker
fix: color mode flicker
2 parents c77b373 + 2bc9696 commit 6d953d2

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

pages/_app.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import "../styles/globals.css";
22
import type { AppProps } from "next/app";
3+
import colors from "native-base/src/theme/base/colors";
34
import { ColorMode, NativeBaseProvider, StorageManager } from "native-base";
45
import { AppContext } from "../src/AppContext";
56
import { useEffect, useState } from "react";
67
import React from "react";
7-
import { theme } from "../src/theme";
8+
import { getNativeBaseTheme, theme } from "../src/theme";
89

910
type MyThemeType = typeof theme;
1011
declare module "native-base" {
@@ -32,13 +33,23 @@ function MyApp({ Component, pageProps }: AppProps) {
3233
const [activeVersion, setActiveVersion] = useState("/");
3334
const [activeSidebarItem, setActiveSidebarItem] = useState("");
3435
const [isNavbarOpen, setIsNavbarOpen] = useState(false);
36+
const [colorMode, setColorMode] = useState<ColorMode>(null);
37+
const [mounted, setMounted] = useState(false);
3538

36-
useEffect(() => {
37-
document.getElementsByTagName("html")[0].setAttribute("data-theme", "dark");
39+
React.useEffect(async () => {
40+
const currColorMode = await colorModeManager.get();
41+
document
42+
.getElementsByTagName("html")[0]
43+
.setAttribute("data-theme", currColorMode);
44+
setColorMode(currColorMode);
45+
setMounted(true);
3846
}, []);
3947

48+
const currTheme = getNativeBaseTheme(colorMode);
49+
4050
const updateActiveVersion = (version: string) => setActiveVersion(version);
41-
return (
51+
52+
const body = (
4253
<AppContext.Provider
4354
value={{
4455
activeVersion,
@@ -52,14 +63,20 @@ function MyApp({ Component, pageProps }: AppProps) {
5263
{/* @ts-ignore */}
5364
<NativeBaseProvider
5465
isSSR
55-
theme={theme}
66+
theme={currTheme}
5667
config={config}
5768
colorModeManager={colorModeManager}
5869
>
5970
<Component {...pageProps} />
6071
</NativeBaseProvider>
6172
</AppContext.Provider>
6273
);
74+
75+
if (!mounted) {
76+
return <div style={{ visibility: "hidden" }}>{body}</div>;
77+
}
78+
79+
return body;
6380
}
6481

6582
export default MyApp;

src/theme/index.tsx

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,92 @@
11
import { extendTheme } from "native-base";
22
import colors from "native-base/src/theme/base/colors";
3+
4+
export const getNativeBaseTheme = (colorMode) => {
5+
return extendTheme({
6+
colors: {
7+
backgroundLight: colors.coolGray[100],
8+
backgroundDark: colors.blueGray[900],
9+
10+
primaryTextDark: colors.cyan[200],
11+
primaryTextLight: colors.cyan[700],
12+
13+
borderColorDark: colors.coolGray[800],
14+
borderColorLight: "#dfe3e6",
15+
16+
// -------------------------------------------------- TOC -------------------------------------------
17+
18+
tocActiveTextLightColor: colors.coolGray[900],
19+
tocActiveTextDarkColor: colors.coolGray[50],
20+
21+
tocTextLightColor: colors.coolGray[400],
22+
tocTextDarkColor: colors.coolGray[500],
23+
24+
tocLeftDarkBorder: colors.coolGray[700],
25+
tocLeftLightBorder: colors.coolGray[300],
26+
27+
tocLeftDarkBorderBall: colors.coolGray[800],
28+
tocLeftLightBorderBall: colors.coolGray[300],
29+
30+
tocLeftDarkBorderActiveBall: colors.coolGray[400],
31+
tocLeftLightBorderActiveBall: colors.coolGray[600],
32+
33+
// -------------------------------------------------- SideBar -------------------------------------------
34+
35+
sidebarBackgroundLight: colors.coolGray[100],
36+
sidebarBackgroundDark: colors.blueGray[900],
37+
sidebarItemHeadingTextDark: colors.coolGray[50],
38+
sidebarItemHeadingTextLight: colors.blueGray[900],
39+
sidebarItemTextDark: colors.coolGray[200],
40+
sidebarItemTextLight: "#11181c",
41+
42+
activeSidebarItemBackgroundLight: colors.cyan[200],
43+
activeSidebarItemBackgroundDark: colors.cyan[700],
44+
activeSidebarItemHoverBackgroundLight: colors.cyan[200],
45+
activeSidebarItemHoverBackgroundDark: colors.cyan[600],
46+
47+
inactiveSidebarItemHoverBackgroundLight: colors.blueGray[200],
48+
inactiveSidebarItemHoverBackgroundDark: colors.blueGray[800],
49+
50+
// -------------------------------------------------- Link -------------------------------------------
51+
52+
inactiveLinkTextColorLight: "#687076",
53+
inactiveLinkTextColorDark: "#9ba1a6",
54+
inactiveHoverBorderBottomLinkColorDark: "#26292b",
55+
inactiveHoverBorderBottomLinkColorLight: "#eceef0",
56+
57+
activeLinkTextColorLight: "#11181c",
58+
activeLinkTextColorDark: "#ecedee",
59+
activeBorderBottomLinkColorLight: "#eceef0",
60+
activeBorderBottomLinkColorDark: "#eceef0",
61+
activeHoverBorderBottomLinkColorDark: "#26292b",
62+
activeHoverBorderBottomLinkColorLight: "#eceef0",
63+
64+
// -------------------------------------------------- Page Navs -------------------------------------------
65+
66+
pageNavigationHeadingLight: "#687076",
67+
pageNavigationHeadingDark: "#9ba1a6",
68+
pageNavigationMainTitleLight: "#006adc",
69+
pageNavigationMainTitleDark: "#52a9ff",
70+
71+
// -------------------------------------------------- Tabs -------------------------------------------
72+
73+
SelectedTabTextColor: "#52a9ff",
74+
SelectedTabBorderColor: "#52a9ff",
75+
76+
// -------------------------------------------------- CodeBlock -------------------------------------------
77+
78+
codeBlockBackgroundColor: "#171E2E",
79+
80+
// -------------------------------------------------- Admonitions -------------------------------------------
81+
tipBackgroundColorDark: colors.coolGray[300],
82+
tipBackgroundColorLight: colors.coolGray[900],
83+
},
84+
config: {
85+
initialColorMode: colorMode,
86+
},
87+
});
88+
};
89+
390
export const theme = extendTheme({
491
colors: {
592
backgroundLight: colors.coolGray[100],

styles/globals.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ body {
55
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
66
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
77
position: relative;
8-
background-color: #0f172a;
98
}
109
#nativebase-body-light ::-moz-selection {
1110
/* Code for Firefox */

0 commit comments

Comments
 (0)