Skip to content

Commit 427e88a

Browse files
committed
styled-components and useContext
1 parent 2fd72cc commit 427e88a

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

src/App.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
import React from 'react';
1+
import React from "react";
22
import Component from './components/Component'
33

4-
const App = () => <div className="App"><Component/></div>;
4+
//Provider
5+
import ThemeContext from "./contexts/theme";
6+
7+
const App = () => {
8+
const theme = {
9+
colors: {
10+
primary: "#4A430B",
11+
primaryVariant: "#3C3A2D",
12+
background: "#F7DF1E",
13+
secondary: "#F9EA6F",
14+
secondaryVariant: "#FFFFFF"
15+
}
16+
};
17+
18+
return (
19+
<ThemeContext.Provider value={{ theme }}>
20+
<div className="App">
21+
<Component/>
22+
</div>
23+
</ThemeContext.Provider>);
24+
}
525
export default App;

src/components/Component.jsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React from "react";
1+
import React, { useContext } from "react";
2+
import ThemeContext from "../contexts/theme";
23
import styled from "styled-components";
34

45
const Wrapper = styled.div`
56
padding: 40px;
67
text-align: left;
7-
background: #f7df1e;
8-
color: #3c3a2d;
8+
background-color: ${({ theme }) => theme.colors.background};
9+
color: ${({ theme }) => theme.colors.primaryVariant};
910
`;
1011

1112
const Title = styled.h1`
@@ -19,14 +20,14 @@ const Paragraph = styled.p`
1920
const Button = styled.button`
2021
font-size: 14px;
2122
font-weight: 400;
22-
background-color: #4a430b;
23+
background-color: ${({ theme }) => theme.colors.primary};
2324
border: none;
2425
cursor: pointer;
25-
color: #ffffff;
26+
color: ${({ theme }) => theme.colors.secondaryVariant};
2627
padding: 10px 20px;
2728
border-radius: 4px;
2829
&:hover {
29-
background-color: #342f08;
30+
background-color: ${({ theme }) => theme.colors.primaryVariant};
3031
}
3132
`;
3233
const Link = styled.a`
@@ -35,23 +36,25 @@ const Link = styled.a`
3536
background-color: transparent;
3637
border: none;
3738
cursor: pointer;
38-
color: #3c3a2d;
39+
color: ${({ theme }) => theme.colors.primaryVariant};
3940
padding: 10px 20px;
4041
border-radius: 4px;
4142
`;
4243

4344
const Component = props => {
45+
const { theme } = useContext(ThemeContext);
46+
4447
return (
45-
<Wrapper>
48+
<Wrapper theme={theme}>
4649
<Title>Example component CSS</Title>
4750
<Paragraph>
4851
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ducimus,
4952
temporibus labore voluptatibus aspernatur illo ad id sit nulla, a atque
5053
doloribus itaque ab? Soluta, exercitationem illum provident quod iure
5154
numquam?
5255
</Paragraph>
53-
<Button>I'm a button</Button>
54-
<Link href="/#" onClick={e => e.preventDefault()}>
56+
<Button theme={theme}>I'm a button</Button>
57+
<Link href="/#" onClick={e => e.preventDefault()} theme={theme}>
5558
I'm a link
5659
</Link>
5760
</Wrapper>

src/contexts/theme.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from "react";
2+
3+
export default React.createContext();

0 commit comments

Comments
 (0)