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

Allow use of ref in theme.json settings, as is possible with styles #57286

Open
kaelansmith opened this issue Dec 20, 2023 · 0 comments
Open
Labels
[Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Package] Style Engine /packages/style-engine [Type] Enhancement A suggestion for improvement.

Comments

@kaelansmith
Copy link

kaelansmith commented Dec 20, 2023

What problem does this address?

I use theme.json settings > color > palette to set a "global" list of allowed colors to use throughout the whole block editor, then I restrict certain blocks to certain colors by providing a refined list of colors in settings > blocks > {block_name} > color > palette; however, it seems I have to manually re-define color objects (i.e. { "slug": "...", "name": "...", "color": "..." }) when defining a block-specific palette, and can't reference colors from my global palette in a dynamic way. I end up having to copy/paste the color definitions from my global palette into my block-specific palettes multiple times, resulting in tons of duplicate code that creates maintenance hell should I choose to change colors later. Example:

{
  "$schema": "https://schemas.wp.org/trunk/theme.json",
  "version": 2,
  "settings": {
     "color": {
       "palette": [
          {
             "slug": "primary",
             "name": "Primary",
             "color": "#2563EB"
          },
          {
             "slug": "secondary",
             "name": "Secondary",
             "color": "#121212"
          }
        ]
     },
     "blocks": {
       "core/button": {
          "color": {
              "palette": [
                  { // duplicate copy-pasted definition:
                     "slug": "primary",
                     "name": "Primary",
                     "color": "#2563EB"
                  }
               ]
           }
        }
     }
  }
}

What is your proposed solution?

In my troubleshooting/research I came across the introduction of the ref mechanism in theme.json's styles, where you can dynamically reference other values in the styles object/tree. This is exactly the solution I want, but for theme.json settings. I would have expected ref to be use-able throughout the whole theme.json tree, not just under styles.

Here's what I'd like:

{
  "$schema": "https://schemas.wp.org/trunk/theme.json",
  "version": 2,
  "settings": {
     "color": {
       "palette": [
          {
             "slug": "primary",
             "name": "Primary",
             "color": "#2563EB"
          },
          {
             "slug": "secondary",
             "name": "Secondary",
             "color": "#121212"
          }
        ]
     },
     "blocks": {
       "core/button": {
          "color": {
              "palette": [
                  { "ref": "settings.color.palette.primary" }
               ]
           }
        }
     }
  }
}

Please let me know if I'm missing an existing capability for achieving this!

@kaelansmith kaelansmith added the [Type] Enhancement A suggestion for improvement. label Dec 20, 2023
@jordesign jordesign added [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Package] Style Engine /packages/style-engine labels Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Package] Style Engine /packages/style-engine [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

2 participants