Skip to content

Consider allowing themes to enqueue stylesheets in theme.json #26902

Open

Description

@kjellr

If all of the add_theme_support() options from #26901 are incorporated into theme.json, the only things a theme must include in a functions.php file are its stylesheets.

For the sake of discussion, would it be possible (and a good idea) to allow block-based themes to define their front-end and editor stylesheets directly from within theme.json?

Activity

  1. added
    [Feature] ThemesQuestions or issues with incorporating or styling blocks in a theme.
    [Type] DiscussionFor issues that are high-level and not yet ready to implement.
    on Nov 11, 2020
  2. scruffian commented on Nov 12, 2020

    @scruffian
    Contributor

    Will themes still need style sheets?

  3. kjellr commented on Nov 13, 2020

    @kjellr
    ContributorAuthor

    Many themes will still have stylesheets for things like transition and animations, etc. Global Styles won't cover all of those use cases.

    But at a baseline, they'll need a style.css to house the standard DocBloc. WP won't currently recognize a theme without one. (I guess this could be changed. 😄)

  4. scruffian commented on Nov 13, 2020

    @scruffian
    Contributor

    In which case can we just always enqueue style.css without needing a line in functions.php? Other stylesheets could be @imported into style.css...

  5. kjellr commented on Nov 13, 2020

    @kjellr
    ContributorAuthor

    Yeah, currently, @imported stylesheets aren't loaded into the editor at all, so that would need to be changed. I filed #26963 to discuss that point.

  6. jorgefilipecosta commented on Nov 13, 2020

    @jorgefilipecosta
    Member

    The issue with imported stylesheets is that a plugin would have no way to not enqueue a specific stylesheet.

  7. jorgefilipecosta commented on Nov 13, 2020

    @jorgefilipecosta
    Member

    But at a baseline, they'll need a style.css to house the standard DocBloc. WP won't currently recognize a theme without one. (I guess this could be changed. 😄)

    I think we should allow the docblock to come from theme.json in order to allow a theme without any CSS and just with a theme.json file.

  8. jorgefilipecosta commented on Nov 13, 2020

    @jorgefilipecosta
    Member

    I think it would be a good idea to allow theme.json to load external stylesheets.
    I imagine something like:

    styles: {
    	"$handle": {
    		"src": "/assets/alignments-front.css",
    		"ver": (optional contrary to wp_enqueue_style defaults to the theme version instead of the core version WordPress version, we know that on theme.json we are in theme code and the theme version is more important).
    		"media": (optional same as wp_enqueue_style media)
    	}
    }
    

    Styles loaded in this way would behave exactly like the styles added in PHP, and plugins could interact with them in the same way.

    cc: @mtias, @youknowriad

  9. youknowriad commented on Nov 13, 2020

    @youknowriad
    Contributor

    I don't know if this is a good idea, I need to think about it more but what I know is that the capabilities of theme.json are growing constantly and IMO before thinking of adding new features, let's make sure what we already have works perfectly. (For instance, do we really want to show all global styles UI like we have now, or do we want it to be controlled by the theme?)

  10. oandregal commented on Nov 18, 2020

    @oandregal
    Member

    I'd say the theme.json model (configs per block/context) is at odds with loading stylesheets that don't match those elements. I'd also refrain from trying to do this at the moment.

    A different approach that was mentioned #26901 is whether we can/should do some things automatically (like enqueueing block styles, etc) if a theme has a theme.json. I think this could be good to explore to understand the trade-offs better.

  11. oandregal commented on Nov 18, 2020

    @oandregal
    Member

    Cross-referencing this convo WordPress/theme-experiments#81 (comment) TLDR: if we want to remove the stylesheets, we have to update the logic for what WordPress core & WordPress theme directory considers a minimum theme.

  12. jorgefilipecosta commented on Nov 23, 2020

    @jorgefilipecosta
    Member

    For instance, do we really want to show all global styles UI like we have now, or do we want it to be controlled by the theme?

    Hi @youknowriad,

    The UI controls are already controlled by the theme right now.

    • to disable color options: a theme can pass an empty array of available colors and disable custom colors.
    • to disable font sizes, a theme can pass an empty array of font sizes, and disable custom font sizes.
    • to disable line-height, a theme can set customLineHeight to false.
    • to disable padding, a theme cans set customPadding to false.

    Themes can control all these settings at the global level and at the block level, e.g., a theme can disable all these settings at the global level but enable for specific blocks or the contrary.
    There is no difference between enabling/disabling at the global styles or block level. E.g., if a theme disables line height for a paragraph, it will stop being available in the paragraph block inspector and in the global styles paragraph panel.

    Would you recommend a change in the API's we have to disable UI elements from the user, or is there anything you think is missing?

  13. youknowriad commented on Nov 24, 2020

    @youknowriad
    Contributor

    How do I do that now?

    • Enable color options, font sizes, line-height and padding everywhere in the post editor.
    • Only allow the user to change the "main color" + "global font sizes" at the site editor level.
  14. overclokk commented on Jul 17, 2021

    @overclokk

    @import inside a stylesheet should never being used.

  15. oandregal commented on Dec 21, 2021

    @oandregal
    Member

    I learned that we have an issue with dynamic styles. It affects both the ergonomics of theme authors and the iframe work: #18571

    In talking to @ellatrix about this, a system along the lines of @jorgefilipecosta proposal above could help address all these issues:

    • themes won't need to use functions.php to enqueue styles
    • there's a handle that we can use to load styles in the iframe
    • themes that want to use dynamic styles have also a handle they know will be managed for them (either to load them in the iframe or to wrap them with the .editor-styles-wrapper class, which is something that's lacking with the current add_editor_styles wrapper).

    I was initially thinking that we could use a similar method as of the block.json for blocks: just having a stylesheets key to declare a handle. But it sounds like allowing to declare the styles to be enqueued solves more issues.

  16. ellatrix commented on Aug 17, 2023

    @ellatrix
    Member

    Would be cool to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] ThemesQuestions or issues with incorporating or styling blocks in a theme.[Status] Not ImplementedIssue/PR we will (likely) not implement.[Type] DiscussionFor issues that are high-level and not yet ready to implement.[Type] EnhancementA suggestion for improvement.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions