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 block-specific settings controls #22672

Closed
wants to merge 2 commits into from

Conversation

adamziel
Copy link
Contributor

@adamziel adamziel commented May 27, 2020

Description + Test plan

The problem
Let's imagine I'd like to add an additional MenuItem to block settings menu in a core/navigation-link block. I add the following snippet of code to the edit.js:

	<BlockSettingsMenuControls>
		{ () => <MenuItem> My test menu item <MenuItem> }
	</BlockSettingsMenuControls>

Nice! This should do the trick. Let's test with a navigation link selected:

Zrzut ekranu 2020-05-27 o 13 54 33

So far so good, maybe we can call it a day? Let's add one more link to be extra sure:

Zrzut ekranu 2020-05-27 o 13 55 59

Uh oh, not good! Let's se what happens when the parent navigation block is selected:

Zrzut ekranu 2020-05-27 o 13 55 39

Wow - that's unexpected.

Proposed solution

This behavior occurs because BlockSettingsMenuControls.Slot doesn't have any restrictions on block id - it just accepts all the fills from the entire component tree. This plays nicely with MenuItems that are supposed to be in every BlockSettingsMenu such as "duplicate" or "convert", but doesn't support specifying additional controls by the block itself.

This PR introduces a separation between the two - the BlockSettingsMenuControls fill now has two modes: block-specific (default) and global. Unless forAllBlocks property is set to true, the fill will have an instance-specific name and will not be used by any BlockSettingsMenu related to another block (or multi-block selection).

Note that making block-specific mode the default feels more natural, but it also breaks BC - any third party code relying on the default global behavior will no longer work. I'd really like to get a second opinion about this decision before merging.

With this PR applied, adding the snippet of code mentioned above results in a more intuitive outcome:

Zrzut ekranu 2020-05-27 o 14 02 25

Zrzut ekranu 2020-05-27 o 14 02 31

Zrzut ekranu 2020-05-27 o 14 03 10

If this approach gets positive feedback, I'd like to add some tests before merging this PR.

Types of changes

Breaking change (fix or feature that would cause existing functionality to not work as expected)

@adamziel adamziel added the [Feature] Block settings menu The block settings screen label May 27, 2020
@adamziel adamziel self-assigned this May 27, 2020
@adamziel adamziel added this to PRs in progress in Navigation editor May 27, 2020
@github-actions
Copy link

github-actions bot commented May 27, 2020

Size Change: +150 B (0%)

Total Size: 1.12 MB

Filename Size Change
build/block-editor/index.js 106 kB +130 B (0%)
build/edit-post/index.js 302 kB +10 B (0%)
build/editor/index.js 44.6 kB +10 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 3.4 kB 0 B
build/autop/index.js 2.83 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.48 kB 0 B
build/block-directory/style-rtl.css 788 B 0 B
build/block-directory/style.css 788 B 0 B
build/block-editor/style-rtl.css 11 kB 0 B
build/block-editor/style.css 11 kB 0 B
build/block-library/editor-rtl.css 7.2 kB 0 B
build/block-library/editor.css 7.2 kB 0 B
build/block-library/index.js 119 kB 0 B
build/block-library/style-rtl.css 7.48 kB 0 B
build/block-library/style.css 7.48 kB 0 B
build/block-library/theme-rtl.css 684 B 0 B
build/block-library/theme.css 686 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/index.js 190 kB 0 B
build/components/style-rtl.css 17.1 kB 0 B
build/components/style.css 17.1 kB 0 B
build/compose/index.js 9.31 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.42 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 771 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.11 kB 0 B
build/edit-navigation/index.js 6.62 kB 0 B
build/edit-navigation/style-rtl.css 857 B 0 B
build/edit-navigation/style.css 856 B 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/index.js 14 kB 0 B
build/edit-site/style-rtl.css 5.52 kB 0 B
build/edit-site/style.css 5.53 kB 0 B
build/edit-widgets/index.js 8.05 kB 0 B
build/edit-widgets/style-rtl.css 4.59 kB 0 B
build/edit-widgets/style.css 4.59 kB 0 B
build/editor/editor-styles-rtl.css 425 B 0 B
build/editor/editor-styles.css 428 B 0 B
build/editor/style-rtl.css 5.06 kB 0 B
build/editor/style.css 5.06 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.71 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.68 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

Comment on lines +62 to +63
// Let's use non-existent ID in case the context is missing
const clientId = context?.clientId || 'non-such-id';
Copy link
Contributor Author

@adamziel adamziel May 27, 2020

Choose a reason for hiding this comment

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

Do we have any assertion mechanism available? Using an non-existent ID is an okayish workaround but if it's used in a block-specific mode outside of block context I'd really prefer to provide a clear error message in the console (if not blow up entirely).

@gziolo
Copy link
Member

gziolo commented May 27, 2020

Did you try you to render this fill conditionally when the block is selected:

isSelected && (
    <BlockSettingsMenuControls>
        <MenuItem> My test menu item <MenuItem>
    </BlockSettingsMenuControls>
)

The issue is caused by the fact that you share one menu for all child blocks.

@adamziel
Copy link
Contributor Author

adamziel commented May 27, 2020

@gziolo this would solve the problem in the post editor, but there's also the experimental menu management screen where navigator items have an ellipsis menu that's clickable even when the block is not selected:

Zrzut ekranu 2020-05-27 o 15 40 47

In theory we could just select the block when the menu is clicked, but it would shift the focus from the navigator over to the editor area which would be a bad experience (especially for users using primarily their keyboards).

@adamziel
Copy link
Contributor Author

With requirements evolving rapidly, this PR may not be needed in the end. If I'm wrong, let's revisit. See also: #22089 (comment)

@adamziel adamziel closed this Jun 29, 2020
Navigation editor automation moved this from PRs in progress to Done Jun 29, 2020
@gziolo gziolo deleted the fix/block-specific-settings-controls branch June 29, 2020 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block settings menu The block settings screen
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants