Skip to content

Commit 3afa8c2

Browse files
committed
lady bug was messing with search
1 parent 81f364e commit 3afa8c2

4 files changed

Lines changed: 106 additions & 53 deletions

File tree

layouts/partials/home/modern-page-end.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
{{ partial "home/footer.html" $page }}
1818
</main>
1919
{{ partial "cookie-consent.html" $page }}
20+
{{- if not ($page.Scratch.Get "searchPaletteLoaded") -}}
2021
{{ partial "home/search-palette.html" $page }}
22+
{{- end -}}
2123
<script src="/js/home-nav.js" defer></script>
2224
<script src="/js/theme.js" defer></script>
2325
</body>

layouts/partials/home/search-palette.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@
6767
</div>
6868
</div>
6969
<script>
70-
window.CFD_SEARCH = {
71-
baseURL: {{ .Site.BaseURL | jsonify }},
72-
data: {
73-
home: {{ hugo.Data.home | jsonify | safeJS }},
74-
ai: {{ hugo.Data.ai | jsonify | safeJS }},
75-
games: {{ hugo.Data.games | jsonify | safeJS }},
76-
designlab: {{ hugo.Data.designlab | jsonify | safeJS }},
77-
store: {{ hugo.Data.store | jsonify | safeJS }}
78-
}
79-
};
70+
window.CFD_SEARCH = {{- dict
71+
"baseURL" .Site.BaseURL
72+
"data" (dict
73+
"home" hugo.Data.home
74+
"ai" hugo.Data.ai
75+
"games" hugo.Data.games
76+
"designlab" hugo.Data.designlab
77+
"store" hugo.Data.store
78+
)
79+
| jsonify | safeJS -}};
8080
</script>
8181
<script src="/js/search-palette.js" defer></script>
8282
{{- end -}}

static/js/ladybug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@
553553

554554
window.addEventListener('click', function (e) {
555555
if (isLadybugGateHidden()) return;
556-
if (e.target && e.target.closest && e.target.closest('#ladybug-reveal-alpha, #ladybug-reveal-beta')) return;
556+
if (e.target && e.target.closest && e.target.closest('#ladybug-reveal-alpha, #ladybug-reveal-beta, [data-search-open], #search-palette, .search-palette')) return;
557557
if (state === 'h1feast') {
558558
endH1Feast(true);
559559
}

static/js/search-palette.js

Lines changed: 93 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
var allItems = [];
66
var isEmbedded = false;
77
var isOpen = false;
8+
var triggersBound = false;
89

910
function $(sel, root) {
1011
return (root || document).querySelector(sel);
@@ -72,9 +73,10 @@
7273

7374
function absUrl(url, base) {
7475
if (!url) return '#';
76+
var root = (base || '/').replace(/"/g, '').replace(/\/+$/, '');
7577
if (/^https?:\/\//.test(url)) return url;
76-
if (url.charAt(0) === '/') return base.replace(/\/?$/, '') + url;
77-
return base + url;
78+
if (url.charAt(0) === '/') return root + url;
79+
return root + '/' + url;
7880
}
7981

8082
function highlight(text, q) {
@@ -127,7 +129,7 @@
127129
resultsList.hidden = false;
128130

129131
var base = (window.CFD_SEARCH && window.CFD_SEARCH.baseURL) || '/';
130-
matches.slice(0, 24).forEach(function (item, i) {
132+
matches.slice(0, 24).forEach(function (item) {
131133
var li = document.createElement('li');
132134
li.setAttribute('role', 'option');
133135
var a = document.createElement('a');
@@ -183,27 +185,39 @@
183185
}
184186
}
185187

188+
function closeMobileMenu() {
189+
var menuBtn = document.getElementById('mobile-menu-btn');
190+
var menu = document.getElementById('mobile-menu');
191+
if (menuBtn && menu && menuBtn.getAttribute('aria-expanded') === 'true') {
192+
menuBtn.click();
193+
}
194+
}
195+
186196
function open(query) {
187-
if (!palette || isOpen) return;
197+
if (!palette) return;
198+
if (isEmbedded) {
199+
if (input) {
200+
input.focus();
201+
runSearch(query || input.value || '');
202+
}
203+
return;
204+
}
205+
if (isOpen) return;
206+
207+
closeMobileMenu();
188208
isOpen = true;
209+
palette.removeAttribute('hidden');
189210
palette.hidden = false;
190211
palette.setAttribute('aria-hidden', 'false');
191212
document.body.classList.add('search-palette-open');
192-
if (!isEmbedded) {
193-
requestAnimationFrame(function () {
194-
palette.classList.add('is-visible');
195-
});
196-
} else {
197-
palette.classList.add('is-visible', 'search-palette--embedded');
198-
}
213+
requestAnimationFrame(function () {
214+
palette.classList.add('is-visible');
215+
});
199216
if (input) {
200217
input.value = query || '';
201218
input.focus();
202219
runSearch(input.value);
203220
}
204-
var url = new URL(window.location.href);
205-
if (query) url.searchParams.set('q', query);
206-
if (isEmbedded) window.history.replaceState({}, '', url);
207221
}
208222

209223
function close() {
@@ -214,6 +228,7 @@
214228
document.body.classList.remove('search-palette-open');
215229
setTimeout(function () {
216230
palette.hidden = true;
231+
palette.setAttribute('hidden', '');
217232
if (input) input.value = '';
218233
renderResults([], '');
219234
}, 200);
@@ -237,36 +252,34 @@
237252
}
238253

239254
function bindTriggers() {
240-
document.querySelectorAll('[data-search-open]').forEach(function (btn) {
241-
btn.addEventListener('click', function (e) {
242-
e.preventDefault();
243-
open('');
244-
});
245-
});
246-
document.querySelectorAll('[data-search-close]').forEach(function (el) {
247-
el.addEventListener('click', close);
248-
});
249-
}
255+
if (triggersBound) return;
256+
triggersBound = true;
250257

251-
function init() {
252-
if (!window.CFD_SEARCH) return;
258+
document.addEventListener(
259+
'click',
260+
function (e) {
261+
var openBtn = e.target.closest('[data-search-open]');
262+
if (openBtn) {
263+
e.preventDefault();
264+
e.stopPropagation();
265+
open('');
266+
return;
267+
}
268+
if (e.target.closest('[data-search-close]')) {
269+
e.preventDefault();
270+
close();
271+
}
272+
},
273+
true
274+
);
275+
}
253276

254-
palette = document.getElementById('search-palette');
277+
function bindPalette() {
255278
if (!palette) return;
256279

257-
backdrop = $('.search-palette__backdrop', palette);
258-
dialog = $('.search-palette__dialog', palette);
259-
input = document.getElementById('search-palette-input');
260-
resultsList = document.getElementById('search-palette-results');
261-
defaultPanel = document.getElementById('search-palette-default');
262-
emptyPanel = document.getElementById('search-palette-empty');
263-
isEmbedded =
264-
document.body.classList.contains('search-palette-page') ||
265-
/^\/search\/?$/.test(window.location.pathname);
266-
267-
allItems = buildIndex(window.CFD_SEARCH.data);
268-
initKbdLabels();
269-
bindTriggers();
280+
document.querySelectorAll('[data-search-close]').forEach(function (el) {
281+
el.addEventListener('click', close);
282+
});
270283

271284
if (input) {
272285
var debounce;
@@ -300,6 +313,32 @@
300313
}
301314
});
302315
}
316+
}
317+
318+
function init() {
319+
if (!window.CFD_SEARCH || !window.CFD_SEARCH.data) {
320+
return false;
321+
}
322+
323+
palette = document.getElementById('search-palette');
324+
if (!palette) {
325+
return false;
326+
}
327+
328+
backdrop = $('.search-palette__backdrop', palette);
329+
dialog = $('.search-palette__dialog', palette);
330+
input = document.getElementById('search-palette-input');
331+
resultsList = document.getElementById('search-palette-results');
332+
defaultPanel = document.getElementById('search-palette-default');
333+
emptyPanel = document.getElementById('search-palette-empty');
334+
isEmbedded =
335+
document.body.classList.contains('search-palette-page') ||
336+
/^\/search\/?$/.test(window.location.pathname);
337+
338+
allItems = buildIndex(window.CFD_SEARCH.data);
339+
initKbdLabels();
340+
bindTriggers();
341+
bindPalette();
303342

304343
document.addEventListener('keydown', function (e) {
305344
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
@@ -312,6 +351,7 @@
312351
});
313352

314353
if (isEmbedded) {
354+
palette.removeAttribute('hidden');
315355
palette.hidden = false;
316356
palette.classList.add('is-visible', 'search-palette--embedded');
317357
isOpen = true;
@@ -324,11 +364,22 @@
324364
}
325365

326366
window.cfdSearchPalette = { open: open, close: close };
367+
return true;
368+
}
369+
370+
function boot() {
371+
bindTriggers();
372+
if (init()) return;
373+
var attempts = 0;
374+
var timer = setInterval(function () {
375+
attempts += 1;
376+
if (init() || attempts > 40) clearInterval(timer);
377+
}, 50);
327378
}
328379

329380
if (document.readyState === 'loading') {
330-
document.addEventListener('DOMContentLoaded', init);
381+
document.addEventListener('DOMContentLoaded', boot);
331382
} else {
332-
init();
383+
boot();
333384
}
334385
})();

0 commit comments

Comments
 (0)