Code Monkey home page Code Monkey logo

Comments (6)

siriwatknp avatar siriwatknp commented on August 24, 2024

This is expected by design. We should do better at documenting it.

The theme.colorSchemes should contain only tokens (to be converted to CSS variables). What you provided is a stylesheet that could not be converted to a CSS variable.

If you want to create a reusable styles, you can:

  • create a new nodes, e.g. theme.styles.*
  • use css() and reexport it.

from pigment-css.

o-alexandrov avatar o-alexandrov commented on August 24, 2024

Importing mixins from colorSchemes directly as-is won’t work, because the background might be different for each colorScheme.

So either the design of Pigment should change to allow this case, or there should be a converter of a path to a token from colorSchemes into a variable name to be used in mixins

from pigment-css.

siriwatknp avatar siriwatknp commented on August 24, 2024

Importing mixins from colorSchemes directly as-is won’t work, because the background might be different for each colorScheme.

So either the design of Pigment should change to allow this case, or there should be a converter of a path to a token from colorSchemes into a variable name to be used in mixins

Thanks for the feedback, before going into the solution, can you explain in more detail about your use case? Do you want to customize a background based on a different color scheme?

theme.vars contains fields that refer to CSS variables. It cannot contain stylesheets.

from pigment-css.

o-alexandrov avatar o-alexandrov commented on August 24, 2024

@siriwatknp Since you mentioned using mixins within colorSchemes is prohibited by design, please refer to the example below for the proposal on reusing css variables in mixins outside of colorSchemes.

const theme = extendTheme({
  colorSchemes: {
    dark: {
       // these are the colorScheme-dependent tokens you want to use in mixins
      palette: {
        overlay: `black`,
        primary: {
          main: `#fafafa`,
        }
      }
    }
  },
  // then you want to define a reusable stylesheet within mixins
  mixins: {
    example: {
      /**
       * Pigment should have a function to make it type-safe.
       *   - you could also infer the type automatically from the theme.colorSchemes[string] and allow dot notation reference w/o a function (ex. `getVar`)
       *
       * If a function is your preference, then Pigment (`getVar` below) also needs to take into account the `cssVarPrefix` bundler's option see: https://github.com/mui/material-ui/tree/master/packages/pigment-css-react#adding-a-prefix-to-css-variables
       * 
       * In the example below, the function is named as `getVar`
       *     the type of the parameter can be
       *        import type { Paths } from "type-fest" // see: https://github.com/sindresorhus/type-fest/blob/main/source/paths.d.ts
       *        type getVar = (key: Paths<PigmentTheme["colorSchemes"][keyof PigmentTheme["colorSchemes"]]>) => `var(${string})`
       * 
       * @example how it would look like in the code
       * backgroundColor: getVar("palette.overlay")
       * "&:hover": {
       *   backgroundColor: getVar("palette.primary.main")
       * }
       * 
       * Below, how it's currently must be done.
       * It is not type-safe, but it works.
       */
      backgroundColor: `var(--palette-overlay)`,
      "&:hover": {
        backgroundColor: `var(--palette-primary-main)`,
      }
    }
  }
})

from pigment-css.

brijeshb42 avatar brijeshb42 commented on August 24, 2024

@o-alexandrov Could you also add a code example on how you'd use these mixins in your css or styled definitions ?

from pigment-css.

o-alexandrov avatar o-alexandrov commented on August 24, 2024

@brijeshb42 in this case, it'd be:

export const classNameWithMixin = pigment.css(({ theme }) => ({
  ...theme.mixins.example,
  display: `flex`
}))

from pigment-css.

Related Issues (20)

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.