Skip to content

Make theme.json cross-platform (web & mobile) #24165

@oandregal

Description

@oandregal

The experimental-theme.json specification structures the way themes provide styling information for blocks, among other things. However, as it currently stands, the value of the style declarations are evaluated as text that is passed down to the CSS output unmodified.

Take this example:

"core/heading/h1": {
    "styles": {
        "color": {
            "background": "red",
            "text": "var(--wp--preset--color--primary)",
            "link": "blue",
        },
        "typography": {
            "fontSize": "calc(1px * var(--wp--preset--font-size--huge))"
        }
    }
}

When processed (and the core, theme, and user data is merged) it'll be converted to a CSS stylesheet like this:

h1 {
    background: red;
    color: var(--wp--preset--color--primary);
    --wp--link--color: blue;
    font-size: calc(1px * var(--wp--preset--font-size--huge);
}

Note how the values of the style declarations are exactly the same. This works fine for the web, as long as it is a valid CSS declaration that browsers understand. However, this is problematic for supporting Global Styles on mobile (see). Issues that we have are:

  • Functions as calc are not supported.
  • CSS Custom Properties are not supported (var(--wp--preset--color--primary) but also attaching a new value for --wp--link--color per node).

Related context

For context, I also wanted to share some other things that are relevant to styles on mobile:

  • Not all CSS properties have mobile equivalents.
  • Mobile styles are attached to a component, we can't do "make all buttons red". At the moment, the mobile team is using react context to pass values, etc.
  • Current block styles are created based on the *.native.scss files + some attributes of the block (background color). Ex: create a post in a site you can access from mobile, add a button and change the background color to a custom color. Save and check that the mobile app shows the same color.
  • There's a related action item which is to to create a new endpoint for mobile to have access to the global styles data. I'll leave that for a different issue/PR for the mobile team to own.

Next steps

There are some areas I'd welcome some feedback:

  1. What style properties does mobile need to support for each block?

Colors seem to be the first target the mobile team is considering for block level styles. What about the others style properties that we have, would we be interested in supporting them in the future or aren't they relevant for the mobile app? I'm thinking on things like spacing/padding that are highly tied to the available canvas/viewport. For reference, this is the full list of style attributes we currently aim to support: colors (text, background, links, gradients), typography (font-size, font-family, font-weight, line-height), spacing (margin, padding). cc @pinarol

  1. We need to restrict the style declarations in theme.json to a subset of CSS, so we can reference every style prop to a raw value mobile can use. What would be the more complex declaration we want to support?
  • What would be the more complex use case we want to support in theme.json? Would it be enough for themes if we provide a mechanism that can substitute this declaration: calc(1px * var(--wp--preset--font-size--huge))? I mean, unit conversion + reference some variable stored somewhere. cc @kjellr

  • Can we use css-wide keywords such as initial, inherit, unset? cc @pinarol

Metadata

Metadata

Assignees

No one assigned

    Labels

    Global StylesAnything related to the broader Global Styles efforts, including Styles Engine and theme.jsonMobile App - i.e. Android or iOSNative mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change)

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions