在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):braposo/graphql-css开源软件地址(OpenSource Url):https://github.com/braposo/graphql-css开源编程语言(OpenSource Language):JavaScript 99.1%开源软件介绍(OpenSource Introduction):
Comes with a bunch of utilities so it's easy to integrate with your favourite way of building components. Installationnpm install graphql-css
# or
yarn add graphql-css Dependencies
Quick startimport useGqlCSS from "graphql-css";
import styles from "your-style-guide";
const App = () => {
const { styled } = useGqlCSS(styles);
const H2 = styled.h2`
{
typography {
h2
}
marginLeft: spacing {
xl
}
color: colors {
green
}
}
`;
return <H2>This is a styled text</H2>;
}; APIBy default, It also exports a couple of other utilities:
useGqlCSSThe main export is the
Here's how you can use it to create a new component with import useGqlCSS from "graphql-css";
...
const { styled } = useGqlCSS(styles);
const Text = styled.p`
{
typography {
fontSize: scale {
l
}
}
}
`;
...
<Text>This is a styled text</Text> alternatively, you can also return the styles as an object with import useGqlCSS, { gql } from "graphql-css";
import styled from "@emotion/styled";
...
const query = gql`
{
color: colors {
green
}
}
`;
const { getStyles } = useGqlCSS(styles);
const StyledComponent = styled.div(getStyles(query)); If you want to keep the declarative API you can also use the import useGqlCSS, { gql } from "graphql-css";
...
const { GqlCSS } = useGqlCSS(styles);
const query = gql`
{
typography {
h2
}
}
`;
...
<GqlCSS query={query} component="h2">This is a styled text</GqlCSS> Please check the GqlCSS
All the remaining props are passed to the generated component. Here are some examples: ...
<GqlCSS styles={styles} query={query}>This is a styled text</GqlCSS>
<GqlCSS styles={styles} query={queryH1} component="h1">This is a styled H1 heading</GqlCSS>
... Styles objectThe styles object is a valid JSON object that is used to define the styleguide of your project. Usually it includes definitions for colors, spacing, typography, etc. const base = 4;
const styles = {
typography: {
scale: {
s: base * 3,
base: base * 4,
m: base * 6,
l: base * 9,
xl: base * 13,
xxl: base * 20,
unit: "px",
},
weight: {
thin: 300,
normal: 400,
bold: 700,
bolder: 900,
},
},
spacing: {
s: base,
base: base * 2,
m: base * 4,
l: base * 6,
xl: base * 8,
xxl: base * 10,
unit: "px",
},
colors: {
blue: "blue",
green: "green",
red: "red",
},
}; This is completely up to you and one of the big advantages of using You can also specify the unit of each property by definining the scale: {
s: base * 3,
base: base * 4,
m: base * 6,
l: base * 9,
xl: base * 13,
xxl: base * 20,
unit: "em"
}, Building the GraphQL queryThe GraphQL query follows the structure of the styles object with a few particular details. When building the query you need to alias the values you're getting from the style guide to the correspondent CSS property. Here's a quick example: {
typography {
fontSize: scale {
xl
}
fontWeight: weight {
bold
}
}
} This also means that you can reuse the same query by using different alias: {
marginLeft: spacing {
l
}
paddingTop: spacing {
xl
}
} Using fragmentsBecause This is just GraphQL™, you can also create fragments that can then be included in other queries: const h1Styles = gql`
fragment H1 on Styles {
base {
typography {
fontSize: scale {
xl
}
fontWeight: weight {
bold
}
}
}
}
`;
const otherH1Styles = gql`
${h1Styles}
{
...H1
base {
color: colors {
blue
}
}
}
`; This is a powerful pattern that avoids lots of repetitions and allows for a bigger separation of concerns. Defining custom unitYou can also override the pre-defined unit directly in your query by using the argument {
marginLeft: spacing(unit: "em") {
l
}
paddingTop: spacing {
xl
}
} This will return Using style variations (theming)One of the big advantages of CSS-in-GQL™ is that you can use the power of variables to build custom queries. In Let's start with this style definition file: const styles = {
theme: {
light: {
button: {
// button light styles
},
},
dark: {
button: {
// button dark styles
},
},
},
}; We now have two options to handle theming, first using the import useGqlCSS, { gql } from "graphql-css";
...
const { styled } = useGqlCSS(styles);
const Button = styled.button`
{
theme(variant: ${props => props.variant}) {
button
}
}
`;
...
<Button variant="dark">Some text</Button> Alternatively, we can use GraphQL variables instead by using import useGqlCSS, { gql } from "graphql-css";
import styled from "@emotion/styled";
...
const { getStyles } = useGqlCSS(styles);
const query = gql`
{
theme(variant: $variant) {
button
}
}
`;
const LightButton = styled.button(getStyles(query, { variant: "light" }));
...
<LightButton>Some text</LightButton> DevelopingYou can use ContributingPlease follow our contributing guidelines. License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论