-
Notifications
You must be signed in to change notification settings - Fork 3
feature.LinkTray #344
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
base: dev
Are you sure you want to change the base?
feature.LinkTray #344
Conversation
| const li = document.createElement('li'); | ||
| li.setAttribute('slot', 'tray-link'); | ||
|
|
||
| const link = element.outerHTML; | ||
|
|
||
| li.textContent = link; | ||
|
|
||
| const chevron = document.createElement('span'); | ||
| chevron.innerHTML = ` | ||
| <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-right" viewBox="0 0 16 16"> | ||
| <path fill-rule="evenodd" d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708"/> | ||
| </svg> `; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed in the inspector that none of the slotted elements are wrapped in li tags, even though that's what is supposed to happen here.
The reason is that we're creating the li element at line 62 here, but we're not inserting it into the component shadowDOM at any point.
To insert the li into the shadowDOM, we'd have to do something like this:
element.parentNode.insertBefore(li, element);That would basically tell the browser: find the parent (which happens to be the component root in this case cod-linktray) of the slotted element (the a element), and insert the li right before it. I pasted the rendered HTML for reference below.
<cod-linktray class="linktray">
<span slot="tray-title"> Mayor's Office </span>
<a slot="tray-link" href="https://www.example.com">Mayor's Office
</a><a slot="tray-link" href="https://www.example.com">DPD
</a><a slot="tray-link" href="https://www.example.com">City Clerk
</a><a slot="tray-link" href="https://www.example.com">HRD
</a><a slot="tray-link" href="https://www.example.com">DWSD
</a><a slot="tray-link" href="https://www.example.com">DPW
</a><a slot="tray-link" href="https://www.example.com">Mayor's Office
</a><a slot="tray-link" href="https://www.example.com">DPD
</a><a slot="tray-link" href="https://www.example.com">City Clerk
</a><a slot="tray-link" href="https://www.example.com">HRD
</a><a slot="tray-link" href="https://www.example.com">DWSD
</a><a slot="tray-link" href="https://www.example.com">DPW
</a></cod-linktray>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, I wouldn't worry about getting the content of the links to looks quite right, or the chevron to appear. Just try to get the story to render with the links wrapped in li elements.
| const li = document.createElement('li'); | ||
|
|
||
| // append element to li | ||
| li.appendChild(element); | ||
|
|
||
| // append Chevron | ||
| const chevron = document.createElement('span'); | ||
| chevron.classList.add('chevron'); | ||
| chevron.innerHTML = ` | ||
| <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-right" viewBox="0 0 16 16"> | ||
| <path fill-rule="evenodd" d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708"/> | ||
| </svg> `; | ||
| li.appendChild(chevron); | ||
|
|
||
| // place li at the at the top of ul | ||
| const trayLinks = this.shadowRoot.querySelector('#tray-links'); | ||
| trayLinks.insertBefore(li, trayLinks.children[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite right. This is taking the slotted a elements, moving them out of the light DOM slot, and moving them into the shadow DOM (and out of the slot).
Notice how there's nothing inside the <slot name=tray-link> slot:
We want the slotted elements to remain in the light DOM slot, but wrapped in li elements.
To keep the elements in the light DOM, we need to add the slot=link-tray attribute to the li tag created, and then append the li elements to the element cod-linktray instead of appending to an element in the shadowRoot like '#tray-links.
You can see an example of how this is done at lines 88-94 for the section navigation component:
COD-Design-System/src/stable/components/SectionNavigation/SectionNavigation.js
Lines 88 to 94 in d908d2e
| // Wrap the 'a' element in an 'li' | |
| const li = document.createElement('li'); | |
| li.classList.add('nav-item'); | |
| li.setAttribute('slot', 'nav-items'); | |
| element.removeAttribute('slot'); | |
| element.parentNode.insertBefore(li, element); | |
| li.appendChild(element); |
Notice how the resulting HTML looks for the section navigation with the li elements inside the <slot name=nav-items>:



First draft of linktray, unfinished