Files
laysite/public/assets/site.js
T

71 lines
2.7 KiB
JavaScript

'use strict';
jQuery(function ($) {
const $toggle = $('.menu-toggle');
const $nav = $('.primary-nav');
if ($toggle.length && $nav.length) {
$toggle.prop('hidden', false);
$nav.addClass('has-toggle');
const setMenu = (open) => {
$toggle.attr('aria-expanded', String(open));
$nav.toggleClass('is-open', open);
};
$toggle.on('click', () => setMenu($toggle.attr('aria-expanded') !== 'true'));
$nav.on('click', 'a', () => setMenu(false));
$(document).on('keydown', (event) => {
if (event.key === 'Escape' && $toggle.attr('aria-expanded') === 'true') {
setMenu(false);
$toggle.trigger('focus');
}
});
}
$('[data-article-tabs]').each(function () {
const $feed = $(this);
const $list = $feed.children('[data-tab-list]');
const $panels = $feed.children('[data-tab-panel]');
const $tabs = $list.children('[role="tab"]');
function selectTab(index, focus = false) {
$tabs.each(function (number) {
$(this).attr({'aria-selected': String(index === number), tabindex: index === number ? '0' : '-1'});
});
$panels.prop('hidden', true).eq(index).prop('hidden', false);
if (focus) $tabs.eq(index).trigger('focus');
}
$panels.each(function (index) {
$(this).attr({role: 'tabpanel', 'aria-labelledby': $tabs.eq(index).attr('id'), tabindex: '0'});
}).children('[data-tab-heading]').prop('hidden', true);
$list.prop('hidden', false);
selectTab(0);
$tabs.on('click', function () {
selectTab($tabs.index(this));
}).on('keydown', function (event) {
const index = $tabs.index(this);
const last = $tabs.length - 1;
const destinations = {ArrowLeft: index === 0 ? last : index - 1, ArrowRight: index === last ? 0 : index + 1, Home: 0, End: last};
if (!Object.hasOwn(destinations, event.key)) return;
event.preventDefault();
selectTab(destinations[event.key], true);
});
});
if ('IntersectionObserver' in window && !matchMedia('(prefers-reduced-motion: reduce)').matches) {
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
$(entry.target).addClass('moment-active');
observer.unobserve(entry.target);
}
}
}, {threshold: 0.35});
$('[data-moment]').each(function () {
observer.observe(this);
});
}
});