Skip to content

Commit

Permalink
refactor(cxl-ui): cxl-marketing-nav get mobile groups once
Browse files Browse the repository at this point in the history
  • Loading branch information
anoblet authored and pawelkmpt committed Jan 12, 2024
1 parent 3b7c176 commit c44fa6e
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions packages/cxl-ui/src/components/cxl-marketing-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,7 @@ export class CXLMarketingNavElement extends LitElement {
* menu items last in the list by sorting the original this.groups array before
* flat map operation.
*/
@property({ type: Array })
get mobileGroups() {
// eslint-disable-next-line no-nested-ternary
const sorter = (a, b) => ('global' === a.name ? 1 : 'global' === b.name ? -1 : 0);
const groups = [...this.groups];

return [
{
name: 'primary',
items: groups
.sort(sorter)
.map((group) => group.items.filter((item) => !item.isSearch))
.flat(1),
},
];
}
@property({ type: Array }) mobileGroups = [];

get menuItemSearchElement() {
return this.querySelector('.search-button');
Expand All @@ -129,7 +114,6 @@ export class CXLMarketingNavElement extends LitElement {
});

requestAnimationFrame(() => {
this._updateContextMenuItems();
this._replaceMenuIcon();
this.initHeadroom();
});
Expand All @@ -140,6 +124,10 @@ export class CXLMarketingNavElement extends LitElement {
this._updateContextMenuItems();
this._replaceMenuIcon();
}

if (changes.has('groups')) {
this.mobileGroups = this.getMobileGroups();
}
}

render() {
Expand Down Expand Up @@ -442,4 +430,20 @@ export class CXLMarketingNavElement extends LitElement {

headroom.init();
}

getMobileGroups() {
// eslint-disable-next-line no-nested-ternary
const sorter = (a, b) => ('global' === a.name ? 1 : 'global' === b.name ? -1 : 0);
const groups = [...this.groups];

return [
{
name: 'primary',
items: groups
.sort(sorter)
.map((group) => group.items.filter((item) => !item.isSearch))
.flat(1),
},
];
}
}

0 comments on commit c44fa6e

Please sign in to comment.