Skip to content

Commit e7afbfa

Browse files
committed
Add theme switch to example
swaps logo when dark improves color choices for switching
1 parent 5f5ee51 commit e7afbfa

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed
8.27 KB
Loading

example/index.tsx

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@ import 'typeface-lato'
44
import * as React from 'react'
55
import * as ReactDOM from 'react-dom'
66
import * as C from '../dist'
7-
import logo from './images/Committed - Yellow Trans-128px.png'
7+
import logoLight from './images/Committed - Yellow Trans-128px.png'
8+
import logoDark from './images/Committed - Black-128px.png'
89

910
const footerHeight = '200px'
1011

11-
const Header = () => (
12+
const Header = ({
13+
themeChoice,
14+
toggleThemeChoice,
15+
}: {
16+
themeChoice: C.ThemeChoice
17+
toggleThemeChoice: () => void
18+
}) => (
1219
<C.AppBar position="relative">
1320
<C.Toolbar>
1421
<C.Box flexGrow={1}>
1522
<C.Heading.h1>Example</C.Heading.h1>
1623
</C.Box>
1724
<C.Avatar src="https://i.pravatar.cc/40" />
25+
<C.ThemeSwitch
26+
themeChoice={themeChoice}
27+
toggleThemeChoice={toggleThemeChoice}
28+
variant="celestial"
29+
/>
1830
<C.Button color="inherit" variant="text">
1931
Logout
2032
</C.Button>
@@ -27,7 +39,7 @@ const Post = ({ name, index }: { name: string; index: number }) => (
2739
<C.Flex
2840
flexDirection="column"
2941
bgcolor="primary.main"
30-
color="secondary.main"
42+
color="secondary.dark"
3143
>
3244
<img src={`https://picsum.photos/300/200?random=${index}`} />
3345
<C.CardActionArea>
@@ -69,6 +81,12 @@ const Content = () => (
6981
</C.Container>
7082
)
7183

84+
const Logo = () => {
85+
const theme = C.useTheme()
86+
const logo = theme.palette.type == 'light' ? logoLight : logoDark
87+
return <img width="128px" src={logo} alt="Committed Logo" />
88+
}
89+
7290
const Footer = () => (
7391
<C.Row
7492
p={3}
@@ -78,10 +96,10 @@ const Footer = () => (
7896
height={footerHeight}
7997
justifyContent="space-around"
8098
bgcolor="primary.main"
81-
color="white"
99+
color="primary.contrastText"
82100
>
83101
<C.Column justifyContent="center" alignItems="center">
84-
<img width="128px" src={logo} alt="Committed Logo" />
102+
<Logo />
85103
</C.Column>
86104
<C.Column>
87105
<C.Heading.h5 mb={0}>Links</C.Heading.h5>
@@ -115,21 +133,31 @@ const Footer = () => (
115133
</C.Row>
116134
)
117135

118-
const App = () => (
119-
<C.ThemeProvider
120-
fonts={{
121-
typography: { fontFamily: 'Lato' },
122-
display: { fontFamily: 'Arciform' },
123-
}}
124-
>
125-
<C.Box position="relative" minHeight="100vh">
126-
<C.Box pb={footerHeight} bgcolor="background.default">
127-
<Header />
128-
<Content />
136+
const App = () => {
137+
const [themeChoice, toggleThemeChoice, componentMounted] = C.useThemeChoice()
138+
const component = componentMounted ? (
139+
<C.ThemeProvider
140+
choice={themeChoice}
141+
createFonts={() =>
142+
Object.assign(C.fonts.defaultFonts, {
143+
typography: { fontFamily: 'Lato' },
144+
display: { fontFamily: 'Arciform' },
145+
})
146+
}
147+
>
148+
<C.Box position="relative" minHeight="100vh" bgcolor="background.paper">
149+
<C.Box pb={footerHeight}>
150+
<Header
151+
themeChoice={themeChoice}
152+
toggleThemeChoice={toggleThemeChoice}
153+
/>
154+
<Content />
155+
</C.Box>
156+
<Footer />
129157
</C.Box>
130-
<Footer />
131-
</C.Box>
132-
</C.ThemeProvider>
133-
)
158+
</C.ThemeProvider>
159+
) : null
134160

161+
return component
162+
}
135163
ReactDOM.render(<App />, document.getElementById('root'))

0 commit comments

Comments
 (0)