Spaces:
Running
Running
File size: 2,733 Bytes
05382ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
class CustomDropdown extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-btn {
background: none;
border: none;
cursor: pointer;
color: #374151;
font-weight: 500;
transition: color 0.2s;
display: flex;
align-items: center;
gap: 0.25rem;
}
.dropdown-btn:hover {
color: #13aa52;
}
.dropdown-content {
position: absolute;
background-color: white;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.1);
border-radius: 0.5rem;
z-index: 1;
opacity: 0;
visibility: hidden;
transform: translateY(10px);
transition: all 0.2s ease;
}
.dropdown-content.show {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.dropdown-content a {
color: #374151;
padding: 0.75rem 1rem;
text-decoration: none;
display: block;
transition: background-color 0.2s;
font-size: 0.875rem;
}
.dropdown-content a:hover {
background-color: #f3f4f6;
color: #13aa52;
}
.icon {
transition: transform 0.2s;
}
.dropdown.active .icon {
transform: rotate(180deg);
}
</style>
<div class="dropdown">
<button class="dropdown-btn">
<span>${this.getAttribute('title') || 'Menu'}</span>
<svg class="icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</button>
<div class="dropdown-content">
<slot></slot>
</div>
</div>
`;
const dropdownBtn = this.shadowRoot.querySelector('.dropdown-btn');
const dropdownContent = this.shadowRoot.querySelector('.dropdown-content');
const dropdown = this.shadowRoot.querySelector('.dropdown');
dropdownBtn.addEventListener('click', (e) => {
e.stopPropagation();
dropdownContent.classList.toggle('show');
dropdown.classList.toggle('active');
});
// Close when clicking outside
document.addEventListener('click', () => {
dropdownContent.classList.remove('show');
dropdown.classList.remove('active');
});
}
}
customElements.define('custom-dropdown', CustomDropdown); |