Use different MenuItem props for plain text and rich text labels #6752
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt to fix #6740.
The issue described there was caused when the
label
prop was changed fromstring
toComponentChildren
in #6667.The
label
prop is used used for both the menu content and the submenu toggle button title, and it was changed toComponentChildren
so that a new icon could be added without breaking the existing icon logic. However, for the toggle title it needs to be a string.The solution proposed here involves adding a new optional
richLabel
prop, with typeComponentChildren
, and makelabel
bestring
.If
richLabel
is not provided,label
is used everywhere, as before #6667 was merged, but if it is provided, then it is used for the menu content, whilelabel
is still used for the toggle title.Drawbacks
This solution is not perfect, as it requires leaking some internal details (the fact that
label
is used as a title, and therefore it needs to be a string). Furthermore, if bothrichLabel
andlabel
are provided for a menu item without a submenu, thelabel
is not even used, but it's required, which is confusing.However, this was the simplest solution, considering MenuItem's props API is relatively complex.
Alternatives
Some other alternatives I considered are:
react-render-to-string
or some other way to stringify thelabel
prop, so that we can "extract" the part we need from it for the title, while we keep it asComponentChildren
. However, this seemed hacky, and there's no way to know what's going to be the structure, so it's also a bit brittle.Ultimately, it was hard to remove html tags when stringifying, so I discarded this approach.
icon
prop and keeplabel
asstring
, but then I realized that prop already exists.Then I thought we could maybe deprecate
icon
and replace it withleftIcon
andrightIcon
, but then I realized theicon
conditionally renders on the left side for top-level menu items, and on the right side when they are part of a submenu, so this would also be quite convoluted to achieve.