diff --git a/core/lib/Drupal/Core/Menu/MenuLinkTree.php b/core/lib/Drupal/Core/Menu/MenuLinkTree.php
index 090293c..a5ea3d2 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['in_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/book/src/BookManager.php b/core/modules/book/src/BookManager.php
index bd0d419..556350f 100644
--- a/core/modules/book/src/BookManager.php
+++ b/core/modules/book/src/BookManager.php
@@ -539,30 +539,29 @@ protected function buildItems(array $tree) {
     $items = [];
 
     foreach ($tree as $data) {
-      $class = ['menu-item'];
+      $element = [];
+
       // Generally we only deal with visible links, but just in case.
       if (!$data['link']['access']) {
         continue;
       }
       // Set a class for the <li>-tag. Since $data['below'] may contain local
-      // tasks, only set 'expanded' class if the link also has children within
+      // tasks, only set 'expanded' true if the link also has children within
       // the current book.
       if ($data['link']['has_children'] && $data['below']) {
-        $class[] = 'menu-item--expanded';
+        $element['is_expanded'] = TRUE;
       }
       elseif ($data['link']['has_children']) {
-        $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['link']['in_active_trail']) {
-        $class[] = 'menu-item--active-trail';
+        $element['in_active_trail'] = TRUE;
       }
 
       // Allow book-specific theme overrides.
-      $element = [];
       $element['attributes'] = new Attribute();
-      $element['attributes']['class'] = $class;
       $element['title'] = $data['link']['title'];
       $node = $this->entityManager->getStorage('node')->load($data['link']['nid']);
       $element['url'] = $node->urlInfo();
diff --git a/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php b/core/modules/system/tests/src/Unit/Menu/MenuLinkTreeTest.php
index da545b2..64d14b9 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 ($element->inActiveTrail) {
+        $return['in_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..e8e6b45 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 classes = [
+            'menu-item',
+            item.is_expanded ? 'menu-item--expanded',
+            item.is_collapsed ? 'menu-item--collapsed',
+            item.in_active_trail ? 'menu-item--active-trail',
+          ]
+        %}
+        <li{{ item.attributes.addClass(classes) }}>
           {{ 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/book-tree.html.twig b/core/themes/classy/templates/navigation/book-tree.html.twig
index 543696c..f6c7f02 100644
--- a/core/themes/classy/templates/navigation/book-tree.html.twig
+++ b/core/themes/classy/templates/navigation/book-tree.html.twig
@@ -30,7 +30,15 @@
       <ul class="menu">
     {% endif %}
       {% for item in items %}
-        <li{{ item.attributes }}>
+        {%
+          set classes = [
+            'menu-item',
+            item.is_expanded ? 'menu-item--expanded',
+            item.is_collapsed ? 'menu-item--collapsed',
+            item.in_active_trail ? 'menu-item--active-trail',
+          ]
+        %}
+        <li{{ item.attributes.addClass(classes) }}>
           {{ link(item.title, item.url) }}
           {% if item.below %}
             {{ book_tree.book_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..aa6b469 100644
--- a/core/themes/classy/templates/navigation/menu.html.twig
+++ b/core/themes/classy/templates/navigation/menu.html.twig
@@ -29,14 +29,22 @@
     {% else %}
       <ul class="menu">
     {% endif %}
-      {% for item in items %}
-        <li{{ item.attributes }}>
-          {{ link(item.title, item.url) }}
-          {% if item.below %}
-            {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
-          {% endif %}
-        </li>
-      {% endfor %}
+    {% for item in items %}
+      {%
+        set classes = [
+          'menu-item',
+          item.is_expanded ? 'menu-item--expanded',
+          item.is_collapsed ? 'menu-item--collapsed',
+          item.in_active_trail ? 'menu-item--active-trail',
+        ]
+      %}
+      <li{{ item.attributes.addClass(classes) }}>
+        {{ link(item.title, item.url) }}
+        {% if item.below %}
+          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
+        {% endif %}
+      </li>
+    {% endfor %}
     </ul>
   {% endif %}
 {% endmacro %}
