Menus, Dialogs, and Tooltips, oh my!
In any web... | Yahoo Engineering
Menus, Dialogs, and Tooltips, oh my!
In any web application, we need a generic pattern for reusable widgets that display above the main content of the page. We previously blogged about how we’re using React and Flux to rewrite the Yahoo Mail web application. The Mail application uses widgets like menus, notifications, dialogs, and tooltips. We considered three different ways we could solve this:
Rendering widgets as children of React components
Rendering widgets using Flux
Rendering widgets using a mixin
Rendering widgets as children
The simplest approach would be to have components include these widgets as children, then use z-index to display the widget on top of the page. But, this solution is very fragile because of the nature of z-index and stacking contexts. If a developer accidentally creates a stacking context with any parent HTML element of our widget, the z-index of our widget will not matter because the widget can only display on top of elements within its own stacking context. If there exists another stacking context that has a higher z-index than the widget’s stacking context, the elements within the other stacking context will always display on top of our widget. In Yahoo Mail, we wanted to improve the predictability of the application. So, we decided against this approach because it could lead to unintended consequences if a developer does not have a good understanding of stacking contexts.
To avoid the stacking context problem, we have to attach the widgets to document.body. Because document.body is the highest stacking context in our app, we guarantee that all elements attached to it will be stacked according to z-index.
Rendering widgets using Flux
The next approach we considered was to use Flux to display the widgets. We had a WidgetControllerView that contained the reference to the widget’s Store and rendered the widget’s Components, if necessary. Our AppControllerView rendered the WidgetControllerView. The application’s React components triggered actions (through action creators) that the widget’s Store listened to.
This approach works, but it seems unnatural for the widget use case. Our developers all wanted to render a <Menu> or a <Dialog> from within the components themselves; they did not want to fire an action. The Flux flow led to calls like the following when a developer wanted to render a menu widget:
MenuActionCreator.showMenu([
{
label:
Yahoo Engineering