• I’m working on a new site and have a palette defined for the button block in the settings section of my theme.json.

    "core/button": {
                    "border": {
                        "radius": false
                    },
                    "color": {
                        "palette": [
                            {
                                "slug": "dark-green",
                                "color": "#76742D",
                                "name": "Dark Green"
                            },
                            {
                                "slug": "light-green",
                                "color": "#B4B15D",
                                "name": "Light Green"
                            },
                            {
                                "slug": "dark-blue",
                                "color": "#273A4A",
                                "name": "Dark Blue"
                            },
                            {
                                "slug": "gold",
                                "color": "#D9BC56",
                                "name": "Gold"
                            }
                        ]
                    }
                }

    And later on, in the styles section I’m defining the default styles, which works great.

    "button": {
                    "typography": {
                        "fontWeight": "700",
                        "fontSize": "16px",
                        "textTransform": "uppercase"
                    },
                    "color": {
                        "background": "var(--wp--preset--color--dark-green)",
                        "text": "#FFFFFF"
                    },
                    "border": {
                        "radius": "0",
                        "width": "2px",
                        "color": "var(--wp--preset--color--dark-green)",
                        "style": "solid"
                    },
                    "spacing": {
                        "padding": ".75rem 2.5rem"
                    },
                    ":hover": {
                        "color": {
                            "background": "transparent",
                            "text": "var(--wp--preset--color--dark-green)"
                        }
                    }
                }

    But I don’t see a way in theme.json to control the hover and border colors of the additional color palette options so I end up with something like this, which is not ideal.

    I have been adding styles as normal in style.css to fix the borders and hover effects. But I have to add !important to everything to override theme.json-based styles. Is this the correct way to do this? It feels wrong to partially declare styles in one file and override them in another.

    The :hover pseudo element exists but only for the default style. It does not seem like an option exists to use it for the defined color palette.

    • This topic was modified 1 year, 11 months ago by Jan Dembowski.
Viewing 1 replies (of 1 total)
  • Hi @pinksharpii ??

    I’m not sure I follow 100%, so apologies in advance if I’m on the wrong track.

    But I don’t see a way in theme.json to control the hover and border colors of the additional color palette options so I end up with something like this, which is not ideal.

    Are you asking here if you can set different border and hover colors via theme.json based on the user selected background color?

    If so, that is not currently possible through theme.json

    But I have to add !important to everything to override theme.json-based styles. Is this the correct way to do this?

    My understanding is that the ! important flag was required by theme.json and global styles so that user selections were honoured. Therefore if you do in fact need to override them in any way, it makes sense you’ll also have to resort to ! important.

    You can find some history on this in https://github.com/WordPress/gutenberg/issues/29985.

    It feels wrong to partially declare styles in one file and override them in another.

    With the release of WordPress 6.2 and Gutenberg 15.1, you can specify custom CSS for individual block types via theme.json. Which would in a lot of cases reduce the need for that second file.

    Unfortunately, in your case, if you are wishing to apply custom CSS to only a subset of buttons (those with a particular background color), then you’ll still need the style.css file.

    The :hover pseudo element exists but only for the default style. It does not seem like an option exists to use it for the defined color palette.

    If an issue doesn’t already exist, perhaps this would be a good one to raise on the Gutenberg repo. You can submit a new issue here.

    I hope that helps confirm or clarify things a little!

Viewing 1 replies (of 1 total)
  • The topic ‘Can button styles be targeted by background color in theme.json?’ is closed to new replies.