Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds current menu class to navigation block #20076

Merged
merged 4 commits into from
Mar 23, 2020

Conversation

draganescu
Copy link
Contributor

@draganescu draganescu commented Feb 6, 2020

Description

Closes #19993
Closes #19858

This PR adds two new updates to the server side rendering of the Navigation block:

  • it renders on each navigation link the custom CSS class saved from the block editor
  • it renders on navigation links a class named 'current-menu-item'

How has this been tested?

  1. In a post add a new navigation block
  2. Add three links to the post you are editing and two other posts and/or pages
    2.1 Add a custom CSS class to one of the links
  3. Save the block as a reusable block
  4. Edit the pages/posts that you linked to and add the reusable block
  5. Visit the pages and notice the class being added to the current menu item and the custom CSS class being rendered

Types of changes

Updated the server side rendering of the Navigation block to output the custom CSS class of each NavigationLink and to check for the current post ID.

@draganescu
Copy link
Contributor Author

Thinking about this PR, if we implement this block in a future menus.php admin page, the Navigation block's rendering should handle all the classes printed by wp_nav_menu so things don't break, so this 'current-menu-item' does not seem sufficient.


$html .= '<li class="' . esc_attr( $css_classes . ( $has_submenu ? ' has-child' : '' ) ) . '"' . $style_attribute . '>' .
$html .= '<li class="' . esc_attr( $css_classes . ( $has_submenu ? ' has-child' : '' ) ) .
( $is_active ? ' current-menu-item' : '' ) . '"' . $style_attribute . '>' .
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow the pattern set by other class names, shouldn't it be something like is-current-navigation-link?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the is- prefix fits pretty good here, in the same way that is-selected, etc... does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but all WP menus use this convention of current-menu-item, check wp_nav_menu. There is a whole section of default CSS classes and this is-* convention isn't there.

<< I do agree >> with the is-* convention it is just unclear when we start breaking backwards compat.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes made to the Navigation block won't have any effect on existing themes (which don't use blocks at all), so I see no problem with using an is- class here. But maybe someone else has another opinion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding both of them is too much? So, we could guarantee the compatibility with the wp_nav_menu(), and at the same time maybe adding a super basic style for .is-current-navigation-link?

.is-current-navigation-link {
    border: 1px solid red; // ;-)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it could be handled from the sidebar too, in a similar way that submenus icon does

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@retrofox That sounds like overkill to me. It should be entirely up to the theme to determine if the current navigation link recieves special styles or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Just bringing here the idea that I could be handled in both ways. Through the theme and/or a Navigation option.

@retrofox
Copy link
Contributor

retrofox commented Feb 6, 2020

Could we add a very simple default style? could it be too intrusive?

@draganescu draganescu added the Needs Design Feedback Needs general design feedback. label Feb 7, 2020
@ellatrix ellatrix removed this from the Gutenberg 7.5 milestone Feb 10, 2020
@draganescu draganescu added [Block] Navigation Affects the Navigation Block [Type] Enhancement A suggestion for improvement. Good First Review A PR that's suitable for someone looking to contribute for the first time by reviewing code labels Feb 18, 2020
Copy link
Contributor

@tellthemachines tellthemachines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small bug to be addressed, but looks good apart from that 😄

packages/block-library/src/navigation/index.php Outdated Show resolved Hide resolved
@draganescu draganescu force-pushed the add/navigation-show-current-page branch from 081c57a to 37e20eb Compare March 18, 2020 15:20
@github-actions
Copy link

github-actions bot commented Mar 18, 2020

Size Change: -863 B (0%)

Total Size: 856 kB

Filename Size Change
build/block-editor/index.js 100 kB +144 B (0%)
build/block-editor/style-rtl.css 10.9 kB +30 B (0%)
build/block-editor/style.css 10.9 kB +34 B (0%)
build/block-library/editor-rtl.css 7.24 kB +3 B (0%)
build/block-library/editor.css 7.24 kB +4 B (0%)
build/block-library/index.js 110 kB -1.14 kB (1%)
build/block-library/style-rtl.css 7.41 kB -6 B (0%)
build/block-library/style.css 7.42 kB -6 B (0%)
build/blocks/index.js 57.5 kB -23 B (0%)
build/components/index.js 191 kB -3 B (0%)
build/components/style-rtl.css 15.8 kB +9 B (0%)
build/components/style.css 15.7 kB +9 B (0%)
build/edit-post/style-rtl.css 8.47 kB +4 B (0%)
build/edit-post/style.css 8.46 kB +3 B (0%)
build/edit-widgets/index.js 4.43 kB -1 B
build/editor/index.js 43.8 kB -3 B (0%)
build/editor/style-rtl.css 3.97 kB +2 B (0%)
build/editor/style.css 3.96 kB +3 B (0%)
build/media-utils/index.js 4.84 kB +6 B (0%)
build/primitives/index.js 1.5 kB +2 B (0%)
build/rich-text/index.js 14.4 kB +61 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 998 B 0 B
build/annotations/index.js 3.43 kB 0 B
build/api-fetch/index.js 3.39 kB 0 B
build/autop/index.js 2.58 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.02 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 760 B 0 B
build/block-library/theme-rtl.css 669 B 0 B
build/block-library/theme.css 671 B 0 B
build/block-serialization-default-parser/index.js 1.65 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/compose/index.js 6.21 kB 0 B
build/core-data/index.js 10.6 kB 0 B
build/data-controls/index.js 1.04 kB 0 B
build/data/index.js 8.2 kB 0 B
build/date/index.js 5.37 kB 0 B
build/deprecated/index.js 771 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.06 kB 0 B
build/edit-post/index.js 91.2 kB 0 B
build/edit-site/index.js 5.56 kB 0 B
build/edit-site/style-rtl.css 2.62 kB 0 B
build/edit-site/style.css 2.62 kB 0 B
build/edit-widgets/style-rtl.css 2.58 kB 0 B
build/edit-widgets/style.css 2.58 kB 0 B
build/editor/editor-styles-rtl.css 381 B 0 B
build/editor/editor-styles.css 382 B 0 B
build/element/index.js 4.44 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 6.95 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 1.93 kB 0 B
build/html-entities/index.js 621 B 0 B
build/i18n/index.js 3.49 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.3 kB 0 B
build/keycodes/index.js 1.69 kB 0 B
build/list-reusable-blocks/index.js 2.99 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/notices/index.js 1.58 kB 0 B
build/nux/index.js 3.01 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.54 kB 0 B
build/priority-queue/index.js 780 B 0 B
build/redux-routine/index.js 2.83 kB 0 B
build/server-side-render/index.js 2.55 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/url/index.js 4.01 kB 0 B
build/viewport/index.js 1.61 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

Copy link
Contributor

@tellthemachines tellthemachines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@draganescu draganescu merged commit 58ab622 into master Mar 23, 2020
@draganescu draganescu deleted the add/navigation-show-current-page branch March 23, 2020 06:57
@vindl
Copy link
Member

vindl commented Mar 23, 2020

This seems to be breaking the experimental Site Editor for me. I'm still not sure if it's related to the template content that I had before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block Good First Review A PR that's suitable for someone looking to contribute for the first time by reviewing code Needs Design Feedback Needs general design feedback. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Navigation Block should support current-menu-item Additional CSS Class(es) not working on Navigation Block
6 participants