-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
The Slot/Fill feature depends on a singleton registry in components package #27191
Comments
This appears to be a good solution. The reasons for WC Admin to bundle The reason I'm saying this is because going through a major refactor for the purposes of letting consumers bundle Having said that, moving SlotFill to its own package creates a nice temporary solution while we work to unbundle |
@psealock No matter whether WC Admin decides to unbundle After separated, import { startsWith } from 'lodash';
import { addFilter } from '@wordpress/hooks';
addFilter( 'blocks.registerBlockType', 'enhance-core-blocks', ( settings, name ) => {
if ( startsWith( name, 'core/' ) ) {
settings.edit = enhancedEdit( settings.edit );
}
return settings;
} ); The plugin adds a filter that intercepts blocks registration and enhances the editor UI for Here, the The Similarly, you might want to bundle Another way to frame the idea is that bundling a package should have predictable consequences. By bundling Bundling |
I forgot to comment on this issue. Thank you for opening this discussion and sharing your perspective which I can totally relate to. There are a few issues with It's also worth mentioning that internally there are two different implementation of SlotFill. @diegohaz and @youknowriad should have the best advices which API to extract and promote moving forward. We can also discuss technical aspects like hiding or getting rid completely of strings used for SlotFill in the exploratory PRs. For backward compatibility we can always create mapping layer. The truth is that those strings only create issues 😃 |
I did the This should make |
This is actually something I already built in an old PR for another reason but closed it because I didn't feel that there were big benefits.
I'm not sure that's a great idea tbh. Just more deprecations and still requires backward compatibility which means the components package still holds a singleton. I personally wonder if the main issue here is that a WP plugin is bundling something that already exists in WPAdmin. So I'm not sure we should make changes in the package itself. |
I think the
I've thought about using the DOM as the source of truth in this case. Instead of using string keys and context, we would pass DOM elements or selectors to Returning separate contexts from |
There is always a middle ground solution for React Native, we move the non-portal implementation to |
After this change, the We don't need to deprecate the
There's also the case where someone uses the NPM-published packages to build their own editor, and needs to manage the possible duplication of |
@jsnajdr Just checking back through some older issues - I just wanted to check if this is an ongoing concern, or if it has been resolved otherwise? |
Yes, I think it's still a good idea that |
Steps to reproduce:
@wordpress/components
package for better Core compat.Slot
/Fill
pair from that plugin.@wordpress/components
Actual result:
The slots and fills will never materialize in the UI, because the
wc-admin
plugin has its own instance of a React context, different from the default one inwp-admin
. Bundling@wordpress/components
created a duplicate instance of the context, which effectively acts as a singleton registry for slots and fills.More on the issue
The core issue seems to be that when
@wordpress/components
creates a React context by callingcreateContext
and exports it, that context becomes a singleton that needs to be carefully managed. That singleton serves as a registry where all slot/fill pairs are registered byname
. It becomes very similar to the@wordpress/data
or@wordpress/hooks
packages that also maintain a centralized registry of things. That obviously prevents them from being safely bundled.Possible solution 1
The ultimate framework-level solution would be to remove that singleton, and make
@wordpress/components
export only thecreateSlotFill
function, not an instance of the context. EachcreateSlotFill
would create its own context (I think it would be even a performance improvement), managed by the specific package rather than by@wordpress/components
library.This, however, is a breaking API change, because we can no longer reference a slot by string name. A string name implies that it's a key into some registry. We can't avoid a singleton and have string names at the same time. A similar change was done by @gziolo recently in
@wordpress/data
: deprecating store names in favor of store objects.The downside is also that
createSlotFill
would have to return a triplet:Slot
,Fill
andSlotFillContextProvider
that connects them together. The app would have to render a potentially big number of context providers at the top, one for each slot/fill pair that it uses. That might prove untenable.Or maybe it's possible to implement slot/fill without a context, making the slot and the fills communicate in some different, more direct way? Cc @diegohaz who is the architect of the latest "bubble-virtually" implementation.
Possible solution 2:
Another solution would be to keep the singleton, but move the slot/fill feature to a separate package. Making it clear that it contains a singleton registry (like
data
orhooks
), and therefore makingcomponents
a bundle-able library again.Workaround:
Reexporting the provider from the
navigation
package looks like a good workaround. Note that with the registry-less solution described above, we'd be forced to export the provider, too, because it's now an unique part of the API, together with theSlot
and theFill
.The issue was originally raised by @psealock in a private Woo-related discussion.
The text was updated successfully, but these errors were encountered: