diff --git a/src/_includes/partials/header.njk b/src/_includes/partials/header.njk index 12dd156..4a24224 100644 --- a/src/_includes/partials/header.njk +++ b/src/_includes/partials/header.njk @@ -7,7 +7,7 @@
{% include "partials/uio.njk" %} - {% localeLink locale, translationKey, category, collections %} + {% include 'partials/language.njk' %}
diff --git a/src/_includes/partials/language.njk b/src/_includes/partials/language.njk new file mode 100644 index 0000000..b4599fb --- /dev/null +++ b/src/_includes/partials/language.njk @@ -0,0 +1,6 @@ + diff --git a/src/assets/scripts/app.js b/src/assets/scripts/app.js index 6982b5c..c2248ee 100644 --- a/src/assets/scripts/app.js +++ b/src/assets/scripts/app.js @@ -21,3 +21,49 @@ jQuery("#menu").menu({ } } }); + +const toggleExpansion = (element, state) => { + let isExpanded = element.getAttribute("aria-expanded") === "true" ? true : false; + state = typeof(state) !== "undefined" ? state : !isExpanded; + + element.setAttribute("aria-expanded", state ? "true" : "false"); + return state; +}; + +const menu = (container, button = "button") => { + let menu = document.querySelector(container); + + if (!menu) { + throw new Error(`No element found for container selector: ${container}`); + } + + let btn = menu.querySelector(button); + + if (!btn) { + throw new Error(`No element found for button selector: ${button}`); + } + + // Close the menu when focus is moved away from the menu + menu.addEventListener("focusout", (event) => { + if (!menu.contains(event.relatedTarget)) { + toggleExpansion(btn, false); + } + }); + + // Close the menu when the "Escape" key is pressed and the menu has focus. Shifts focus back to the button. + menu.addEventListener("keyup", (event) => { + if (event.code === "Escape") { + toggleExpansion(btn, false); + btn.focus(); + } + }); + + // Toggle expansion of menu when button is clicked + btn.addEventListener("click", () => { + toggleExpansion(btn); + }); +}; + +menu(".idg-language-picker"); + + diff --git a/src/assets/styles/app.styl b/src/assets/styles/app.styl index 67a6f64..897e1ea 100644 --- a/src/assets/styles/app.styl +++ b/src/assets/styles/app.styl @@ -135,15 +135,48 @@ a:hover .idg-logo { } } -.idg-locale { +.idg-language-picker { width: 100%; display: flex; - justify-content: flex-end; + flex-direction: column; + align-items: flex-end; padding-block-start: 1rem; padding-inline: 2rem; } -.idg-locale a { +.idg-language-picker button[aria-expanded="false"]::after { + content: "▼"; +} + +.idg-language-picker button::after { + padding-inline-start: 0.5rem; + content: "▲"; +} + +.idg-language-picker button[aria-expanded="false"]+ul { + display: none; +} + +.idg-language-picker button+ul { + position: absolute; + margin-block-start: 3.3rem; + background-color: grey; + z-index: 2; + list-style: none; +} + +.idg-language-picker li { + padding-inline: 1rem; + text-align: end; +} + +.idg-language-picker li a[aria-current]::before { + color: #fff; + content: "✔"; + padding-inline-end: 0.5rem; + } + +.idg-language-picker a { color: #fff; } diff --git a/src/locales/messages.js b/src/locales/messages.js index 534c8e6..32463e3 100644 --- a/src/locales/messages.js +++ b/src/locales/messages.js @@ -10,6 +10,7 @@ _i("Hosted with ${netlify}.") _i("This documentation is licensed under a ${license}.") _("Skip to Content") _i("Home: ${siteName}") +_("Language") _("Reset") _("activities") _("insights") @@ -21,5 +22,4 @@ _("https://creativecommons.org/licenses/by/3.0/") _("Inclusive Design Research Centre") _("https://idrc.ocad.ca/") _("OCAD University") -_("https://www.ocadu.ca/") -_("Language") \ No newline at end of file +_("https://www.ocadu.ca/") \ No newline at end of file diff --git a/src/shortcodes/generateLocaleLinks.js b/src/shortcodes/generateLocaleLinks.js index 898c46f..8c98076 100644 --- a/src/shortcodes/generateLocaleLinks.js +++ b/src/shortcodes/generateLocaleLinks.js @@ -1,23 +1,26 @@ "use strict"; const config = require("../_data/config.json"); -const i18n = require("eleventy-plugin-i18n-gettext"); module.exports = (locale, translationKey, category, collection) => { let output = ""; - for (let language in config.languages) { - if (locale && locale !== language) { + + if (locale) { + for (let language in config.languages) { let langProps = config.languages[language]; - if (category && collection) { + let attrs = locale === language ? "aria-current=\"page\"" : ""; + let url = language === config.defaultLanguage ? "/" : `/${langProps.slug}`; + + if (category && collection && translationKey) { let matchedPage = collection[`${category}_${language}`].find(page => page.fileSlug === translationKey); if (matchedPage) { - output += `${langProps.name}`; + url = matchedPage.url; } - } else { - output += `${langProps.name}`; } + + output += `
  • ${langProps.name}
  • `; } } - return output ? `` : output; + return output; };