Repository navigation
Expand file tree
/
Copy pathmobile.js
More file actions
220 lines (199 loc) · 8.21 KB
/
Copy pathmobile.js
File metadata and controls
220 lines (199 loc) · 8.21 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
/** Mobile nav toggle for Design Component pages. */
(function () {
function ready(fn) {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fn, { once: true });
} else {
fn();
}
}
function scrollToHashTarget(behavior) {
const raw = window.location.hash;
if (!raw || raw === "#top") return false;
const el = document.querySelector(raw);
if (!el) return false;
const header = document.querySelector("header");
const offset = (header && header.offsetHeight) || 72;
const top = el.getBoundingClientRect().top + window.scrollY - offset - 8;
window.scrollTo({ top: Math.max(0, top), behavior: behavior || "smooth" });
return true;
}
// Pin every image/video box to its natural ratio so it can never stretch,
// whatever the zoom level or flex sizing quirks of the browser.
function lockAspectRatio(media) {
const w = media.naturalWidth || media.videoWidth;
const h = media.naturalHeight || media.videoHeight;
if (!w || !h) return;
const ratio = w + " / " + h;
// Only write on change: the observer below watches style attributes.
if (media.style.aspectRatio !== ratio) media.style.aspectRatio = ratio;
if (media.style.objectFit !== "contain") media.style.objectFit = "contain";
}
function initAspectRatioLock() {
const onLoad = (e) => {
const t = e.target;
if (t && (t.tagName === "IMG" || t.tagName === "VIDEO")) lockAspectRatio(t);
};
document.addEventListener("load", onLoad, true);
document.addEventListener("loadedmetadata", onLoad, true);
const sweep = () => document.querySelectorAll("img, video").forEach(lockAspectRatio);
sweep();
// The Design Component runtime remounts markup and rewrites inline styles;
// cached images re-rendered that way never fire another load event.
let queued = false;
new MutationObserver(() => {
if (queued) return;
queued = true;
queueMicrotask(() => {
queued = false;
sweep();
});
}).observe(document, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ["style", "src"]
});
}
initAspectRatioLock();
function initHashScroll() {
if (!window.location.hash || window.location.hash === "#top") return;
let tries = 0;
const tick = () => {
if (scrollToHashTarget(tries++ ? "smooth" : "auto") || tries > 60) return;
setTimeout(tick, 100);
};
tick();
}
function tagTimeline() {
const timeline =
document.querySelector(".timeline") ||
document.querySelector('#experience [style*="width:1000"], #experience [style*="width: 1000"]');
if (!timeline) return false;
timeline.classList.add("timeline");
const rows = Array.from(timeline.children).filter((el) => el.tagName === "DIV" && !el.hasAttribute("aria-hidden"));
rows.forEach((row) => {
row.classList.add("timeline-row");
Array.from(row.children).forEach((col) => {
if (col.classList.contains("timeline-marker") || col.classList.contains("timeline-card") || col.classList.contains("timeline-meta")) {
return;
}
const style = col.getAttribute("style") || "";
const isMarker =
/flex:\s*0\s*0\s*20px/.test(style) ||
(col.children.length === 1 &&
col.querySelector('[style*="border-radius:9999px"], [style*="border-radius: 9999px"]'));
const isCard =
/padding:\s*24px/.test(style) &&
(/background:\s*#fff\b/.test(style) ||
/background:\s*rgb\(\s*255\s*,\s*255\s*,\s*255\s*\)/.test(style) ||
/border:\s*1px\s+solid/.test(style));
if (isMarker) col.classList.add("timeline-marker");
else if (isCard) col.classList.add("timeline-card");
else col.classList.add("timeline-meta");
});
});
return rows.length > 0 && rows.every((row) => row.querySelector(".timeline-meta") && row.querySelector(".timeline-card"));
}
function enhance() {
// Tag timeline first so a nav failure cannot block mobile stacking.
try {
tagTimeline();
} catch (err) {
console.warn("timeline tagging failed", err);
}
const headerInner = document.querySelector("header > div");
const nav = document.querySelector("header nav");
if (headerInner && nav && !headerInner.querySelector(".nav-toggle")) {
try {
nav.classList.add("site-nav");
const btn = document.createElement("button");
btn.type = "button";
btn.className = "nav-toggle";
btn.setAttribute("aria-expanded", "false");
btn.setAttribute("aria-controls", "site-nav");
btn.setAttribute("aria-label", "Open menu");
btn.innerHTML =
'<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M4 7h16"/><path d="M4 12h16"/><path d="M4 17h16"/></svg>';
nav.id = "site-nav";
headerInner.insertBefore(btn, nav);
const sync = (open) => {
document.body.classList.toggle("nav-open", open);
btn.setAttribute("aria-expanded", open ? "true" : "false");
btn.setAttribute("aria-label", open ? "Close menu" : "Open menu");
btn.innerHTML = open
? '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M6 6l12 12"/><path d="M18 6L6 18"/></svg>'
: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><path d="M4 7h16"/><path d="M4 12h16"/><path d="M4 17h16"/></svg>';
};
btn.addEventListener("click", () => {
sync(!document.body.classList.contains("nav-open"));
});
nav.addEventListener("click", (e) => {
if (e.target.closest("a")) sync(false);
});
} catch (err) {
console.warn("nav enhance failed", err);
}
}
// Tag structural hooks the stylesheet relies on
const root = document.querySelector('[style*="min-width"]');
if (root) {
root.style.minWidth = "0";
root.style.width = "100%";
}
const crumb = document.querySelector("header + div");
if (crumb && crumb.textContent.includes("Projects")) crumb.classList.add("crumb-bar");
document.querySelectorAll("nav").forEach((n) => {
if (n.querySelector('a[href*="Previous"], a[href*="prev"]') || /Previous:|Next:/.test(n.textContent)) {
n.classList.add("pager");
}
});
document.querySelectorAll("body nav").forEach((n) => {
if (n.classList.contains("site-nav")) return;
if (/All case studies/.test(n.textContent || "")) n.classList.add("pager");
});
}
ready(() => {
document.addEventListener("click", (e) => {
const a = e.target.closest('a[href^="#"]');
if (!a) return;
const href = a.getAttribute("href");
if (!href || href === "#" || href === "#top") return;
const el = document.querySelector(href);
if (!el) return;
e.preventDefault();
history.pushState(null, "", href);
scrollToHashTarget("smooth");
});
// DC runtime boots async (React UMD). Retry until header + timeline are ready.
let tries = 0;
const tick = () => {
enhance();
initHashScroll();
const navReady = !!document.querySelector(".nav-toggle");
const timelineReady = !document.querySelector("#experience") || !!document.querySelector(".timeline-row");
if ((!navReady || !timelineReady) && tries++ < 80) {
setTimeout(tick, 100);
}
};
tick();
// Re-tag if the Design Component remounts experience rows.
const observe = () => {
const host = document.querySelector("#dc-root") || document.body;
if (!host || host.dataset.timelineObserved) return;
host.dataset.timelineObserved = "1";
let queued = false;
const observer = new MutationObserver(() => {
if (queued) return;
queued = true;
requestAnimationFrame(() => {
queued = false;
tagTimeline();
});
});
observer.observe(host, { childList: true, subtree: true });
};
observe();
setTimeout(observe, 500);
});
})();