All Projects → braposo → Graphql Css

braposo / Graphql Css

Licence: mit
A blazing fast CSS-in-GQL™ library.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Graphql Css

Django Api Domains
A pragmatic styleguide for Django API Projects
Stars: ✭ 365 (-45.68%)
Mutual labels:  graphql, styleguide
jss-material-ui
A enhanced styling engine for material-ui
Stars: ✭ 15 (-97.77%)
Mutual labels:  styleguide, styles
Core
Source Code for dotCMS Java Enterprise Content Management System
Stars: ✭ 615 (-8.48%)
Mutual labels:  graphql
Awesome Styleguides
📋 A list of styleguides
Stars: ✭ 644 (-4.17%)
Mutual labels:  styleguide
Pizzaql
🍕 Modern OSS Order Management System for Pizza Restaurants
Stars: ✭ 631 (-6.1%)
Mutual labels:  graphql
Clean Ts Api
API em NodeJs usando Typescript, TDD, Clean Architecture, Design Patterns e SOLID principles
Stars: ✭ 619 (-7.89%)
Mutual labels:  graphql
Nestjs Learning
nestjs 学习教程 📚,跟我一起学习 nest 框架~ 💪
Stars: ✭ 638 (-5.06%)
Mutual labels:  graphql
Starlette
The little ASGI framework that shines. 🌟
Stars: ✭ 6,337 (+843.01%)
Mutual labels:  graphql
Urql
The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
Stars: ✭ 6,604 (+882.74%)
Mutual labels:  graphql
Elixir Boilerplate
⚗ The stable base upon which we build our Elixir projects at Mirego.
Stars: ✭ 627 (-6.7%)
Mutual labels:  graphql
Graphql Ts Server Boilerplate
A GraphQL server boilerplate made with Typescript, PostgreSQL, and Redis
Stars: ✭ 643 (-4.32%)
Mutual labels:  graphql
Graphqlbundle
This bundle provides tools to build a complete GraphQL server in your Symfony App.
Stars: ✭ 628 (-6.55%)
Mutual labels:  graphql
Graphql.github.io
GraphQL Documentation at graphql.org
Stars: ✭ 622 (-7.44%)
Mutual labels:  graphql
Practical.cleanarchitecture
Asp.Net Core 5 Clean Architecture (Microservices, Modular Monolith, Monolith) samples (+Blazor, Angular 11, React 17, Vue 2.6), Domain-Driven Design, CQRS, Event Sourcing, SOLID, Asp.Net Core Identity Custom Storage, Identity Server 4 Admin UI, Entity Framework Core, Selenium E2E Testing, SignalR Notification, Hangfire Tasks Scheduling, Health Checks, Security Headers, ...
Stars: ✭ 639 (-4.91%)
Mutual labels:  graphql
Graphql Client
Typed, correct GraphQL requests and responses in Rust
Stars: ✭ 620 (-7.74%)
Mutual labels:  graphql
Django Graphql Jwt
JSON Web Token (JWT) authentication for Graphene Django
Stars: ✭ 649 (-3.42%)
Mutual labels:  graphql
React App
Create React App with server-side code support
Stars: ✭ 614 (-8.63%)
Mutual labels:  graphql
Graphql Multipart Request Spec
A spec for GraphQL multipart form requests (file uploads).
Stars: ✭ 622 (-7.44%)
Mutual labels:  graphql
Awesome React Graphql
A curated collection of resources, clients and tools that make working with `GraphQL and React/React Native` awesome
Stars: ✭ 635 (-5.51%)
Mutual labels:  graphql
Graphql Java Tools
A schema-first tool for graphql-java inspired by graphql-tools for JS
Stars: ✭ 667 (-0.74%)
Mutual labels:  graphql

graphql-css is a blazing fast CSS-in-GQL™ library that converts GraphQL queries into styles for your components.

Comes with a bunch of utilities so it's easy to integrate with your favourite way of building components.

Build Status Code Coverage npm version npm downloads gzip size MIT License

Module format Prettier format PRs Welcome Blazing Fast Modern Enterprise Grade

Installation

npm install graphql-css
# or
yarn add graphql-css

Dependencies

graphql-css requires graphql to be installed as a peer dependency. It's compatible with React hooks so you can use it with React's latest version.

Quick start

import 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>;
};

Edit graphql-css

API

By default, graphql-css exports a hook-like function called useGqlCSS.

It also exports a couple of other utilities:

  • GqlCSS: a component that provides the same declarative API
  • gql: the default export from graphql-tag so you don't have to install it if only using graphql-css

useGqlCSS

The main export is the useGqlCSS function that should be used in most cases. It provides these utilities:

  • styled: a styled-component inspired function to create components from gqlCSS queries
  • getStyles: a function to extract styles to an object
  • GqlCSS: a component that encapsulates the styled functionality

useGqlCSS needs to be initialised with the styles from the styleguide in a JSON format (check examples folder for a detailed example).

Here's how you can use it to create a new component with styled:

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 getStyles so you can use it with other CSS-in-JS libraries:

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 GqlCSS, which is an exact match to the main GqlCSS component exported by this library. The only difference is that the useGqlCSS version already has the styles initialised.

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 section below for a detailed reference.

GqlCSS

<GqlCSS> component allows for a more declarative API and accepts these props:

Prop Type Default Definition
styles object The styleguide object with all the rules
query gql The gql query to get the styles
component string || node "div" HTML element or React component to be displayed

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 object

The 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 graphql-css as you can adapt it to your needs. As long as the styles and the queries match their structure, there shouldn't be much problem.

You can also specify the unit of each property by definining the unit key.

scale: {
    s: base * 3,
    base: base * 4,
    m: base * 6,
    l: base * 9,
    xl: base * 13,
    xxl: base * 20,
    unit: "em"
},

Building the GraphQL query

The 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 fragments

Because 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 unit

You can also override the pre-defined unit directly in your query by using the argument unit:

{
    marginLeft: spacing(unit: "em") {
        l
    }
    paddingTop: spacing {
        xl
    }
}

This will return { marginLeft: "24em", paddingTop: "32px" }.

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 graphql-css that means that we can easily define variants (think themes) for specific components.

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 styled function from useGqlCSS:

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 getStyles:

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>

Developing

You can use yarn run dev to run it locally, but we recommend using the CodeSandbox playground for development.

Contributing

Please follow our contributing guidelines.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].