@illinois-toolkit/ilw-header-megamenu 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,285 @@
1
+ import { LitElement, html, unsafeCSS } from "lit";
2
+ // @ts-ignore
3
+ import styles from './MegaMenuSection.styles.css?inline';
4
+ import './MegaMenuSection.css';
5
+ import { customElement, property } from "lit/decorators.js";
6
+
7
+ @customElement("ilw-header-megamenu-section")
8
+ export default class MegaMenuSection extends LitElement {
9
+
10
+ @property({
11
+ type: Boolean
12
+ })
13
+ current = false;
14
+
15
+ @property({
16
+ type: Boolean
17
+ })
18
+ compact = false;
19
+
20
+ @property({
21
+ type: Boolean
22
+ })
23
+ right = false;
24
+
25
+ @property({
26
+ type: Boolean
27
+ })
28
+ expanded = false;
29
+
30
+ @property({
31
+ type: Boolean
32
+ })
33
+ mouseover = false;
34
+
35
+ @property({
36
+ reflect: true,
37
+ type: Boolean
38
+ })
39
+ linked = false;
40
+
41
+ static get styles() {
42
+ return unsafeCSS(styles);
43
+ }
44
+
45
+ constructor() {
46
+ super();
47
+ this.addEventListener('keydown', this.handleWindowKeydown.bind(this));
48
+ }
49
+
50
+ connectedCallback() {
51
+ super.connectedCallback();
52
+ }
53
+
54
+ handleToggleClick(evt: Event) {
55
+ this.expanded = !this.expanded;
56
+ this.dispatchEvent(new CustomEvent('ilw-header-megamenu-section-expanded', {detail: !this.expanded, bubbles: true, composed: true}));
57
+ }
58
+
59
+ handleWindowKeydown(evt: KeyboardEvent) {
60
+ if (evt.key === 'ArrowDown') {
61
+ evt.stopPropagation();
62
+ evt.preventDefault();
63
+ this.moveToNextItem();
64
+ this.closeAllExceptOneSections(evt.target);
65
+ }
66
+ else if (evt.key === 'ArrowRight' || evt.key === 'ArrowLeft') {
67
+ if (this.isOnAnchorInLinked() && evt.key === 'ArrowRight') {
68
+ evt.stopPropagation();
69
+ evt.preventDefault();
70
+ this.expanded = false;
71
+ this.setFocus(true);
72
+ } else if (this.isOnButtonInLinked() && evt.key === 'ArrowLeft') {
73
+ evt.stopPropagation();
74
+ evt.preventDefault();
75
+ this.expanded = false;
76
+ this.setFocus();
77
+ } else if (this.isOnButtonInLinked() && evt.key === 'ArrowRight') {
78
+ this.expanded = false;
79
+ } else if (this.isOnAnchorInLinked() && evt.key === 'ArrowLeft') {
80
+ this.expanded = false;
81
+ } else {
82
+ this.expanded = false;
83
+ this.setFocus();
84
+ }
85
+ }
86
+ else if (evt.key === 'ArrowUp') {
87
+ evt.stopPropagation();
88
+ evt.preventDefault();
89
+ this.moveToPreviousItem();
90
+ this.closeAllExceptOneSections(evt.target);
91
+ }
92
+ else if (evt.key === 'Escape') {
93
+ if (this.expanded) {
94
+ evt.stopPropagation();
95
+ this.setFocus();
96
+ this.expanded = false;
97
+ }
98
+ }
99
+ }
100
+
101
+ isEmbedded() {
102
+ return this.parentElement && this.parentElement.closest('ilw-header-megamenu-section') != null;
103
+ }
104
+
105
+ isOnAnchorInLinked() {
106
+ return this.linked && document.activeElement && document.activeElement.tagName === 'A';
107
+ }
108
+
109
+ isOnButtonInLinked() {
110
+ return this.linked && document.activeElement && document.activeElement.tagName === 'ILW-HEADER-MEGAMENU-SECTION';
111
+ }
112
+
113
+ setFocus(useButton: Boolean = false) {
114
+ let newNode: Element | null = null;
115
+ if (this.linked && !useButton) {
116
+ newNode = this.querySelector('a');
117
+ }
118
+ else if (this.shadowRoot) {
119
+ newNode = this.shadowRoot.querySelector('a');
120
+ if (newNode == null) {
121
+ newNode = this.shadowRoot.querySelector('button');
122
+ }
123
+ }
124
+ if (newNode != null) {
125
+ (newNode as HTMLElement).focus();
126
+ }
127
+ }
128
+
129
+
130
+ moveToNextItem() {
131
+ if (!this.expanded && !this.isEmbedded()) {
132
+ this.expanded = true;
133
+ }
134
+
135
+ const activeElement = document.activeElement as HTMLElement | null;
136
+ if (!activeElement) return;
137
+
138
+ const section = activeElement.closest('ILW-HEADER-MEGAMENU-SECTION');
139
+ if (!section) return;
140
+
141
+ const links = Array.from(section.querySelectorAll('a, button')) as HTMLElement[];
142
+ const currentIndex = links.indexOf(activeElement);
143
+
144
+ let nextIndex = currentIndex + 1;
145
+ if (currentIndex === -1) {
146
+ nextIndex = 0;
147
+ }
148
+
149
+ if (nextIndex >= 0 && nextIndex < links.length) {
150
+ const nextLink = links[nextIndex];
151
+ nextLink.focus();
152
+ }
153
+ }
154
+
155
+ moveToPreviousItem() {
156
+ const activeElement = document.activeElement as HTMLElement | null;
157
+ if (!activeElement) return;
158
+
159
+ const section = activeElement.closest('ILW-HEADER-MEGAMENU-SECTION');
160
+ if (!section) return;
161
+
162
+ const links = Array.from(section.querySelectorAll('a, button')) as HTMLElement[];
163
+ const currentIndex = links.indexOf(activeElement);
164
+
165
+ let prevIndex = currentIndex - 1;
166
+ if (currentIndex === -1) {
167
+ prevIndex = links.length - 1;
168
+ }
169
+
170
+ if (prevIndex >= 0 && prevIndex < links.length) {
171
+ const prevLink = links[prevIndex];
172
+ prevLink.focus();
173
+ }
174
+ }
175
+
176
+ renderArrow() {
177
+ return html`<svg class="arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40.4 23.82" aria-hidden="true">
178
+ <path id="chevron" d="m39.34,1.06c-1.41-1.41-3.7-1.41-5.12,0l-14.02,14.02L6.18,1.06C4.76-.35,2.47-.35,1.06,1.06s-1.41,3.7,0,5.12l16.58,16.58c1.41,1.41,3.7,1.41,5.12,0L39.34,6.18c1.41-1.41,1.41-3.7,0-5.12Z"/>
179
+ </svg>`;
180
+ }
181
+
182
+ toggleMouseOver() {
183
+ this.mouseover = !this.mouseover;
184
+ }
185
+
186
+ handleNavigationSectionToggleClick(evt: CustomEvent) {
187
+ evt.stopPropagation();
188
+ this.closeAllExceptOneSections(evt.target);
189
+ }
190
+
191
+ getSections() {
192
+ return this.querySelectorAll('ilw-header-megamenu-section');
193
+ }
194
+
195
+ closeAllExceptOneSections(target: EventTarget | null) {
196
+ this.getSections().forEach(s => { if (target == null || target != s) { s.expanded = false; } });
197
+ }
198
+
199
+
200
+ render() {
201
+ const actionSpan = this.querySelector('span[slot="action"]');
202
+ let actionId: string | null = null;
203
+ if (actionSpan) {
204
+ const pText = actionSpan.querySelector('p')?.textContent?.trim() || '';
205
+ let link = actionSpan.querySelector('a, button');
206
+ if (pText) {
207
+ actionId = `action-${pText.replace(/\s+/g, '-')}`; // normalize spaces
208
+ actionSpan.id = actionId;
209
+ }
210
+ if (link && actionId) {
211
+ link.setAttribute('aria-labelledby', actionId);
212
+ }
213
+ }
214
+
215
+ const sections = document.querySelectorAll('ilw-header-megamenu-section');
216
+
217
+ sections.forEach((section) => {
218
+
219
+ const lists = section.querySelectorAll<HTMLLIElement>('ilw-header-megamenu-section > ul > li');
220
+ const listitems = lists.length;
221
+
222
+ if (listitems === 5) {
223
+ lists.forEach((list, index) => {
224
+ list.style.borderRight = 'none';
225
+ });
226
+ return;
227
+ }
228
+
229
+ let columnend = Math.floor(listitems / 5) * 5;
230
+
231
+ columnend = Math.min(columnend, 15);
232
+
233
+ lists.forEach((list, index) => {
234
+ const position = index + 1;
235
+
236
+ if (position <= columnend) {
237
+ list.style.borderRight = 'solid 2px var(--il-storm-85, #d5d3d3)';
238
+ } else {
239
+ list.style.borderRight = 'none';
240
+ }
241
+ });
242
+ });
243
+
244
+
245
+
246
+
247
+
248
+
249
+ let isSubMenu = this.parentElement != null && this.parentElement.closest("ilw-header-megamenu-section") != null;
250
+ this.current = this.current || (this.getAttribute('aria-current') != null && (this.getAttribute('aria-current') === 'page' || this.getAttribute('aria-current') === 'true'));
251
+ var withLink = html`
252
+ <div class="header-link ${this.mouseover ? "highlighted" : ""} ${this.compact ? "compact" : ""} ${this.current ? "current" : ""}" @mouseover="${this.toggleMouseOver.bind(this)}" @mouseout="${this.toggleMouseOver.bind(this)}">
253
+ <slot name="link"></slot>
254
+ <button class="arrow-only" @click=${this.handleToggleClick.bind(this)} aria-expanded=${this.expanded ? 'true' : 'false'} aria-label=${this.querySelector('a[slot="link"]')?.textContent + ' submenu'} aria-controls="items">
255
+ ${this.renderArrow()}
256
+ </button>
257
+ </div>
258
+ `;
259
+
260
+
261
+ var withoutLink = html`
262
+ <button class="${this.current ? "current" : ""}" @click=${this.handleToggleClick.bind(this)} aria-expanded=${this.expanded ? 'true' : 'false'} aria-controls="items">
263
+ <div class="header">
264
+ <div class="label"><slot name="label"></slot> </div>
265
+ <div class="icon">${this.renderArrow()}</div>
266
+ </div>
267
+ </button>
268
+ `;
269
+
270
+ return html`
271
+ <div class="${isSubMenu ? 'submenu' : 'menu'} parent" @ilw-header-megamenu-section-expanded=${this.handleNavigationSectionToggleClick}>
272
+ ${this.linked ? withLink : withoutLink}
273
+ <div id="items" class="${this.expanded ? 'expanded' : ''} ${this.right ? 'right' : ''}">
274
+ <slot></slot>
275
+ </div>
276
+ </div>
277
+ `;
278
+ }
279
+ }
280
+
281
+ declare global {
282
+ interface HTMLElementTagNameMap {
283
+ "ilw-header-megamenu-section": MegaMenuSection;
284
+ }
285
+ }
@@ -0,0 +1,2 @@
1
+ import "./MegaMenu";
2
+ import "./MegaMenuSection";