torexdev's picture
responsiveligi yaxshilab ber.
eb3b59e verified
Raw History Blame Contribute Delete
2.97 kB
class CustomSidebar extends HTMLElement {
static get observedAttributes() {
return ['contacts'];
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'contacts') {
this.renderContacts(JSON.parse(newValue));
}
}
renderContacts(contacts) {
const contactsContainer = this.shadowRoot.querySelector('.contacts-container');
if (contactsContainer) {
contactsContainer.innerHTML = contacts.map(contact => `
<div class="contact-item flex items-center p-3 hover:bg-gray-200 cursor-pointer border-b border-gray-100">
<div class="relative mr-3">
<img src="${contact.avatar}" alt="${contact.name}" class="w-12 h-12 rounded-full object-cover">
${contact.unread > 0 ? `
<div class="absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center">
${contact.unread}
</div>
` : ''}
</div>
<div class="flex-1">
<div class="flex justify-between items-center">
<h3 class="font-semibold">${contact.name}</h3>
<span class="text-xs text-gray-500">${contact.time}</span>
</div>
<p class="text-sm text-gray-600 truncate">${contact.lastMessage}</p>
</div>
</div>
`).join('');
}
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
.sidebar {
width: 350px;
background-color: white;
border-right: 1px solid #e5e7eb;
overflow-y: auto;
}
.contact-item {
transition: background-color 0.2s;
}
@media (max-width: 768px) {
.sidebar {
width: 100%;
position: fixed;
top: 56px;
bottom: 0;
z-index: 20;
transform: translateX(-100%);
transition: transform 0.3s ease;
}
.sidebar.active {
transform: translateX(0);
}
.contact-item {
padding: 12px 16px;
}
}
</style>
<div class="sidebar h-full">
<div class="contacts-container"></div>
</div>
`;
}
}
customElements.define('custom-sidebar', CustomSidebar);