Spaces:
Running
Running
File size: 7,354 Bytes
4a7cb62 7ee89bc 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 9539581 4a7cb62 7ee89bc 4a7cb62 | 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | 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); |