89 lines
3.0 KiB
JavaScript
89 lines
3.0 KiB
JavaScript
function createMobileUpdateDetailNavigation(options) {
|
|
const buttons = options.buttons || {};
|
|
const targets = options.targets || {};
|
|
const listeners = new Map();
|
|
const prefersReducedMotion = options.prefersReducedMotion || (() => false);
|
|
const targetNames = new Map(Object.entries(targets).map(([name, target]) => [target, name]));
|
|
let observer = null;
|
|
|
|
function select(name) {
|
|
Object.entries(buttons).forEach(([key, button]) => {
|
|
if (!button) return;
|
|
if (key === name) button.setAttribute('aria-current', 'location');
|
|
else button.removeAttribute('aria-current');
|
|
});
|
|
}
|
|
|
|
function navigate(name, navigationOptions = {}) {
|
|
if (name === 'activity') {
|
|
if (options.jumpToNewActivity) options.jumpToNewActivity();
|
|
select(name);
|
|
return true;
|
|
}
|
|
const target = targets[name];
|
|
if (!target) return false;
|
|
if (name === 'context') target.open = true;
|
|
target.scrollIntoView({
|
|
block:'start',
|
|
behavior:prefersReducedMotion() ? 'auto' : 'smooth',
|
|
});
|
|
if (name === 'reply' && options.replyComposer && navigationOptions.focus !== false) {
|
|
options.replyComposer.focus({ preventScroll:true });
|
|
}
|
|
select(name);
|
|
return true;
|
|
}
|
|
|
|
function reset() {
|
|
if (targets.context) targets.context.open = false;
|
|
select('activity');
|
|
}
|
|
|
|
return {
|
|
start() {
|
|
Object.entries(buttons).forEach(([name, button]) => {
|
|
if (!button || listeners.has(button)) return;
|
|
const listener = event => {
|
|
event.preventDefault();
|
|
navigate(name);
|
|
if (options.onSectionChange) options.onSectionChange(name, { replace:false });
|
|
};
|
|
listeners.set(button, listener);
|
|
button.addEventListener('click', listener);
|
|
});
|
|
reset();
|
|
const observe = options.observe || ((handler, observedTargets) => {
|
|
if (typeof IntersectionObserver === 'undefined') return null;
|
|
const instance = new IntersectionObserver(handler, {
|
|
root:options.root || null,
|
|
rootMargin:'-20% 0px -60% 0px',
|
|
threshold:[0, 0.25, 0.5, 0.75, 1],
|
|
});
|
|
observedTargets.forEach(target => instance.observe(target));
|
|
return instance;
|
|
});
|
|
observer = observe(entries => {
|
|
const visible = entries
|
|
.filter(entry => entry.isIntersecting && targetNames.has(entry.target))
|
|
.sort((left, right) => right.intersectionRatio - left.intersectionRatio)[0];
|
|
if (visible) {
|
|
const name = targetNames.get(visible.target);
|
|
select(name);
|
|
if (options.onSectionChange) options.onSectionChange(name, { replace:true });
|
|
}
|
|
}, Array.from(targetNames.keys()).filter(Boolean));
|
|
},
|
|
stop() {
|
|
listeners.forEach((listener, button) => button.removeEventListener('click', listener));
|
|
listeners.clear();
|
|
if (observer) observer.disconnect();
|
|
observer = null;
|
|
},
|
|
navigate,
|
|
reset,
|
|
select,
|
|
};
|
|
}
|
|
|
|
if (typeof module !== 'undefined') module.exports = createMobileUpdateDetailNavigation;
|