diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTree.php b/core/lib/Drupal/Core/Menu/MenuLinkTree.php
index 090293c..518534f 100644
--- a/core/lib/Drupal/Core/Menu/MenuLinkTree.php
+++ b/core/lib/Drupal/Core/Menu/MenuLinkTree.php
@@ -243,26 +243,24 @@ protected function buildItems(array $tree, CacheableMetadata &$tree_access_cache
       if ($data->access instanceof AccessResultInterface && !$data->access->isAllowed()) {
         continue;
       }
+      $element = array();
 
-      $class = ['menu-item'];
-      // Set a class for the <li>-tag. Only set 'expanded' class if the link
+      // Set a variable for the <li>-tag. Only set 'expanded' true if the link
       // also has visible children within the current tree.
       if ($data->hasChildren && !empty($data->subtree)) {
-        $class[] = 'menu-item--expanded';
+        $element['is_expanded'] = TRUE;
       }
       elseif ($data->hasChildren) {
-        $class[] = 'menu-item--collapsed';
+        $element['is_collapsed'] = TRUE;
       }
-      // Set a class if the link is in the active trail.
+      // Set a helper variable to indicate whether link is in active trail.
       if ($data->inActiveTrail) {
-        $class[] = 'menu-item--active-trail';
+        $element['is_active_trail'] = TRUE;
       }
 
       // Note: links are rendered in the menu.html.twig template; and they
       // automatically bubble their associated cacheability metadata.
-      $element = array();
       $element['attributes'] = new Attribute();
-      $element['attributes']['class'] = $class;
       $element['title'] = $link->getTitle();
       $element['url'] = $link->getUrlObject();
       $element['url']->setOption('set_active_class', TRUE);
diff --git a/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php b/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
index da545b2..6385c69 100644
--- a/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
+++ b/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
@@ -141,13 +141,25 @@ public function providerTestBuildCacheability() {
     ];
 
     $get_built_element = function(MenuLinkTreeElement $element, array $classes) {
-      return [
-        'attributes' => new Attribute(['class' => array_merge(['menu-item'], $classes)]),
+      $return = [
+        'attributes' => new Attribute(),
         'title' => $element->link->getTitle(),
         'url' => new Url($element->link->getRouteName(), $element->link->getRouteParameters(), ['set_active_class' => TRUE]),
         'below' => [],
         'original_link' => $element->link,
       ];
+
+      if ($element->hasChildren && !empty($element->subtree)) {
+        $return['is_expanded'] = TRUE;
+      }
+      elseif ($element->hasChildren) {
+        $return['is_collapsed'] = TRUE;
+      }
+      if ($ekement->inActiveTrail) {
+        $return['is_active_trail'] = TRUE;
+      }
+
+      return $return;
     };
 
     // The three access scenarios described in this method's documentation.
diff --git a/core/modules/toolbar/templates/menu--toolbar.html.twig b/core/modules/toolbar/templates/menu--toolbar.html.twig
index 3578b95..090b7a4 100644
--- a/core/modules/toolbar/templates/menu--toolbar.html.twig
+++ b/core/modules/toolbar/templates/menu--toolbar.html.twig
@@ -32,7 +32,15 @@
       <ul class="toolbar-menu">
     {% endif %}
       {% for item in items %}
-        <li{{ item.attributes }}>
+      {%
+        set class = [
+          'menu-item',
+          item.is_expanded ? 'menu-item--expanded',
+          item.is_collapsed ? 'menu-item--collapsed',
+          item.is_active_trail ? 'menu-item--active-trail',
+        ]
+      %}
+        <li{{ item.attributes.addClass(class) }}>
           {{ link(item.title, item.url) }}
           {% if item.below %}
             {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
diff --git a/core/themes/classy/templates/navigation/menu.html.twig b/core/themes/classy/templates/navigation/menu.html.twig
index 4e12ea1..f28178c 100644
--- a/core/themes/classy/templates/navigation/menu.html.twig
+++ b/core/themes/classy/templates/navigation/menu.html.twig
@@ -29,8 +29,16 @@
     {% else %}
       <ul class="menu">
     {% endif %}
-      {% for item in items %}
-        <li{{ item.attributes }}>
+    {% for item in items %}
+      {%
+        set class = [
+          'menu-item',
+          item.is_expanded ? 'menu-item--expanded',
+          item.is_collapsed ? 'menu-item--collapsed',
+          item.is_active_trail ? 'menu-item--active-trail',
+        ]
+      %}
+        <li{{ item.attributes.addClass(class) }}>
           {{ link(item.title, item.url) }}
           {% if item.below %}
             {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
