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

Force HTML blocks to be in preview mode when in a shared block #7138

Merged
merged 1 commit into from
Jun 8, 2018

Conversation

noisysocks
Copy link
Member

Fixes #4875. Supersedes #5298.

Introduces Disabled.Consumer so that components are able to tell if they are wrapped in a <Disabled> component.

We then use this functionality to force HTML blocks to render in preview mode when disabled. This means that HTML blocks always render in preview mode when they are shared which is a nice UX enhancement.

html-preview

How has this been tested?

I unfortunately had to temporarily skip all of the Disabled tests because Enzyme does not support React 16.3 APIs as of yet (enzymejs/enzyme#1513).

I manually tested by:

  1. Create a HTML block
  2. Convert it to a shared block—it should now be in preview mode
  3. Edit the shared block—it should now be in edit mode
  4. Toggle the block between preview and edit mode

Introduces Disabled.Consumer so that components are able to tell if they
are wrapped in a <Disabled> or not. Then, use this to force HTML blocks
to render in preview mode when disabled. This will be the case when in a
shared block.
@noisysocks noisysocks added [Type] Enhancement A suggestion for improvement. [Feature] Blocks Overall functionality of blocks [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) labels Jun 5, 2018
@noisysocks noisysocks added this to the 3.1 milestone Jun 5, 2018
@noisysocks noisysocks requested review from gziolo and aduth June 5, 2018 03:49
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to understand what kind of magic is happening in this PR. I like this usage of Context API. I must admit it nicely takes advantage of the virtual DOM tree scoping to inform child blocks that they should be in disabled state ❤️

It also tests well, very nice improvement. Can we use it also with other blocks that have preview mode, maybe shortcodes?

@noisysocks noisysocks merged commit d44f116 into master Jun 8, 2018
@noisysocks noisysocks deleted the add/force-html-block-to-preview-when-shared branch June 8, 2018 02:14
@noisysocks
Copy link
Member Author

Thanks @gziolo! To my knowledge, the HTML block is the only block that has a distinction between edit mode and preview mode when viewed in the visual editor.

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this approach! 👍

<div ref={ this.bindNode } className="components-disabled">
{ this.props.children }
</div>
<Provider value={ true }>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, if we set the default value for context above to true, there's no need to pass value at all.

i.e.

const { Consumer, Provider } = createContext( true );

// ...

return <Provider>{ /* ... */ }</Provider>;

Demo: https://codepen.io/aduth/pen/KeNGjL

@gziolo
Copy link
Member

gziolo commented Jun 8, 2018

@aduth, the thing is that you want to default to false so you could override the value with the consumer with true only for selected branches - shared block in edit mode as of this PR.

@aduth
Copy link
Member

aduth commented Jun 8, 2018

Oooh right, makes sense. I guess I forgot that the consumer is expected to be used even when a provider ancestor doesn't exist (i.e. detect that element is, in-fact, not disabled).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants