-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApp.jsx
101 lines (93 loc) · 2.71 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React, { useContext, useState } from "react";
import styleguide from "./styleguide";
import useGqlCSS, { GqlCSS, gql } from "../src";
import { h1Styles, h2Styles, stateStyles } from "./styleQueries";
import cxs from "cxs/component";
import PropTypes from "prop-types";
const Context = React.createContext();
function StatefulComponent() {
const [variant, setVariant] = useState("normal");
const { styled, GqlCSS } = useGqlCSS(styleguide);
const toggleVariant = () => setVariant(state => (state === "normal" ? "done" : "normal"));
const OtherComponent = styled.button`{
theme(variant: ${props => props.variant}) {
button
}
base {
marginLeft: spacing {
${variant === "done" ? "m" : "xl"}
}
}
}`;
OtherComponent.propTypes = {
variant: PropTypes.string.isRequired,
};
return (
<React.Fragment>
<GqlCSS query={stateStyles} variables={{ variant }} onClick={toggleVariant}>
Using stateful component
</GqlCSS>
<OtherComponent variant={variant}>Other component sharing state</OtherComponent>
</React.Fragment>
);
}
function SubscriberComponent() {
const styleguide = useContext(Context);
const { getStyles } = useGqlCSS(styleguide);
const styles = getStyles(gql`
{
base {
typography {
fontSize: scale {
m
}
}
marginLeft: spacing {
xl
}
color: colors {
blue
}
}
}
`);
const StyledComponent = cxs("h3")(styles);
return <StyledComponent>Getting styles through context</StyledComponent>;
}
function App() {
const { styled } = useGqlCSS(styleguide);
const H2 = styled.h2(h2Styles);
const H3 = styled.h3`{
base {
marginLeft: spacing {
m
}
}
${props =>
props.blue &&
`
base {
color: colors {
blue
}
}
`}
}`;
H3.propTypes = {
blue: PropTypes.bool,
};
return (
<div>
<H2>This is a styled text</H2>
<H3 blue={true}>Component with template literal</H3>
<GqlCSS styles={styleguide} query={h1Styles}>
A styled component
</GqlCSS>
<Context.Provider value={styleguide}>
<SubscriberComponent />
</Context.Provider>
<StatefulComponent />
</div>
);
}
export default App;