Repository navigation
Description
Context
- Related to
@wordpress/components: modality of popover-based components #63674 - Related to Popover-based UI: do not render in Popover.Slot by default #56482
As we're re-writing some of the components in the @wordpress/components package to use ariakit, we have the chance to re-discuss our approach to architecting our APIs and the behavior of such components.
More specifically, as we're working on the new version of DropdownMenu and CustomSelectControl, one of the aspects that we should discuss and agree on at a package level is where in the DOM popover-based components should render.
This is, at the time of writing, the behavior of the first-party Popover component (and all components based on it) with regards to where it renders in the DOM:
- if the
inlineprop istrue, it renders inline (ie. in the same DOM tree as its parent React component); otherwise, - in the slot defined through the
__unstableSlotNameProviderReact context (code); - if no slot is found in the context, it renders in the slot defined via the
__unstableSlotName - if the
__unstableSlotNameis not set, it renders in thePopover.Slot; - if the slot declared via any of the above ways can't be found through the
useSlothook, it fallbacks to rendering in adiv.components-popover__fallback-containerappended to thedocument.body.
This behavior can be changed via the __unstableSlotNameProvidercontext and __unstableSlotName prop (to pick another slot), and the inline prop (when set to true, the popover will render inline instead of in a slot).
Note
Slot is a Gutenberg-specific term and refers to the SlotFill APIs, a technology that exposes React's portal functionality as a set of React components. You can read more about it in this article, or you consult the official docs and the Storybook examples.
Using ariakit allows us to easily tweak, if necessary, this aspect for popover-based components (popover, tooltips, dropdowns, dropdown menus, dialogs, selects, comboboxes...) via the portal and portalElement props.
This topic was partially discussed in a related issue, where an initial consensus was reached on changing the default behavior of the Popover component to append to document.body by default.
What
Now that we're adding a new set of ariakit-based components, we have the chance of making changes to the behaviors explained above.
Therefore, we need to decide:
- Where in the DOM should popover-based component render by default? Inline, or portal-ed? If portal-ed, where in the DOM?
- Should we expose APIs in our components to change such behavior? If yes, for what components and in what shape?
- How would the new behavior and APIs relate to the
SlotFillAPIs? - How does that impact layering and nesting popovers, and is it enough to stop relying on z-index by default?
- Do we need to take any additional precautions into account, given that there will be a transional phase during which some components will use the ariakit popover, and other components will use the first-party
Popoverfrom this package?
cc @WordPress/gutenberg-components
Activity
youknowriad commentedon Jul 18, 2024
Quick side question: What are we hoping to gain in terms of UX by switching some of these low level components to ariakit?
ciampo commentedon Jul 18, 2024
Here are my thoughts:
I think that
Popoverand popover-based components should all render in a portal appended to thedocument.bodyby default.All popover-based components could expose two props, mimicking
ariakit's APIs:portalboolean prop. Whentrue, the component is portal-ed. Whenfalse, the component is rendered inline;portalElementprop (which defaults to a div appended to thedocument.body), used to pick a different portal root.I think that the popover-based components should not use
SlotFillAPIs internally, nor in their public APIs (ie. having aslotNameprop).Keeping
SlotFilland popover-based components decoupled simplifies the learning curve and allows for better flexibility and interoperability, especially when the components are rendered outside of the block editor (ie. dataviews and other admin UI).The
portalElementaccepts any HTML element — and therefore, consumers who wish to render a popover-based component in aSlotcould simply do the following:The
__unstableSlotNameProvidercontext mechanism should not replicated inside the popover-based components either, and an equivalent React context should instead be used by consumers of the component as needed.One limitation of current popover-based components is caused by the fact that they rely on their
z-indexto render on top of existing UI, which is a brittle and inflexible technique.If all components get appended to the
document.bodythey should intrinsically render on top of most UI. Also, rendering popovers on top of popovers should also intrinsically work as expected as it would rely on DOM order.Having said that, there may be instances in which a
z-indexwill still be needed — although that should ideally be a tweak made by consumers of the component on a case-by-case basis, and not added systematically to the components library.We may have to retain the
z-indexstyles during the transition period to ensure that popovers with different underlying implementations still layer on top of each other as expected.One thing to keep in mind is that both
ariakitand our first-party popover are based on the@floating-uilibrary, which is a good common layer to have between the two.ciampo commentedon Jul 18, 2024
There are a few reasons, but specifically to the UX side, using a headless component library (in this case,
ariakit) allows us to delegate the task of providing standard, well-functioning, modular and accessible component base implementations, instead of having to build and maintain them ourselves. A few examples:Tabscomponent, thanks to its modularity, can be used across the editor in places whereTabPanelcouldn't be used, thus implementing standard tab interfaces in more places;CustomSelectControlimproved the semantics and keyboard accessibility of the component;DropdownMenusupports nested submenus, a highly requested feature that would have been expensive to implement properly in a first-party component;tyxla commentedon Jul 18, 2024
Thanks for opening the discussion, @ciampo 🙌
I agree with all your suggestions:
portalandportalElementmakes sense and provides us with enough flexibility while not overcomplicating or providing too complex low-level APIsyouknowriad commentedon Jul 18, 2024
@ciampo Thanks for the details Marco. Those are good reasons but nothing clear on the UX side. I'd love if we can focus more on unlocking more UX improvements from our components and refactoring as a mean not as a goal.
ciampo commentedon Jul 19, 2024
@youknowriad we are definitely aligned on this, the main goal of all of this work is to ultimately provide a better UX for our users!
These specific conversations that we're having right now about modality and portals are more of a consequence/opportunity of using ariakit and are quite technical, but they could also have UX implications, for example in avoiding issues like this one, this one, or this one.
Having more components based on the same underlying headless component library (for example, ariakit) gives us several advantages that, in my opinion, all contribute to a better UX all around:
With this in mind, the above examples can be considered improvements on the UX side.
ciampo commentedon Sep 3, 2026
Closing as we're basically working on this strategy inherently by providing a new set of overlay components in
@wordpress/ui. All components are based on the same underlying technology stack (base ui -> floating-ui).Progressively refactoring Gutenberg to use these components will take time, but the end result should allow us to remove a lot of complexity (especially around portal and z-index management) and have fewer stacking bugs.