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

Add filter for the Preferences modal sections #29825

Closed

Conversation

Mamaduka
Copy link
Member

@Mamaduka Mamaduka commented Mar 12, 2021

Description

Introduces new editorPost.PreferencesModal.sections filter. Using this filter, plugin authors will be able to add/remove sections from the "Preferences" modal.

Note: Currently preference modal Section component isn't exported, so I had to copy it for testing. What's the best option to make this available for plugins?

Fixes #28970 and #16401.

How has this been tested?

With custom code that uses this filter.

Custom code
import { addFilter } from '@wordpress/hooks';

const Section = ( { description, title, children } ) => (
	<section className="edit-post-preferences-modal__section">
		<h2 className="edit-post-preferences-modal__section-title">
			{ title }
		</h2>
		{ description && (
			<p className="edit-post-preferences-modal__section-description">
				{ description }
			</p>
		) }
		{ children }
	</section>
);

function addPluginSection( sections ) {
	const pluginSection =
		{
			name: 'plugin-section',
			tabLabel: 'Plugin Section',
			content: (
				<Section title={ 'Plugin Section Title' }>
					<p>Plugin Section Content</p>
				</Section>
			),
		};
	
	// Remove "General" sections for testing purposes.
	const newSections = sections.filter( ( section ) => section.name !== 'general' );

	return [ ...newSections, pluginSection ];
}

addFilter(
	'editorPost.PreferencesModal.sections',
	'mamaduka/addCustomSections',
	addPluginSection
);

Screenshots

plugins-custom-preferences

Types of changes

New feature

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@Mamaduka Mamaduka force-pushed the try/preferences-modal-filter branch from a5030c1 to ef11fee Compare March 15, 2021 16:07
@Mamaduka
Copy link
Member Author

I've updated the filter name to editorPost.PreferencesModal.sections since it will include all sections, not just core ones.

@Mamaduka Mamaduka added the [Type] Enhancement A suggestion for improvement. label Mar 17, 2021
@Mamaduka Mamaduka self-assigned this Mar 18, 2021
@gziolo gziolo added the [Feature] Extensibility The ability to extend blocks or the editing experience label Apr 1, 2021
@gziolo gziolo requested review from gziolo, mcsf and youknowriad April 14, 2021 13:43
@gziolo
Copy link
Member

gziolo commented Jul 12, 2021

I'm all in favor of making the Preferences modal extensible. That's a really great idea. Thank you for starting this PR with the initial proposal.

It feels like we could benefit from creating a more formal API that would be similar to how you work with the custom sidebars using SlotFill as the foundation. There is several plugins available that use the same pattern:
https://github.com/WordPress/gutenberg/tree/trunk/docs/reference-guides/slotfills

Aside from that, the biggest question is whether we want to allow adding extensions only to the Plugins Section tab or whether it should be possible also to add more tabs. Assuming that it's only a single tab to extend, then we should think about more formal ways of defining where the custom components get rendered – each plugin defines in their own section with a label that can contain more granular subsection maybe?

@Mamaduka
Copy link
Member Author

Thanks for the feedback, @gziolo.

I agree that SlotFills as a foundation matches the general pattern used in core better.

This also would be beneficial if we end up extracting editor Preferences to the Interface package (#31965). I assume not all editors would have the same sets of preferences, and they might want to extend it as needed.

/cc @talldan

@talldan
Copy link
Contributor

talldan commented Jul 13, 2021

Yeah, I think a slot/fill makes sense. It's a longer term thing, but it might be good to consider various APIs.

I'm thinking:

I'm still very early into the work on #31965, but will try to share a draft PR in the next few days. It doesn't have any overlap with this PR, but it will move towards a more generic idea of a preference.

@talldan
Copy link
Contributor

talldan commented Jul 30, 2021

@Mamaduka Here's that draft PR, sorry it took a bit longer than I said it would - #33774

I think the preferences modal could be looked at in a follow-up.

At the moment my plan is to move everything into the interface package, but that might change depending on feedback. Interface does already have some good well defined abstractions for extensibility with its complementary areas and pinned items, so I could see a slot/fill for preferences following a similar pattern.

@Mamaduka
Copy link
Member Author

Thanks, @talldan.

I'll have a closer look next week.

@Mamaduka Mamaduka closed this Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a filter to control the list of Block Editor preferences
3 participants