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 defining the default style at runtime #16067

Closed
wants to merge 1 commit into from

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Jun 10, 2019

As part of improving the block style variations #7551 we need the ability to set the default style at runtime.

This PR adds the setDefaultBlockStyle low-level primitive.

Testing

Execute npm run test-unit.

  1. Compile this branch: npm install && npm run dev.
  2. Open the block editor and add a quote block.
  3. Open the InspectorControl and note that the default style is "default".
  4. Open the developer console and execute: wp.data.dispatch( 'core/block' ).setDefaultBlockStyle( 'core/quote', 'large' ).
  5. Add a new quote block.
  6. Open the InspectorControl and note that the default style is the large one.

So far, the new block doesn't reflect the large style. The reason is that the CSS classes for default block styles don't get applied to the block until the user specifically clicks the style. Because the style class is-style-large is not applied, the CSS for the block is still the initial default style, not the one defined at runtime.

@oandregal oandregal self-assigned this Jun 10, 2019
@oandregal oandregal added [Feature] Theme Style Variations Related to style variations provided by block themes [Status] In Progress Tracking issues with work in progress labels Jun 10, 2019
@oandregal oandregal changed the title Add setDefaultBlockStyle primitive Allow defining the default style at runtime Jun 10, 2019
@oandregal
Copy link
Member Author

When a block is first added it doesn't get the style class is-style-X applied. The user has to click one of the block styles for the corresponding class to be added. At the CSS level, we cover for this lack of default class by providing the default styles to the block itself.

Take the quote block as an example. Essentially, what we do is:

.wp-block-quote {

    // rules for the default style, to cover the fact that the block
    // doesn't get the `is-style-default` class applied

    &.is-style-large {
        // rules for the large style
    }
}

What if we always added one style class to the block, even for the default style?

With this approach, the className attribute for the block would get:

  1. the block class name. Ex: wp-block-quote.
  2. the style that is applied (or the default if none). Ex: is-style-large.
  3. the user-defined classes in the Advanced class text field control.

Currently, block styles are exposed in the Advanced CSS class text field in the block sidebar. The user can edit and remove those classes. If we want to make sure one of them is always applied, it makes sense not to expose them in the Advanced CSS class component.

Does this approach looks like something we want? Are there any other issues to be considered?

@oandregal
Copy link
Member Author

From this slack convo in core-editor: currently, this state lives in core/blocks and is created from the static definition of the block. If we allow for it to change, we need a way to persist it across sessions. Open questions: could this live in the localStorage as a first step? is this more user state than it is application state?

@oandregal
Copy link
Member Author

Cleaning up my lingering PRs. We can reopen if necessary.

@oandregal oandregal closed this Oct 30, 2019
@oandregal oandregal deleted the add/set-default-block-style branch October 30, 2019 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Theme Style Variations Related to style variations provided by block themes [Status] In Progress Tracking issues with work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant