Skip to content

Conversation

@SBBlee
Copy link
Contributor

@SBBlee SBBlee commented Jun 10, 2025

First draft of linktray, unfinished

image

@SBBlee SBBlee requested a review from maxatdetroit June 10, 2025 22:41
@SBBlee SBBlee self-assigned this Jun 10, 2025
@maxatdetroit maxatdetroit marked this pull request as draft June 11, 2025 12:44
Comment on lines 62 to 73
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> `;
Copy link
Contributor

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.

image

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>

Copy link
Contributor

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.

Comment on lines 69 to 85
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]);
Copy link
Contributor

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:

image

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:

// 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>:

image

@SBBlee SBBlee requested a review from maxatdetroit June 17, 2025 20:40
@maxatdetroit maxatdetroit marked this pull request as ready for review June 24, 2025 15:44
@maxatdetroit maxatdetroit linked an issue Jul 15, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create LinkTray component

3 participants