Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Tailwind Colors transformPalette not reducing nested values #1339

Closed
3 of 5 tasks
areklam opened this issue Apr 13, 2022 · 3 comments · Fixed by #1341
Closed
3 of 5 tasks

[bug] Tailwind Colors transformPalette not reducing nested values #1339

areklam opened this issue Apr 13, 2022 · 3 comments · Fixed by #1341
Labels
enhancement New feature or request

Comments

@areklam
Copy link

areklam commented Apr 13, 2022

Agreement

Expected Behavior

TransformPalette should output theme.json ready colors for nested colors

Color array like in theme.json ready format like:

"palette": [
        {
          "name": "Primary-name1",
          "slug": "primary-name1",
          "color": "#181818"
        },
        {
          "name": "Primary-name1-50",
          "slug": "primary-name1-50",
          "color": "#383838"
        },
        {
          "name": "Primary-name1-25",
          "slug": "primary-name1-25",
          "color": "#F8F8F8"
        },
        {
          "name": "Primary-name2",
          "slug": "primary-name2",
          "color": "#FFFFFF"
        },
        {
          "name": "Primary-name3",
          "slug": "primary-name3",
          "color": "transparent"
        },

Actual Behavior

Nested colors does gets output with incorrect themeJSON format:

"palette": [
        {
          "name": "Primary-name1",
          "slug": "primary-name1",
          "color": {
            "25": "#F8F8F8",
            "50": "#383838",
            "DEFAULT": "#181818"
          }
        },
        {
          "name": "Primary-name2",
          "slug": "primary-name2",
          "color": "#FFFFFF"
        },
        {
          "name": "Primary-name3",
          "slug": "primary-name3",
          "color": "transparent"
        }

Steps To Reproduce

Create nested Tailwind Colors

colors: {
      primary: {
        name1: {
          DEFAULT: '#181818',
          50: '#383838',
          25: '#F8F8F8',
        },
        name2: '#FFFFFF',
        name3: 'transparent',
      },
    },

Set up bud config for colors (config below) and build

version

5.7.7

What package manager are you using?

yarn classic

version

1.22.18

Are you using pnpm?

  • yes
  • no

Logs

No response

Configuration

const config = require('./tailwind.config.js')
const {transformPalette} = require('@roots/sage/theme/tailwind')
const palette = transformPalette(config.theme.colors)

/**
 * @typedef {import('@roots/bud').Bud} bud
 *
 * @param {bud} app
 */

module.exports = async (app) => {
  app
    /**
     * Application entrypoints
     *
     * Paths are relative to your resources directory
     */
    .entry({
      app: ['@scripts/app', '@styles/app'],
      editor: ['scripts/blocks/*.js', 'scripts/editor.js', 'styles/editor.scss'],
    })

    /**
     * These files should be processed as part of the build
     * even if they are not explicitly imported in application assets.
     */
    .assets('images')

    /**
     * These files will trigger a full page reload
     * when modified.
     */
    .watch([
      'tailwind.config.js',
      'resources/views/*.blade.php',
      'app/View/**/*.php',
    ])

    .serve('http://localhost:3000')

    /**
     * Target URL to be proxied by the dev server.
     *
     * This is your local dev server.
     */
    .proxy('http://www.example.test')

    .themeJson({
      color: {
        custom: false,
        customGradient: false,
        defaultPalette: false,
        defaultGradients: false,
        palette,
      }
    })
};

Relevant .budfiles

budfiles.zip

@areklam areklam added the bug Something isn't working label Apr 13, 2022
@kellymears
Copy link
Member

kellymears commented Apr 13, 2022

Is this a thing that even works in tailwind?

Looking at the tailwind docs I don't see any mention of n-level nesting for color groups. Simplified, the example provided looks like:

module.exports = {
  theme: {
    colors: {
      'tahiti': {
        100: '#cffafe',
      },
    },
  },
}

But you want support for:

module.exports = {
  theme: {
    colors: {
      'tahiti': {
        'sub-color': {
          100: '#cffafe',
        } 
      },
    },
  },
}

Looking at the DefinitelyTyped/tailwindcss repo those types don't allow for this use case either.

A TailwindColorValue is defined as:

export type TailwindColorValue = string | TailwindColorGroup | TailwindColorFunction;

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/tailwindcss/tailwind-config.d.tsL241-L243

And a TailwindColorGroup is just a simple readonly Record object.

export interface TailwindColorGroup {
    readonly [key: string]: string;
}

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/tailwindcss/tailwind-config.d.ts#L188-L190

Am I missing something? Does this work for you? Regardless, since this isn't documented I'm going to mark this as a feature request.

That said, assuming it works and there can be some assurance that this hidden feature is not going to change, I don't mind adding support for it.

@kellymears kellymears added enhancement New feature or request and removed bug Something isn't working labels Apr 13, 2022
@areklam
Copy link
Author

areklam commented Apr 13, 2022

Tailwind does run tests for deeply nested color objects so it should be allowed.

I've been using this nesting in Tailwind for some time without any issues. Relevant Tailwind PR

@kellymears
Copy link
Member

thank you for the references. i think the next step for us is to add recursion to the color value reducer:

const flatten: (
color: string,
shades: TailwindValuesColor | TailwindColorValue,
) => Array<[string, string]> = (color, shades) =>
typeof shades !== 'string'
? Object.entries(shades).reduce((all, [variant, color]) => {
return [...all, [normalizeKey(variant), color]]
}, [])
: [[color, shades]]

afterward we should submit a PR on the typings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants