Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Meta issue: #1980004: [meta] Creating Dream Markup
The markup for Menu's should be improved. We suggest the following markup for Stark and Bartik:
Suggested markup in Stark:
<nav role="navigation">
<ul class="menu">
<li class="is-active">
<a>Current page</a>
</li><li>
<a>Another page</a>
</li><li>
<a>A section with subnav</a>
<ul>
<li>
<a>Submenu item</a>
</li>
</ul>
</li>
</ul>
</nav>
Suggested markup in Bartik:
<nav role="navigation">
<ul class="menu menu--level-1">
<li class="menu__item is-active">
<a class="menu__link">Current page</a>
</li><li class="menu__item">
<a class="menu__link">Another page</a>
</li><li class="menu__item">
<a class="menu__link">A section with subnav</a>
<ul class="menu menu--level-2">
<li class="menu__item">
<a class="menu__link">Submenu item</a>
</li>
</ul>
</li>
</ul>
</nav>
Comments
Comment #1
oresh CreditAttribution: oresh commented<strong>{{ item.link.content }}</strong>
- not good. If you don't want it bold, you'll have to go to defaults. Better:<span>{{ item.link.content }}</span>
.Also need different classed. Maybe something like this?
Comment #2
mortendk CreditAttribution: mortendk commentedsounds reasonable but i would like to kill somethings:
the .menu__list-item we can easyli target with a .menu > ul li {}
i would like if we used the same name for active elements all over so instead maybe us .active instead of menu__active
Comment #3
chippper CreditAttribution: chippper commentedIt's a minor point, but I honestly don't think you need the span at all. Could you not do something like:
I agree that .active is better than .menu_active - I think the latter might introduce confusion more than anything.
Comment #4
LewisNymanThe CSS standards recommend that we don't make classes too broad. We should use
menu-item--active
Comment #5
ry5n CreditAttribution: ry5n commentedI have a couple of different takes on this. I’ve omitted any Twig to focus on the markup.
- Added an ARIA role to the nav
- Removed default classes from the
<nav>
and moved.menu
to the<ul>
. The list is what repeats, so the 'menu' class is more useful there. I’ve not added new classes to the nav in the spirit of 'start with nothing'.- Each item in the menu tree has a class following the new CSS standards. However, there is a case to be made for
.menu > li
instead of.menu__item
.- States follow the new standards too, with
.is-active
for the active item. (State classes are the only place we should see qualified selector in core, i.e..menu__item.is-active
. The extra specificity is useful for states but is a detriment in most other cases.)- Menu levels are indicated by a class modifier, since they are variants of the menu component.
If we wanted to embrace listless navigation, we could even consider this:
Comment #6
minneapolisdan CreditAttribution: minneapolisdan commentedI really like this idea of listless navigation. Having listening to a screen reader speak outloud the endless "list" declarations, especially when tabbing through an extensive navigation structure (e.g. many, many lists within lists, many sublevels), it does make sense to reconsider this output. After all, we are putting these in lists in the first place partly for accessibility, right? And for semantics, the
<nav>
element does address that concern.Comment #7
minneapolisdan CreditAttribution: minneapolisdan commentedYou guys may already be way ahead of me on this, but I was interested in this topic of using HTML lists for menus (or not). And it seems to be a big part of all this code we're writing for twig templates.
So I asked some accessibility folks, and started a post here, on the Accessibility Group page
The consensus seems to be to stick with HTML Lists.
Again, you all may already know this, but on the chance you do not... now you do.
Comment #8
star-szrMoving to core queue.
Comment #9
star-szrActually markup is a better component.
Comment #10
oresh CreditAttribution: oresh commentedLet's stay with the ul > li variant now.
We should better focus on classes and semantics.
@ry5n - how an item without a link will look like?
In D7 in all my projects I add something like that:
So I usually add the depth to each item and not the parent. Also I add the menu name to the item - that makes theming the menus really great. Adding depth on the actual li can reduce the selector target to 1 item:
Any thoughts on this?
Comment #11
mortendk CreditAttribution: mortendk commentedremember that we wanna keep markup as clean as possible outta core
imho that means that we only wanna add "active" class to the stark theme right ? & then add all the other stuff to bartik
The end result should be its easy for whomever to create exactly the markup they wanna do for a given menu, without beeing forced into whatever desissions we are taking right now
Comment #12
ry5n CreditAttribution: ry5n commentedYes, and yes. Especially important for giving us all the ability to mark things up how we want is to ensure that the data structure containing the actual navigation tree is easy to retrieve and manipulate. (But I suppose that’s another story.)
Comment #13
Jeff Burnz CreditAttribution: Jeff Burnz commentedJust active/active trail classes sound good, and no CSS please, or silly triangles on .collapsed items, but you might get some push back removing collapsed classes. leaf, first, last all can go imo.
I'm happy if we have no line endings \n or white space between li elements, I don't wanna be floating li's for another 5 years.
Comment #14
jenlamptonCan someone pleas update the summary with the current requested syntax so someone can start working on this?
Comment #14.0
jenlamptonMeta issue added.
Comment #15
BarisW CreditAttribution: BarisW commentedDone, if someone can confirm the suggested markup in the issue summary I'm happy to work on a patch right-away.
Comment #15.0
BarisW CreditAttribution: BarisW commentedUpdates issue summary
Comment #15.1
LewisNymanUpdated Bartik markup
Comment #16
LewisNymanOk, I've updated the mark up in the summaries to remove white space between the list items based on this article.
Comment #17
BarisW CreditAttribution: BarisW commentedCool, thanks. I'll work on it.
Comment #18
BarisW CreditAttribution: BarisW commentedSeems that we need a major API change for this:
Tasks
- Remove theme_menu_tree
- Remove theme_menu_link
- Update template_preprocess_menu_tree to add extra twig variables (menu_level, items).
- Add twig file (/core/modules/menu/templates/menu-tree.html.twig)
Also, why do we use a theme_menu_tree and a theme_links? And why do we use theme_links for the main_menu? Shouldn't we fix this as well?
Why does my Tools menu is an
ul.menu
and my Main menu anul.links
?Comment #18.0
BarisW CreditAttribution: BarisW commentedUpdated Stark mark up
Comment #19
mgifford@BarisW any ETA on the patch?
Comment #20
BarisW CreditAttribution: BarisW commented@mgifford. I've unassigned myself from the issue. This goed way above my head ;)
Some work is done is this issue: #1777332: Replace theme_menu_link() and menu-tree.html.twig with a single Twig template, so we might want to close this one here to keep the focus in one place?
Comment #21
mgifford#890362: Links should not be indicated by color only is waiting on a resolution of this issue.
Looks like there's been no direct means to address #1777332 which also stalled in October.
Comment #22
Jeff Burnz CreditAttribution: Jeff Burnz commentedthis might not finalise until #1777332: Replace theme_menu_link() and menu-tree.html.twig with a single Twig template which is waiting on #1898478: menu.inc - Convert theme_ functions to Twig
Comment #23
LewisNymanComment #24
mortendk CreditAttribution: mortendk as a volunteer commenteda little cleanup here:
After the updates to the menu.html.twig this issue seems to not be relevant anymore.