pulse-whoop / components /navbar.js
CSNET's picture
ช่วยแก้ JS และกราฟ ไม่ให้เพี้ยนที
9539581 verified
Raw History Blame Contribute Delete
7.35 kB
class MainNavbar extends HTMLElement {
constructor() {
super();
this.currentPage = 'today';
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
this.setupEventListeners();
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
position: fixed;
bottom: 0;
left: 0;
right: 0;
z-index: 50;
}
.navbar {
background: rgba(17, 24, 39, 0.8);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border-top: 1px solid rgba(75, 85, 99, 0.3);
display: flex;
justify-content: space-around;
padding: 0.5rem 0 calc(0.5rem + env(safe-area-inset-bottom));
}
.nav-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
padding: 0.5rem;
text-decoration: none;
color: #9ca3af;
transition: all 0.3s ease;
position: relative;
border-radius: 12px;
margin: 0 0.25rem;
}
.nav-item.active {
color: #34d399;
background: rgba(52, 211, 153, 0.1);
}
.nav-item:not(.active):hover {
color: #e5e7eb;
background: rgba(107, 114, 128, 0.1);
}
.nav-icon {
width: 24px;
height: 24px;
margin-bottom: 0.25rem;
stroke-width: 2;
}
.nav-label {
font-size: 0.7rem;
font-weight: 500;
font-family: 'Prompt', sans-serif;
}
.active-indicator {
position: absolute;
top: -2px;
width: 20px;
height: 3px;
background: linear-gradient(90deg, #34d399, #10b981);
border-radius: 2px;
opacity: 0;
transition: opacity 0.3s ease;
}
.nav-item.active .active-indicator {
opacity: 1;
}
@media (max-width: 640px) {
.navbar {
padding-bottom: calc(0.5rem + env(safe-area-inset-bottom));
}
}
</style>
<nav class="navbar">
<a href="index.html" class="nav-item active" data-page="today">
<div class="active-indicator"></div>
<i class="nav-icon" data-feather="home"></i>
<span class="nav-label">วันนี้</span>
</a>
<a href="sleep.html" class="nav-item" data-page="sleep">
<div class="active-indicator"></div>
<i class="nav-icon" data-feather="moon"></i>
<span class="nav-label">การนอน</span>
</a>
<a href="activity.html" class="nav-item" data-page="activity">
<div class="active-indicator"></div>
<i class="nav-icon" data-feather="zap"></i>
<span class="nav-label">ออกกำลังกาย</span>
</a>
<a href="nutrition.html" class="nav-item" data-page="nutrition">
<div class="active-indicator"></div>
<i class="nav-icon" data-feather="coffee"></i>
<span class="nav-label">โภชนาการ</span>
</a>
<a href="profile.html" class="nav-item" data-page="profile">
<div class="active-indicator"></div>
<i class="nav-icon" data-feather="user"></i>
<span class="nav-label">โปรไฟล์</span>
</a>
</nav>
`;
}
setupEventListeners() {
try {
const navItems = this.shadowRoot.querySelectorAll('.nav-item');
// Set active page based on current URL
const currentPath = window.location.pathname;
const pageMap = {
'/index.html': 'today',
'/sleep.html': 'sleep',
'/activity.html': 'activity',
'/nutrition.html': 'nutrition',
'/profile.html': 'profile'
};
const currentPage = pageMap[currentPath] || 'today';
this.setActivePage(currentPage);
navItems.forEach(item => {
item.addEventListener('click', (e) => {
try {
const page = item.dataset.page;
// Dispatch custom event for other components
this.dispatchEvent(new CustomEvent('page-change', {
detail: { page },
bubbles: true,
composed: true
}));
// Log navigation (in production, this would be analytics)
console.log(`เปลี่ยนหน้าไปที่: ${page}`);
} catch (error) {
console.error('Navigation error:', error);
}
});
});
} catch (error) {
console.error('Setup navbar listeners error:', error);
}
}
setActivePage(page) {
try {
const navItems = this.shadowRoot.querySelectorAll('.nav-item');
navItems.forEach(item => {
if (item.dataset.page === page) {
item.classList.add('active');
} else {
item.classList.remove('active');
}
});
this.currentPage = page;
} catch (error) {
console.error('Set active page error:', error);
}
}
// Public method to set page from outside
navigate(page) {
this.setActivePage(page);
}
// Auto-detect current page on first load
detectCurrentPage() {
const currentPath = window.location.pathname;
const pageMap = {
'/index.html': 'today',
'/sleep.html': 'sleep',
'/activity.html': 'activity',
'/nutrition.html': 'nutrition',
'/profile.html': 'profile'
};
const currentPage = pageMap[currentPath] || 'today';
this.setActivePage(currentPage);
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.currentPage = 'today';
this.render();
this.setupEventListeners();
this.detectCurrentPage(); // Auto-detect current page
}
}
customElements.define('main-navbar', MainNavbar);