|
1 |
| -// TODO figure out case, Netlify is case sensitive |
2 |
| -// https://community.netlify.com/t/support-guide-netlify-app-builds-locally-but-fails-on-deploy-case-sensitivity/10754 |
3 |
| -import React from "react" |
| 1 | +import React, { useState, useEffect } from "react" |
4 | 2 | import { ThemeProvider } from "styled-components"
|
5 | 3 | import { IntlProvider, IntlContextProvider } from "gatsby-plugin-intl"
|
6 | 4 | import styled from "styled-components"
|
@@ -60,106 +58,92 @@ const StyledBannerNotification = styled(BannerNotification)`
|
60 | 58 | `
|
61 | 59 |
|
62 | 60 | // TODO `Layout` renders twice on page load - why?
|
63 |
| -// TODO refactor into function component |
64 |
| -class Layout extends React.Component { |
65 |
| - constructor(props) { |
66 |
| - super(props) |
67 |
| - this.state = { |
68 |
| - isDarkTheme: false, |
69 |
| - } |
70 |
| - } |
| 61 | +const Layout = (props) => { |
| 62 | + const [isDarkTheme, setIsDarkTheme] = useState(false) |
71 | 63 |
|
72 | 64 | // set isDarkTheme based on browser/app user preferences
|
73 |
| - componentDidMount = () => { |
| 65 | + useEffect(() => { |
74 | 66 | if (localStorage && localStorage.getItem("dark-theme") !== null) {
|
75 |
| - this.setState({ |
76 |
| - isDarkTheme: localStorage.getItem("dark-theme") === "true", |
77 |
| - }) |
| 67 | + setIsDarkTheme(localStorage.getItem("dark-theme") === "true") |
78 | 68 | } else {
|
79 |
| - this.setState({ |
80 |
| - isDarkTheme: window.matchMedia("(prefers-color-scheme: dark)").matches, |
81 |
| - }) |
| 69 | + setIsDarkTheme(window.matchMedia("(prefers-color-scheme: dark)").matches) |
82 | 70 | }
|
83 | 71 | window
|
84 | 72 | .matchMedia("(prefers-color-scheme: dark)")
|
85 | 73 | .addListener(({ matches }) => {
|
86 | 74 | if (localStorage && localStorage.getItem("dark-theme") === null) {
|
87 |
| - this.setState({ isDarkTheme: matches }) |
88 |
| - } |
89 |
| - }) |
90 |
| - } |
91 |
| - |
92 |
| - componentWillUnmount = () => { |
93 |
| - window |
94 |
| - .matchMedia("(prefers-color-scheme: dark)") |
95 |
| - .removeListener(({ matches }) => { |
96 |
| - if (localStorage && localStorage.getItem("dark-theme") === null) { |
97 |
| - this.setState({ isDarkTheme: matches }) |
| 75 | + setIsDarkTheme(matches) |
98 | 76 | }
|
99 | 77 | })
|
100 |
| - } |
| 78 | + return () => { |
| 79 | + window |
| 80 | + .matchMedia("(prefers-color-scheme: dark)") |
| 81 | + .removeListener(({ matches }) => { |
| 82 | + if (localStorage && localStorage.getItem("dark-theme") === null) { |
| 83 | + setIsDarkTheme(matches) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | + }, []) |
101 | 88 |
|
102 |
| - handleThemeChange = () => { |
103 |
| - const isDarkTheme = !this.state.isDarkTheme |
104 |
| - this.setState({ isDarkTheme: isDarkTheme }) |
| 89 | + const handleThemeChange = () => { |
| 90 | + setIsDarkTheme(!isDarkTheme) |
105 | 91 | if (localStorage) {
|
106 |
| - localStorage.setItem("dark-theme", isDarkTheme) |
| 92 | + localStorage.setItem("dark-theme", !isDarkTheme) |
107 | 93 | }
|
108 | 94 | }
|
109 | 95 |
|
110 |
| - render() { |
111 |
| - // IntlProvider & IntlContextProvider appear to be necessary in order to pass context |
112 |
| - // into components that live outside page components (e.g. Nav & Footer). |
113 |
| - // https://github.com/wiziple/gatsby-plugin-intl/issues/116 |
114 |
| - const intl = this.props.pageContext.intl |
115 |
| - const theme = this.state.isDarkTheme ? darkTheme : lightTheme |
116 |
| - |
117 |
| - const path = this.props.path |
118 |
| - const shouldShowSideNav = path.includes("/docs/") |
119 |
| - const shouldShowSubNav = path.includes("/developers/") |
120 |
| - const shouldShowBanner = |
121 |
| - path.includes("/eth2/") && !path.includes("/eth2/deposit-contract/") |
122 |
| - |
123 |
| - return ( |
124 |
| - <IntlProvider |
125 |
| - locale={intl.language} |
126 |
| - defaultLocale={intl.defaultLocale} |
127 |
| - messages={intl.messages} |
128 |
| - > |
129 |
| - <IntlContextProvider value={intl}> |
130 |
| - <ThemeProvider theme={theme}> |
131 |
| - <GlobalStyle isDarkTheme={this.state.isDarkTheme} /> |
132 |
| - <ContentContainer> |
133 |
| - <Nav |
134 |
| - handleThemeChange={this.handleThemeChange} |
135 |
| - isDarkTheme={this.state.isDarkTheme} |
136 |
| - path={path} |
137 |
| - /> |
138 |
| - |
139 |
| - <MainContainer |
140 |
| - shouldShowBanner={shouldShowBanner} |
141 |
| - shouldShowSubNav={shouldShowSubNav} |
142 |
| - shouldShowSideNav={shouldShowSideNav} |
143 |
| - > |
144 |
| - {shouldShowSideNav && <SideNav path={path} />} |
145 |
| - {shouldShowSideNav && <SideNavMobile path={path} />} |
146 |
| - <MainContent> |
147 |
| - <StyledBannerNotification shouldShow={shouldShowBanner}> |
148 |
| - Staking has arrived! If you're looking to stake your ETH,{" "} |
149 |
| - <Link to="/eth2/deposit-contract/"> |
150 |
| - confirm the deposit contract address |
151 |
| - </Link> |
152 |
| - . |
153 |
| - </StyledBannerNotification> |
154 |
| - <Main>{this.props.children}</Main> |
155 |
| - </MainContent> |
156 |
| - </MainContainer> |
157 |
| - <Footer /> |
158 |
| - </ContentContainer> |
159 |
| - </ThemeProvider> |
160 |
| - </IntlContextProvider> |
161 |
| - </IntlProvider> |
162 |
| - ) |
163 |
| - } |
| 96 | + // IntlProvider & IntlContextProvider appear to be necessary in order to pass context |
| 97 | + // into components that live outside page components (e.g. Nav & Footer). |
| 98 | + // https://github.com/wiziple/gatsby-plugin-intl/issues/116 |
| 99 | + const intl = props.pageContext.intl |
| 100 | + const theme = isDarkTheme ? darkTheme : lightTheme |
| 101 | + |
| 102 | + const path = props.path |
| 103 | + const shouldShowSideNav = path.includes("/docs/") |
| 104 | + const shouldShowSubNav = path.includes("/developers/") |
| 105 | + const shouldShowBanner = |
| 106 | + path.includes("/eth2/") && !path.includes("/eth2/deposit-contract/") |
| 107 | + |
| 108 | + return ( |
| 109 | + <IntlProvider |
| 110 | + locale={intl.language} |
| 111 | + defaultLocale={intl.defaultLocale} |
| 112 | + messages={intl.messages} |
| 113 | + > |
| 114 | + <IntlContextProvider value={intl}> |
| 115 | + <ThemeProvider theme={theme}> |
| 116 | + <GlobalStyle isDarkTheme={isDarkTheme} /> |
| 117 | + <ContentContainer> |
| 118 | + <Nav |
| 119 | + handleThemeChange={handleThemeChange} |
| 120 | + isDarkTheme={isDarkTheme} |
| 121 | + path={path} |
| 122 | + /> |
| 123 | + |
| 124 | + <MainContainer |
| 125 | + shouldShowBanner={shouldShowBanner} |
| 126 | + shouldShowSubNav={shouldShowSubNav} |
| 127 | + shouldShowSideNav={shouldShowSideNav} |
| 128 | + > |
| 129 | + {shouldShowSideNav && <SideNav path={path} />} |
| 130 | + {shouldShowSideNav && <SideNavMobile path={path} />} |
| 131 | + <MainContent> |
| 132 | + <StyledBannerNotification shouldShow={shouldShowBanner}> |
| 133 | + Staking has arrived! If you're looking to stake your ETH,{" "} |
| 134 | + <Link to="/eth2/deposit-contract/"> |
| 135 | + confirm the deposit contract address |
| 136 | + </Link> |
| 137 | + . |
| 138 | + </StyledBannerNotification> |
| 139 | + <Main>{props.children}</Main> |
| 140 | + </MainContent> |
| 141 | + </MainContainer> |
| 142 | + <Footer /> |
| 143 | + </ContentContainer> |
| 144 | + </ThemeProvider> |
| 145 | + </IntlContextProvider> |
| 146 | + </IntlProvider> |
| 147 | + ) |
164 | 148 | }
|
165 | 149 | export default Layout
|
0 commit comments