/**
* iOS 26-style floating glass tab bar with a tab-bar accessory
* ("now playing" for the planet) and a search button in the same row.
* Minimises to a compact pill while the feed scrolls down.
*/
import { icon, FAMILY_ICON } from './icons.js';
import { $, $$, esc, ago, fmtNum, haptic } from './dom.js';
export const TABS = [
{ id: 'now', label: 'Now', icon: 'now' },
{ id: 'globe', label: 'Globe', icon: 'globe' },
{ id: 'capture', label: 'Capture', icon: 'viewfinder' },
{ id: 'threads', label: 'Threads', icon: 'threads' },
{ id: 'you', label: 'You', icon: 'you' },
];
export function createTabBar(nav, { onSelect, onSearch, onAccessory }) {
nav.innerHTML = `
${TABS.map(
(
t,
) => ``,
).join('')}
`;
const lens = $('.tb-lens', nav);
const tabsEl = $('.tb-tabs', nav);
const acc = $('.tb-accessory', nav);
const miniTab = $('.tb-mini-tab', nav);
const miniAcc = $('.tb-mini-acc', nav);
let current = 'now';
let minimized = false;
let accSituation = null;
function placeLens() {
const idx = TABS.findIndex((t) => t.id === current);
const w = (tabsEl.clientWidth - 8) / TABS.length;
lens.style.width = `${w}px`;
lens.style.transform = `translateX(${idx * w}px)`;
}
function select(id, { silent = false } = {}) {
current = id;
$$('.tb-tab', nav).forEach((b) =>
b.setAttribute('aria-selected', String(b.dataset.tab === id)),
);
const t = TABS.find((x) => x.id === id);
miniTab.innerHTML = icon(t.icon);
placeLens();
if (!silent) setMinimized(false);
}
function setMinimized(v) {
if (v === minimized) return;
minimized = v;
nav.classList.toggle('min', v);
$('.tb-full', nav).toggleAttribute('inert', v);
$('.tb-mini', nav).setAttribute('aria-hidden', String(!v));
$$('.tb-mini button', nav).forEach((b) => (b.tabIndex = v ? 0 : -1));
}
function accessoryHtml(s) {
const m = s.metrics?.[0];
return `${icon(FAMILY_ICON[s.family] || 'now')}
${esc(s.title)}
${esc(s.place || '')} ยท ${esc(ago(s.updatedAt))}
${m ? `${esc(fmtNum(m.value))}${m.unit ? ` ${esc(m.unit)}` : ''}` : ''}`;
}
function setAccessory(s) {
accSituation = s;
acc.hidden = !s;
miniAcc.hidden = !s;
nav.classList.toggle('has-accessory', !!s);
document.documentElement.classList.toggle('has-accessory', !!s);
if (!s) return;
acc.dataset.family = s.family;
miniAcc.dataset.family = s.family;
acc.innerHTML = accessoryHtml(s);
miniAcc.innerHTML = accessoryHtml(s);
acc.setAttribute('aria-label', `Following ${s.title}`);
}
nav.addEventListener('click', (e) => {
const tab = e.target.closest('.tb-tab');
if (tab) {
if (tab.dataset.tab !== current) haptic('tick');
onSelect(tab.dataset.tab, { reselect: tab.dataset.tab === current });
return;
}
if (e.target.closest('.tb-search')) return onSearch();
if (e.target.closest('.tb-accessory, .tb-mini-acc'))
return accSituation && onAccessory(accSituation);
if (e.target.closest('.tb-mini-tab')) setMinimized(false);
});
// Arrow-key roving focus across the tablist (ARIA tabs pattern).
tabsEl.addEventListener('keydown', (e) => {
if (!['ArrowLeft', 'ArrowRight'].includes(e.key)) return;
const tabs = $$('.tb-tab', nav);
const i = tabs.indexOf(document.activeElement);
const n =
tabs[(i + (e.key === 'ArrowRight' ? 1 : -1) + tabs.length) % tabs.length];
n.focus();
});
new ResizeObserver(placeLens).observe(tabsEl);
/** Minimise while scrolling down; restore on scroll up or when scrolling stops. */
function bindScroll(scroller) {
let lastY = scroller.scrollTop;
let acc = 0;
let idle = 0;
scroller.addEventListener(
'scroll',
() => {
const y = scroller.scrollTop;
const dy = y - lastY;
lastY = y;
acc = Math.sign(dy) === Math.sign(acc) ? acc + dy : dy;
if (y < 40) setMinimized(false);
else if (acc > 28) setMinimized(true);
else if (acc < -18) setMinimized(false);
clearTimeout(idle);
idle = setTimeout(() => setMinimized(false), 1400);
},
{ passive: true },
);
}
return {
select,
setMinimized,
setAccessory,
bindScroll,
get current() {
return current;
},
};
}