Coder Social home page Coder Social logo

hauptrolle / stitches-utils Goto Github PK

View Code? Open in Web Editor NEW
35.0 4.0 1.0 448 KB

Helpful stitches shorthand utilities combined in one package

Home Page: https://github.com/hauptrolle/stitches-utils

TypeScript 95.86% JavaScript 4.14%
stitches-utils stitches css-in-js

stitches-utils's People

Contributors

allcontributors[bot] avatar braposo avatar dependabot[bot] avatar hauptrolle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

braposo

stitches-utils's Issues

Access CSS property value from util

Working with @styled/tailwind, I noticed a missing util equivalent for Tailwind's Background Opacity. Trying to implement it myself, I found that this was not supported.

In order to implement this feature, you would need to be able to read the current value of backgroundColor property and return a new backgroundColor property.

Consider this example, which is a psuedo-API for defining utils:

const utils = {
  // sets a background color
  bg: (value) => ({ backgroundColor: value });
  bgOpacity: (value, _config, css) => {
    // Throw error if the backgroundColor hasn't been defined.
    if (css.backgroundColor === undefined) {
      throw new Error("Using bgOpacity requires a background color to be set");
    }

    // Access calculated CSS properties
    const parsedColor = parseCssColor(css.backgroundColor);
    const cssColorWithOpacity = formatCssColor(parsedColor, value);

    // Overwrite the backgroundColor
    return { backgroundColor: cssColorWithOpacity };
  }
}

You could use multiple utils in the same style to "compose" multiple operations:

const example = css({
  bg: 'red', // sets backgroundColor to 'red'
  bgOpacity: 75 // sets backgroundColor to red with opacity 75
  bgTint: 0.5 // sets backgroundColor to tinted red with opacity 75
  bgOpacity: 100 // sets backgroundColor to tinted red with opacity 100
});

One question I'm yet to figure out is when these utilities are called to allow accessing "calculated" properties.

Alternative solutions

An alternative is to define colors in tokens:

{
  tokens: {
    colors: {
      $gray800_75: { backgroundColor: "rgba(150, 150, 150, 0.75)" }
    }
  }
}

Or simply use it inline with your style, similar to the above example. But these won't work well with tokens and theme.

Another solution would be to use functions for this:

import { config } from './styles';

const bgOpacity = (bg, opacity) => {
  const hex = config.theme.colors[bg];
  const [r, g, b] = hexToRgb(hex);
  return `rgba(${r}, ${g}, ${b}, ${opacity / 100})`;
};

const style = css({
  backgroundColor: bgOpacity('gray-800', 75)
})

Add eslint

Add & setup eslint to enforce some coding standards.

Compose custom CSS properties

Hey @hauptrolle thanks for this library, this is really useful!

What do you think of adding a compose function (or something with a better name ๐Ÿ˜…) to create new utils based on specific CSS properties?

Was thinking something like:

function compose(...properties: (keyof CSSStyleDeclaration)[]) {
    return (config: never) => (value: string) => properties.reduce((final, cssProp) => {
        return {
            ...final,
            [cssProp]: value
        }
    }, {});
}

which would then be used like:

export const { styled, css } = createStyled({
  utils: {
    size: compose('width','height'),
  },
});

Do you think this would make sense to include in this library?

If so, would you prefer to use an array as args or just spread the args like this example?

Improve typing

It would be great if utils could be typed against the tokens.

Currently, it only displays the valid CSS Properties:

image

We have an issue opened in Stitches about improving this (stitchesjs/stitches#129), so we may need to wait. But something to keep in mind

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.