DEV: Convert d-navigation-item to glimmer (#22155)

This commit is contained in:
Jarek Radosz 2023-06-16 16:36:50 +02:00 committed by GitHub
parent f8ea5b1136
commit 37030ed348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 74 additions and 88 deletions

View File

@ -1,3 +1,10 @@
<LinkTo @route={{this.route}}> <li
{{yield}} aria-current={{this.ariaCurrent}}
</LinkTo> title={{@title}}
class={{@class}}
...attributes
>
<LinkTo @route={{@route}}>
{{yield}}
</LinkTo>
</li>

View File

@ -1,41 +1,29 @@
import Component from "@ember/component"; import Component from "@glimmer/component";
import { computed } from "@ember/object";
import { inject as service } from "@ember/service"; import { inject as service } from "@ember/service";
export default Component.extend({ export default class DNavigationItem extends Component {
tagName: "li", @service router;
route: null, get ariaCurrent() {
// when there are multiple levels of navigation
router: service(), // we want the active parent to get `aria-current="page"`
// and the active child to get `aria-current="location"`
attributeBindings: ["ariaCurrent:aria-current", "title"], if (
this.args.ariaCurrentContext === "parentNav" &&
ariaCurrent: computed( this.router.currentRouteName !== this.args.route && // not the current route
"router.currentRouteName", this.router.currentRoute.parent.name.includes(this.args.route) // but is the current parent route
"router.currentRoute.parent.name", ) {
"route", return "page";
"ariaCurrentContext",
function () {
let ariaCurrentValue = "page";
// when there are multiple levels of navigation
// we want the active parent to get `aria-current="page"`
// and the active child to get `aria-current="location"`
if (this.ariaCurrentContext === "subNav") {
ariaCurrentValue = "location";
} else if (this.ariaCurrentContext === "parentNav") {
if (
this.router.currentRouteName !== this.route && // not the current route
this.router.currentRoute.parent.name.includes(this.route) // but is the current parent route
) {
return "page";
}
}
return this.router.currentRouteName === this.route
? ariaCurrentValue
: null;
} }
),
}); if (this.router.currentRouteName !== this.args.route) {
return null;
}
if (this.args.ariaCurrentContext === "subNav") {
return "location";
} else {
return "page";
}
}
}

View File

@ -4,15 +4,15 @@
@ariaLabel="User primary" @ariaLabel="User primary"
> >
{{#unless @user.profile_hidden}} {{#unless @user.profile_hidden}}
<DNavigationItem @route="user.summary" @class="user-nav__summary"> <DNavigationItem @route="user.summary" class="user-nav__summary">
{{d-icon "user"}} {{d-icon "user"}}
<span>{{i18n "user.summary.title"}}</span> <span>{{i18n "user.summary.title"}}</span>
</DNavigationItem> </DNavigationItem>
<DNavigationItem <DNavigationItem
@route="userActivity" @route="userActivity"
@class="user-nav__activity"
@ariaCurrentContext="parentNav" @ariaCurrentContext="parentNav"
class="user-nav__activity"
> >
{{d-icon "stream"}} {{d-icon "stream"}}
<span>{{i18n "user.activity_stream"}}</span> <span>{{i18n "user.activity_stream"}}</span>
@ -23,8 +23,8 @@
{{#if @showNotificationsTab}} {{#if @showNotificationsTab}}
<DNavigationItem <DNavigationItem
@route="userNotifications" @route="userNotifications"
@class="user-nav__notifications"
@ariaCurrentContext="parentNav" @ariaCurrentContext="parentNav"
class="user-nav__notifications"
> >
{{d-icon "bell" class="glyph"}} {{d-icon "bell" class="glyph"}}
<span>{{i18n "user.notifications"}}</span> <span>{{i18n "user.notifications"}}</span>
@ -34,8 +34,8 @@
{{#if @showPrivateMessages}} {{#if @showPrivateMessages}}
<DNavigationItem <DNavigationItem
@route="userPrivateMessages" @route="userPrivateMessages"
@class="user-nav__personal-messages"
@ariaCurrentContext="parentNav" @ariaCurrentContext="parentNav"
class="user-nav__personal-messages"
> >
{{d-icon "envelope"}} {{d-icon "envelope"}}
<span>{{i18n "user.private_messages"}}</span> <span>{{i18n "user.private_messages"}}</span>
@ -45,8 +45,8 @@
{{#if @canInviteToForum}} {{#if @canInviteToForum}}
<DNavigationItem <DNavigationItem
@route="userInvited" @route="userInvited"
@class="user-nav__invites"
@ariaCurrentContext="parentNav" @ariaCurrentContext="parentNav"
class="user-nav__invites"
> >
{{d-icon "user-plus"}} {{d-icon "user-plus"}}
<span>{{i18n "user.invited.title"}}</span> <span>{{i18n "user.invited.title"}}</span>
@ -54,7 +54,7 @@
{{/if}} {{/if}}
{{#if @showBadges}} {{#if @showBadges}}
<DNavigationItem @route="user.badges" @class="user-nav__badges"> <DNavigationItem @route="user.badges" class="user-nav__badges">
{{d-icon "certificate"}} {{d-icon "certificate"}}
<span>{{i18n "badges.title"}}</span> <span>{{i18n "badges.title"}}</span>
</DNavigationItem> </DNavigationItem>
@ -69,8 +69,8 @@
{{#if @user.can_edit}} {{#if @user.can_edit}}
<DNavigationItem <DNavigationItem
@route="preferences" @route="preferences"
@class="user-nav__preferences"
@ariaCurrentContext="parentNav" @ariaCurrentContext="parentNav"
class="user-nav__preferences"
> >
{{d-icon "cog"}} {{d-icon "cog"}}
<span>{{i18n "user.preferences"}}</span> <span>{{i18n "user.preferences"}}</span>

View File

@ -4,8 +4,8 @@
<HorizontalOverflowNav @ariaLabel="User secondary - preferences"> <HorizontalOverflowNav @ariaLabel="User secondary - preferences">
<DNavigationItem <DNavigationItem
@route="preferences.account" @route="preferences.account"
@class="user-nav__preferences-account"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-account"
> >
{{d-icon "user"}} {{d-icon "user"}}
<span>{{i18n "user.preferences_nav.account"}}</span> <span>{{i18n "user.preferences_nav.account"}}</span>
@ -13,8 +13,8 @@
<DNavigationItem <DNavigationItem
@route="preferences.security" @route="preferences.security"
@class="user-nav__preferences-security"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-security"
> >
{{d-icon "lock"}} {{d-icon "lock"}}
<span>{{i18n "user.preferences_nav.security"}}</span> <span>{{i18n "user.preferences_nav.security"}}</span>
@ -22,8 +22,8 @@
<DNavigationItem <DNavigationItem
@route="preferences.profile" @route="preferences.profile"
@class="user-nav__preferences-profile"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-profile"
> >
{{d-icon "user"}} {{d-icon "user"}}
<span>{{i18n "user.preferences_nav.profile"}}</span> <span>{{i18n "user.preferences_nav.profile"}}</span>
@ -31,8 +31,8 @@
<DNavigationItem <DNavigationItem
@route="preferences.emails" @route="preferences.emails"
@class="user-nav__preferences-emails"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-emails"
> >
{{d-icon "envelope"}} {{d-icon "envelope"}}
<span>{{i18n "user.preferences_nav.emails"}}</span> <span>{{i18n "user.preferences_nav.emails"}}</span>
@ -40,8 +40,8 @@
<DNavigationItem <DNavigationItem
@route="preferences.notifications" @route="preferences.notifications"
@class="user-nav__preferences-notifications"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-notifications"
> >
{{d-icon "bell"}} {{d-icon "bell"}}
<span>{{i18n "user.preferences_nav.notifications"}}</span> <span>{{i18n "user.preferences_nav.notifications"}}</span>
@ -50,8 +50,8 @@
{{#if this.model.can_change_tracking_preferences}} {{#if this.model.can_change_tracking_preferences}}
<DNavigationItem <DNavigationItem
@route="preferences.tracking" @route="preferences.tracking"
@class="user-nav__preferences-tracking"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-tracking"
> >
{{d-icon "plus"}} {{d-icon "plus"}}
<span>{{i18n "user.preferences_nav.tracking"}}</span> <span>{{i18n "user.preferences_nav.tracking"}}</span>
@ -60,8 +60,8 @@
<DNavigationItem <DNavigationItem
@route="preferences.users" @route="preferences.users"
@class="user-nav__preferences-users"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-users"
> >
{{d-icon "users"}} {{d-icon "users"}}
<span>{{i18n "user.preferences_nav.users"}}</span> <span>{{i18n "user.preferences_nav.users"}}</span>
@ -69,8 +69,8 @@
<DNavigationItem <DNavigationItem
@route="preferences.interface" @route="preferences.interface"
@class="user-nav__preferences-interface"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-interface"
> >
{{d-icon "desktop"}} {{d-icon "desktop"}}
<span>{{i18n "user.preferences_nav.interface"}}</span> <span>{{i18n "user.preferences_nav.interface"}}</span>
@ -79,8 +79,8 @@
{{#if (not (eq this.siteSettings.navigation_menu "legacy"))}} {{#if (not (eq this.siteSettings.navigation_menu "legacy"))}}
<DNavigationItem <DNavigationItem
@route="preferences.navigation-menu" @route="preferences.navigation-menu"
@class="user-nav__preferences-navigation-menu"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__preferences-navigation-menu"
> >
{{d-icon "bars"}} {{d-icon "bars"}}
<span>{{i18n "user.preferences_nav.navigation_menu"}}</span> <span>{{i18n "user.preferences_nav.navigation_menu"}}</span>

View File

@ -2,9 +2,8 @@
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.group.index" @route="userPrivateMessages.group.index"
@class="user-nav__messages-group-latest"
@model={{this.group.name}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-group-latest"
> >
{{d-icon "envelope"}} {{d-icon "envelope"}}
<span>{{i18n "categories.latest"}}</span> <span>{{i18n "categories.latest"}}</span>
@ -13,9 +12,8 @@
{{#if this.viewingSelf}} {{#if this.viewingSelf}}
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.group.new" @route="userPrivateMessages.group.new"
@model={{this.group.name}}
@class="user-nav__messages-group-new"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-group-new"
> >
{{d-icon "exclamation-circle"}} {{d-icon "exclamation-circle"}}
<span>{{this.newLinkText}}</span> <span>{{this.newLinkText}}</span>
@ -23,9 +21,8 @@
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.group.unread" @route="userPrivateMessages.group.unread"
@model={{this.group.name}}
@class="user-nav__messages-group-unread"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-group-unread"
> >
{{d-icon "plus-circle"}} {{d-icon "plus-circle"}}
<span>{{this.unreadLinkText}}</span> <span>{{this.unreadLinkText}}</span>
@ -33,9 +30,8 @@
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.group.archive" @route="userPrivateMessages.group.archive"
@class="user-nav__messages-group-archive"
@model={{this.group.name}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-group-archive"
> >
{{d-icon "archive"}} {{d-icon "archive"}}
<span>{{i18n "user.messages.archive"}}</span> <span>{{i18n "user.messages.archive"}}</span>

View File

@ -7,9 +7,8 @@
<UserNav::MessagesSecondaryNav> <UserNav::MessagesSecondaryNav>
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.user.index" @route="userPrivateMessages.user.index"
@class="user-nav__messages-latest"
@model={{this.model}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-latest"
> >
{{d-icon "envelope"}} {{d-icon "envelope"}}
<span>{{i18n "categories.latest"}}</span> <span>{{i18n "categories.latest"}}</span>
@ -17,9 +16,8 @@
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.user.sent" @route="userPrivateMessages.user.sent"
@class="user-nav__messages-sent"
@model={{this.model}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-sent"
> >
{{d-icon "reply"}} {{d-icon "reply"}}
<span>{{i18n "user.messages.sent"}}</span> <span>{{i18n "user.messages.sent"}}</span>
@ -28,9 +26,8 @@
{{#if this.viewingSelf}} {{#if this.viewingSelf}}
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.user.new" @route="userPrivateMessages.user.new"
@class="user-nav__messages-new"
@model={{this.model}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-new"
> >
{{d-icon "exclamation-circle"}} {{d-icon "exclamation-circle"}}
<span>{{this.newLinkText}}</span> <span>{{this.newLinkText}}</span>
@ -38,9 +35,8 @@
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.user.unread" @route="userPrivateMessages.user.unread"
@class="user-nav__messages-unread"
@model={{this.model}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-unread"
> >
{{d-icon "plus-circle"}} {{d-icon "plus-circle"}}
<span>{{this.unreadLinkText}}</span> <span>{{this.unreadLinkText}}</span>
@ -50,9 +46,8 @@
<DNavigationItem <DNavigationItem
@route="userPrivateMessages.user.archive" @route="userPrivateMessages.user.archive"
@class="user-nav__messages-archive"
@model={{this.model}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__messages-archive"
> >
{{d-icon "archive"}} {{d-icon "archive"}}
<span>{{i18n "user.messages.archive"}}</span> <span>{{i18n "user.messages.archive"}}</span>

View File

@ -4,8 +4,8 @@
<HorizontalOverflowNav @ariaLabel="User secondary - activity"> <HorizontalOverflowNav @ariaLabel="User secondary - activity">
<DNavigationItem <DNavigationItem
@route="userActivity.index" @route="userActivity.index"
@class="user-nav__activity-all"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-all"
> >
{{d-icon "stream"}} {{d-icon "stream"}}
<span>{{i18n "user.filters.all"}}</span> <span>{{i18n "user.filters.all"}}</span>
@ -13,16 +13,16 @@
<DNavigationItem <DNavigationItem
@route="userActivity.topics" @route="userActivity.topics"
@class="user-nav__activity-topics"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-topics"
> >
{{d-icon "list-ul"}} {{d-icon "list-ul"}}
<span>{{i18n "user_action_groups.4"}}</span> <span>{{i18n "user_action_groups.4"}}</span>
</DNavigationItem> </DNavigationItem>
<DNavigationItem <DNavigationItem
@route="userActivity.replies" @route="userActivity.replies"
@class="user-nav__activity-replies"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-replies"
> >
{{d-icon "reply"}} {{d-icon "reply"}}
<span>{{i18n "user_action_groups.5"}}</span> <span>{{i18n "user_action_groups.5"}}</span>
@ -31,9 +31,9 @@
{{#if this.user.showRead}} {{#if this.user.showRead}}
<DNavigationItem <DNavigationItem
@route="userActivity.read" @route="userActivity.read"
@class="user-nav__activity-read"
@title={{i18n "user.read_help"}}
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-read"
title={{i18n "user.read_help"}}
> >
{{d-icon "history"}} {{d-icon "history"}}
<span>{{i18n "user.read"}}</span> <span>{{i18n "user.read"}}</span>
@ -43,8 +43,8 @@
{{#if this.user.showDrafts}} {{#if this.user.showDrafts}}
<DNavigationItem <DNavigationItem
@route="userActivity.drafts" @route="userActivity.drafts"
@class="user-nav__activity-drafts"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-drafts"
> >
{{d-icon "pencil-alt"}} {{d-icon "pencil-alt"}}
<span>{{this.draftLabel}}</span> <span>{{this.draftLabel}}</span>
@ -54,8 +54,8 @@
{{#if (gt this.model.pending_posts_count 0)}} {{#if (gt this.model.pending_posts_count 0)}}
<DNavigationItem <DNavigationItem
@route="userActivity.pending" @route="userActivity.pending"
@class="user-nav__activity-pending"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-pending"
> >
{{d-icon "clock"}} {{d-icon "clock"}}
<span>{{this.pendingLabel}}</span> <span>{{this.pendingLabel}}</span>
@ -64,8 +64,8 @@
<DNavigationItem <DNavigationItem
@route="userActivity.likesGiven" @route="userActivity.likesGiven"
@class="user-nav__activity-likes"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-likes"
> >
{{d-icon "heart"}} {{d-icon "heart"}}
<span>{{i18n "user_action_groups.1"}}</span> <span>{{i18n "user_action_groups.1"}}</span>
@ -74,8 +74,8 @@
{{#if this.user.showBookmarks}} {{#if this.user.showBookmarks}}
<DNavigationItem <DNavigationItem
@route="userActivity.bookmarks" @route="userActivity.bookmarks"
@class="user-nav__activity-bookmarks"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__activity-bookmarks"
> >
{{d-icon "bookmark"}} {{d-icon "bookmark"}}
<span>{{i18n "user_action_groups.3"}}</span> <span>{{i18n "user_action_groups.3"}}</span>

View File

@ -4,8 +4,8 @@
<HorizontalOverflowNav @ariaLabel="User secondary - notifications"> <HorizontalOverflowNav @ariaLabel="User secondary - notifications">
<DNavigationItem <DNavigationItem
@route="userNotifications.index" @route="userNotifications.index"
@class="user-nav__notifications-all"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__notifications-all"
> >
{{d-icon "bell"}} {{d-icon "bell"}}
<span>{{i18n "user.filters.all"}}</span> <span>{{i18n "user.filters.all"}}</span>
@ -13,8 +13,8 @@
<DNavigationItem <DNavigationItem
@route="userNotifications.responses" @route="userNotifications.responses"
@class="user-nav__notifications-responses"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__notifications-responses"
> >
{{d-icon "reply"}} {{d-icon "reply"}}
<span>{{i18n "user_action_groups.6"}}</span> <span>{{i18n "user_action_groups.6"}}</span>
@ -22,8 +22,8 @@
<DNavigationItem <DNavigationItem
@route="userNotifications.likesReceived" @route="userNotifications.likesReceived"
@class="user-nav__notifications-likes"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__notifications-likes"
> >
{{d-icon "heart"}} {{d-icon "heart"}}
<span>{{i18n "user_action_groups.2"}}</span> <span>{{i18n "user_action_groups.2"}}</span>
@ -32,8 +32,8 @@
{{#if this.siteSettings.enable_mentions}} {{#if this.siteSettings.enable_mentions}}
<DNavigationItem <DNavigationItem
@route="userNotifications.mentions" @route="userNotifications.mentions"
@class="user-nav__notifications-mentions"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__notifications-mentions"
> >
{{d-icon "at"}} {{d-icon "at"}}
<span>{{i18n "user_action_groups.7"}}</span> <span>{{i18n "user_action_groups.7"}}</span>
@ -42,8 +42,8 @@
<DNavigationItem <DNavigationItem
@route="userNotifications.edits" @route="userNotifications.edits"
@class="user-nav__notifications-edits"
@ariaCurrentContext="subNav" @ariaCurrentContext="subNav"
class="user-nav__notifications-edits"
> >
{{d-icon "pencil-alt"}} {{d-icon "pencil-alt"}}
<span>{{i18n "user_action_groups.11"}}</span> <span>{{i18n "user_action_groups.11"}}</span>