Skip to content

Commit 2b09bdf

Browse files
author
Horacio Herrera
committed
themeing exercise preparation
1 parent 0549cf2 commit 2b09bdf

File tree

9 files changed

+47
-44
lines changed

9 files changed

+47
-44
lines changed

src/components/patterns/Theming/Page.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const Page = props => (
2424
<Example />
2525
<hr />
2626
<h3>Exercise</h3>
27+
<p>go to <code>src/components/patterns/Theming/exercise/index.jsx</code> and start doing the TODOs mentioned in that file!</p>
2728
<Exercise />
2829
<hr />
2930
<h3>Bonus</h3>
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import React from "react";
2-
import { ThemeConsumer } from "./ThemeContext";
2+
// import { ThemeConsumer } from "./ThemeContext";
33
import Button from "./Button";
44
import Hero from "./Hero";
55

6+
/*
7+
TODO:
8+
- import the ThemeConsumer you exported from `ThemeContext`
9+
- Wrap your Button with these consumer, and implement the `onClick` function in the button. (using render props + the `setValue` function)
10+
*/
11+
612
const App = () => (
7-
<ThemeConsumer>
8-
{({ setValue }) => (
9-
<Hero>
10-
<Button variant="secondary" onClick={setValue}>
11-
Theme Toggle
12-
</Button>
13-
</Hero>
14-
)}
15-
</ThemeConsumer>
13+
<Hero>
14+
<Button variant="secondary">Theme Toggle</Button>
15+
</Hero>
1616
);
1717

1818
export default App;

src/components/patterns/Theming/bonus/components/Button.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from 'react'
22
import styled from 'styled-components'
33

4+
// TODO: change the background-color of this button depending on the theme selected
45
const StyledButton = styled.button`
56
padding: 8px 16px;
67
border-radius: 3px;
78
border: none;
8-
9-
background-color: ${({variant, theme}) => theme.colors[variant] || "white"};
10-
color: white;
9+
color: black;
10+
background: #dedede;
1111
`
1212

1313
const Button = ({onClick, children, ...rest}) => (

src/components/patterns/Theming/bonus/components/Hero.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React from 'react'
22
import styled from 'styled-components'
33

4+
// TODO: add the theme background color to the Wrapper as `background-color`
45
const Wrapper = styled("div")`
56
padding: 100px 200px;
67
display: flex;
78
align-items: center;
89
justify-content: center;
910
flex-direction: column;
10-
background-color: ${({theme}) => theme.colors.background};
1111
`
1212

13+
// TODO: add the primary color as the heading `color`
1314
const Heading = styled.h1`
1415
text-align: center;
1516
font-family: Arial, Helvetica, sans-serif;
16-
color: ${({theme}) => theme.colors.primary};
1717
`
1818

1919
const Hero = ({children}) => (

src/components/patterns/Theming/bonus/components/Root.jsx

Whitespace-only changes.

src/components/patterns/Theming/bonus/components/ThemeContext.jsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,39 @@ import React from 'react'
22
import themes from '../theme'
33
import { ThemeProvider as StyledProvider } from 'styled-components'
44

5-
const ThemeContext = React.createContext({});
5+
// First you need to create your `ThemeContext` (use React.CreateContext());
66

77
class ThemeProvider extends React.Component {
8+
9+
// You should heep track of some sort of state to toggle the theme from `light` to `dark`
10+
// (see the themes.js file)
811
state = {
9-
theme: "light"
12+
theme: "???"
1013
}
1114

1215
handleThemeToggle = () => {
13-
this.setState(prevState => ({theme: prevState.theme === 'light' ? 'dark' : 'light'}))
16+
/*
17+
Toggle the theme state using `this.setState`.
18+
remember you can call this function with a funcion as a parameter:
19+
`this.setState(prevState => {}); // prevState is a reference to the actual previous state.
20+
remember that `this.setState` is asynchronous
21+
TODO: link to setState documentation!
22+
*/
1423
}
1524

1625
render() {
17-
const { theme } = this.state;
18-
const { children } = this.props;
19-
console.log('theme => ', themes[theme]);
26+
const { children } = this.props
2027
return (
21-
<ThemeContext.Provider value={{theme, setValue: this.handleThemeToggle}}>
22-
<StyledProvider theme={themes[theme]}>
23-
{children}
24-
</StyledProvider>
25-
</ThemeContext.Provider>
28+
<div>
29+
{children}
30+
</div>
2631
)
2732
}
2833
}
29-
const ThemeConsumer = ThemeContext.Consumer;
34+
35+
const ThemeConsumer = "???"
3036

3137
export {
3238
ThemeProvider,
33-
ThemeConsumer
34-
}
39+
ThemeConsumer // what should be the value of this variable ?? ThemeContext.???
40+
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
2-
import styled, { ThemeProvider } from "styled-components";
3-
import theme from "./theme";
2+
import styled from "styled-components";
3+
// import theme from "./theme";
44

55
/*
66
Exercise TODO:
@@ -16,28 +16,24 @@ import theme from "./theme";
1616
const Card = styled("div")`
1717
border-radius: 8px;
1818
padding: 16px;
19-
box-shadow: 0 0 8px 4px rgba(0,0,0,0.1);
19+
box-shadow: 0 0 8px 4px rgba(0, 0, 0, 0.1);
2020
font-size: 18px;
2121
font-weight: 800;
2222
text-align: center;
23-
color: ${props => props.theme.colors.primary};
2423
background-color: white;
2524
`;
2625

2726
/*
2827
in this component just use the background color from the theme and asign it to this component
2928
*/
3029
const Wrapper = styled("div")`
31-
background-color: ${props => props.theme.colors.background};
3230
padding: 40px;
3331
`;
3432

3533
const ThemingExercise = () => (
36-
<ThemeProvider theme={theme}>
37-
<Wrapper>
38-
<Card>Hallo I'm a Card</Card>
39-
</Wrapper>
40-
</ThemeProvider>
34+
<Wrapper>
35+
<Card>Hallo I'm a Card</Card>
36+
</Wrapper>
4137
);
4238

4339
export default ThemingExercise;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
const theme = {
2-
colors: {
3-
primary: "peru",
4-
secondary: "#f6f6ff",
5-
background: "papayawhip"
6-
}
2+
// add some `colors` here maybe??
73
}
84

95
export default theme;

src/main.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
html, body {
2+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
3+
}
4+
15
#page-wrap > div:first-child {
26
position: absolute;
37
height: 100%;

0 commit comments

Comments
 (0)