Tabs Support Tickets
Open items from a list in closable tabs, like tickets in a support inbox. An element with data-sv-tabs-close in a tab closes it on click or with Delete, and a tabclosed listener cancels the close and sets hidden on the tab until a click on the ticket in the list shows it again.
Maya Chen · 2 min ago
I was charged twice for my March invoice. Can you refund the second payment?
Omar Haddad · 1 hour ago
The reset link in the email says it has expired, even when I open it right away.
Kenji Sato · Yesterday
Is there a way to export all our projects as a CSV file for our yearly report?
Code
The following example assumes that you have imported the component and set up the theme.
<div class="tabs-support-tickets">48 collapsed lines
<sv-tabs aria-label="Support tickets" value="inbox"> <button value="inbox">Inbox</button> <button value="1042"> Refund request <span aria-hidden="true" data-sv-tabs-close> <svg viewBox="0 0 24 24"><path d="M18 6 6 18M6 6l12 12" /></svg> </span> </button> <button hidden value="1043"> Password reset <span aria-hidden="true" data-sv-tabs-close> <svg viewBox="0 0 24 24"><path d="M18 6 6 18M6 6l12 12" /></svg> </span> </button> <button hidden value="1044"> CSV export <span aria-hidden="true" data-sv-tabs-close> <svg viewBox="0 0 24 24"><path d="M18 6 6 18M6 6l12 12" /></svg> </span> </button>
<div slot="inbox"> <button type="button" class="ticket" data-ticket="1042"> Refund request <span>Maya Chen · 2 min ago</span> </button> <button type="button" class="ticket" data-ticket="1043"> Password reset <span>Omar Haddad · 1 hour ago</span> </button> <button type="button" class="ticket" data-ticket="1044"> CSV export <span>Kenji Sato · Yesterday</span> </button> </div> <div class="ticket-panel" slot="1042"> <span>Maya Chen · 2 min ago</span> <p>I was charged twice for my March invoice. Can you refund the second payment?</p> </div> <div class="ticket-panel" slot="1043"> <span>Omar Haddad · 1 hour ago</span> <p>The reset link in the email says it has expired, even when I open it right away.</p> </div> <div class="ticket-panel" slot="1044"> <span>Kenji Sato · Yesterday</span> <p>Is there a way to export all our projects as a CSV file for our yearly report?</p> </div> </sv-tabs></div>
<script>29 collapsed lines
const ticketTabs = document.querySelector(".tabs-support-tickets sv-tabs");
ticketTabs.addEventListener("click", event => { const ticketId = event.target.closest(".ticket")?.dataset.ticket; if (!ticketId) return;
const ticketTab = ticketTabs.querySelector(`button[value="${ticketId}"]`); ticketTab.hidden = false; ticketTabs.value = ticketId; ticketTab.focus(); });
ticketTabs.addEventListener("tabclosed", event => { const ticketId = event.detail.value; const ticketTab = ticketTabs.querySelector(`button[value="${ticketId}"]`); const ticketPanel = ticketTabs.querySelector(`[slot="${ticketId}"]`); const hasFocus = ticketTab.matches(":focus") || ticketPanel.matches(":focus-within"); event.preventDefault();
if (ticketTabs.value === ticketId) { ticketTabs.value = "inbox"; }
if (hasFocus) { ticketTabs.querySelector('button[value="inbox"]').focus(); }
ticketTab.hidden = true; });</script>
<style>75 collapsed lines
.tabs-support-tickets { max-inline-size: 32rem; margin-inline: auto;
sv-tabs { --sv-tabs-panel-pad: 0;
overflow: hidden; border: 1px solid var(--sv-gray-5); border-radius: var(--sv-bdr-rad-md); }
sv-tabs > button:has([data-sv-tabs-close]) { padding-inline-end: 0.5em; }
[data-sv-tabs-close] { display: inline-grid; place-items: center; inline-size: 1lh; block-size: 1lh; margin-inline-start: 0.25em; vertical-align: middle; border-radius: 50%;
&:hover { background-color: var(--sv-gray-5); }
svg { inline-size: 1em; block-size: 1em; fill: none; stroke: currentcolor; stroke-width: 2; stroke-linecap: round; } }
.ticket { display: grid; gap: 0.2em; inline-size: 100%; padding: 0.75em 1em; font: inherit; font-weight: 600; color: inherit; text-align: start; cursor: pointer; background-color: transparent; border: 0;
& + & { border-block-start: 1px solid var(--sv-gray-5); }
&:hover { background-color: var(--sv-gray-6); } }
:is(.ticket, .ticket-panel) span { font-size: 0.85em; font-weight: 400; color: var(--sv-gray-3); }
.ticket-panel { padding: 1em;
p { margin: 0.5em 0 0; } } }</style>