-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscripts.js
More file actions
294 lines (249 loc) · 8.56 KB
/
Copy pathscripts.js
File metadata and controls
294 lines (249 loc) · 8.56 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const allBtns = document.querySelectorAll("liquid-btn[data-panel]");
let currentPanel = "home";
let transitioning = false;
const isMobileView = () => window.innerWidth <= MOBILE_BREAKPOINT;
let bentoState = "home";
function setBentoState(state) {
if (bentoState === state) return;
bentoState = state;
if (isMobileView()) return;
const mainCard = document.getElementById("main-card");
const bentoRight = document.getElementById("bento-right");
if (state === "detail") {
mainCard.style.left = "12.55vw";
bentoRight.style.left = "130%";
bentoRight.style.pointerEvents = "none";
} else {
mainCard.style.left = "";
bentoRight.style.left = "";
bentoRight.style.pointerEvents = "";
}
}
function navigateTo(panel) {
if (panel === currentPanel || transitioning) return;
transitioning = true;
allBtns.forEach((btn) => {
if (btn.dataset.panel === panel) btn.id = "current";
else btn.removeAttribute("id");
});
const outPanel = document.getElementById("panel-" + currentPanel);
const inPanel = document.getElementById("panel-" + panel);
const fromHome = currentPanel === "home";
const toHome = panel === "home";
const layoutChange = fromHome || toHome;
outPanel.style.transition = "opacity 0.2s ease";
outPanel.style.opacity = "0";
if (layoutChange && !isMobileView()) {
setBentoState(toHome ? "home" : "detail");
}
setTimeout(() => {
outPanel.classList.remove("active");
inPanel.classList.add("active");
inPanel.style.opacity = "0";
inPanel.style.transition = "opacity 0.25s ease";
requestAnimationFrame(() => {
inPanel.style.opacity = "1";
});
currentPanel = panel;
setTimeout(() => {
outPanel.style.opacity = "";
outPanel.style.transition = "";
inPanel.style.transition = "";
transitioning = false;
}, 250);
}, 200);
}
allBtns.forEach((btn) => {
btn.addEventListener("click", () => navigateTo(btn.dataset.panel));
});
const MOBILE_BREAKPOINT = 768;
const MOBILE_SIZES = {
mainCard: { vw: "88", vh: "58", fontSize: "16px" },
sideCard: { vw: "88", vh: "30", fontSize: "15px" },
navBtn: { vw: "20", vh: "6.5", fontSize: null },
repoBtn: { vw: "76", vh: "6.2", fontSize: null },
topbar: { vw: "92", vh: "8.368", fontSize: null },
};
const originalAttrs = new WeakMap();
// per-element generation counter, incremented on every size change so stale
// requestAnimationFrame font-size overrides can detect they've been superseded
const fontGeneration = new WeakMap();
function saveOriginal(el) {
if (!originalAttrs.has(el)) {
originalAttrs.set(el, {
vw: el.getAttribute("vw-width"),
vh: el.getAttribute("vh-height"),
fontSize: el.style.fontSize || "",
});
}
}
function applyMobileSize(el, sizes) {
el.setAttribute("vw-width", sizes.vw);
el.setAttribute("vh-height", sizes.vh);
if (sizes.fontSize) {
const gen = (fontGeneration.get(el) || 0) + 1;
fontGeneration.set(el, gen);
const enforce = () => {
el.style.fontSize = sizes.fontSize;
};
enforce(); // apply immediately after attribute swap
const obs = new MutationObserver(() => {
if (fontGeneration.get(el) !== gen) {
obs.disconnect();
return;
}
if (el.style.fontSize !== sizes.fontSize) enforce();
});
obs.observe(el, { attributes: true, attributeFilter: ["style"] });
setTimeout(() => obs.disconnect(), 500);
}
}
function restoreOriginal(el) {
const orig = originalAttrs.get(el);
if (!orig) return;
// bump generation so any pending mobile rAF font-size override is cancelled
fontGeneration.set(el, (fontGeneration.get(el) || 0) + 1);
if (orig.vw !== null) el.setAttribute("vw-width", orig.vw);
if (orig.vh !== null) el.setAttribute("vh-height", orig.vh);
el.style.fontSize = orig.fontSize;
}
function applyLayout(isMobile) {
const mainCard = document.getElementById("main-card");
const sideCards = document.querySelectorAll("#bento-right liquid-glass");
const navBtns = document.querySelectorAll(".header-buttons liquid-btn");
if (mainCard) saveOriginal(mainCard);
sideCards.forEach(saveOriginal);
navBtns.forEach(saveOriginal);
const repoBtns = document.querySelectorAll("#panel-repo liquid-btn");
repoBtns.forEach(saveOriginal);
const topbarGlass = document.querySelector(".topbar liquid-glass");
if (topbarGlass) saveOriginal(topbarGlass);
if (isMobile) {
if (mainCard) applyMobileSize(mainCard, MOBILE_SIZES.mainCard);
sideCards.forEach((el) => applyMobileSize(el, MOBILE_SIZES.sideCard));
navBtns.forEach((el) => applyMobileSize(el, MOBILE_SIZES.navBtn));
repoBtns.forEach((el) => applyMobileSize(el, MOBILE_SIZES.repoBtn));
if (topbarGlass) applyMobileSize(topbarGlass, MOBILE_SIZES.topbar);
} else {
if (mainCard) restoreOriginal(mainCard);
sideCards.forEach(restoreOriginal);
navBtns.forEach(restoreOriginal);
repoBtns.forEach(restoreOriginal);
if (topbarGlass) restoreOriginal(topbarGlass);
}
}
let lastIsMobile = null;
function checkLayout() {
const isMobile = window.innerWidth <= MOBILE_BREAKPOINT;
if (isMobile !== lastIsMobile) {
lastIsMobile = isMobile;
applyLayout(isMobile);
}
}
customElements.whenDefined("liquid-glass").then(() => {
customElements.whenDefined("liquid-btn").then(() => {
checkLayout();
});
});
let resizeTimer;
window.addEventListener("resize", () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(checkLayout, 160);
});
const hamburger = document.getElementById("hamburger");
const mobileMenu = document.getElementById("mobile-menu");
const mobileNavBtns = document.querySelectorAll(".mobile-nav-btn");
function setMobileMenuOpen(open) {
mobileMenu.classList.toggle("open", open);
hamburger.setAttribute("aria-expanded", open);
mobileMenu.setAttribute("aria-hidden", !open);
const icon = document.getElementById("hamburger-icon");
icon.className = open ? "fa-solid fa-xmark" : "fa-solid fa-bars";
}
function updateMobileActiveBtn(panel) {
mobileNavBtns.forEach((btn) => {
btn.classList.toggle("active", btn.dataset.panel === panel);
});
}
hamburger.addEventListener("click", (e) => {
e.stopPropagation();
setMobileMenuOpen(!mobileMenu.classList.contains("open"));
});
// close menu when tapping outside
document.addEventListener("click", (e) => {
if (
mobileMenu.classList.contains("open") &&
!mobileMenu.contains(e.target) &&
e.target !== hamburger
) {
setMobileMenuOpen(false);
}
});
mobileNavBtns.forEach((btn) => {
btn.addEventListener("click", () => {
setMobileMenuOpen(false);
updateMobileActiveBtn(btn.dataset.panel);
navigateTo(btn.dataset.panel);
});
});
// keep mobile active state in sync with desktop nav
// patch navigateTo to also update mobile buttons
const _origNavigateTo = navigateTo;
allBtns.forEach((btn) => {
btn.addEventListener("click", () => {
updateMobileActiveBtn(btn.dataset.panel);
});
});
// set initial active state
updateMobileActiveBtn(currentPanel);
const VALID_PANELS = ["home", "about", "repo"];
function getPanelFromHash() {
const hash = window.location.hash.replace("#", "").toLowerCase();
return VALID_PANELS.includes(hash) ? hash : "home";
}
function navigateToHash(panel, pushState = true) {
// update the URL hash without triggering a page reload
if (pushState) {
history.pushState(null, "", panel === "home" ? "#home" : `#${panel}`);
}
navigateTo(panel);
updateMobileActiveBtn(panel);
}
customElements.whenDefined("liquid-glass").then(() => {
customElements.whenDefined("liquid-btn").then(() => {
const initial = getPanelFromHash();
if (initial !== currentPanel) {
transitioning = false;
navigateTo(initial);
updateMobileActiveBtn(initial);
// sync the topbar button highlight
allBtns.forEach((btn) => {
if (btn.dataset.panel === initial) btn.id = "current";
else btn.removeAttribute("id");
});
}
});
});
// update hash when desktop nav buttons are clicked
allBtns.forEach((btn) => {
btn.addEventListener("click", () => {
history.pushState(null, "", `#${btn.dataset.panel}`);
});
});
// update hash when mobile nav buttons are clicked
// (they already call navigateTo via their own listener, just push state here)
mobileNavBtns.forEach((btn) => {
btn.addEventListener("click", () => {
history.pushState(null, "", `#${btn.dataset.panel}`);
});
});
// handle browser back/forward
window.addEventListener("popstate", () => {
const panel = getPanelFromHash();
navigateTo(panel);
updateMobileActiveBtn(panel);
allBtns.forEach((btn) => {
if (btn.dataset.panel === panel) btn.id = "current";
else btn.removeAttribute("id");
});
});