Skip to content

Commit

Permalink
Merge pull request #45 from fdnd/feature-layout
Browse files Browse the repository at this point in the history
Updated navigation
  • Loading branch information
ju5tu5 authored Jan 26, 2024
2 parents fcc7258 + a7c4c27 commit 67f4ad1
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 175 deletions.
1 change: 1 addition & 0 deletions docs.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export default {
title: 'FDND Documentatie',
language: 'nl',
meta: [{ name: 'color-scheme', content: 'light dark' }],
css: [
'https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;1,300;1,400&display=swap',
'./assets/style/style.css',
Expand Down
3 changes: 1 addition & 2 deletions docs/assets/img/link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions docs/assets/img/message-exclamation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions docs/assets/img/message-plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 74 additions & 5 deletions docs/assets/script/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,78 @@
const settingsButtons = document.querySelectorAll('.settings button')
const storageKey = 'theme-preference'
const theme = { value: getColorPreference() }

settingsButtons.forEach((button) => {
button.addEventListener('click', toggleSettingsButton)
reflectPreference()

document.querySelector('#theme').addEventListener('click', () => {
theme.value = theme.value === 'light' ? 'dark' : 'light'
setPreference()
})

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', ({ matches: isDark }) => {
theme.value = isDark ? 'dark' : 'light'
setPreference()
})

function getColorPreference() {
if (localStorage.getItem(storageKey)) return localStorage.getItem(storageKey)
else return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}

function reflectPreference() {
document.firstElementChild.setAttribute('data-theme', theme.value)
document.querySelector('#theme')?.setAttribute('aria-label', theme.value)
}

function setPreference() {
localStorage.setItem(storageKey, theme.value)
reflectPreference()
}

// Scrolling functions
const paragraphItems = Array.from(document.querySelectorAll('#paragraph-dropdown a'))
const articleParagraphs = paragraphItems.map((item) => document.getElementById(decodeURI(item.hash).replace('#', '')))

const subnavItems = Array.from(document.querySelectorAll('.subnav a'))
const articleItems = subnavItems.map((item) => document.getElementById(decodeURI(item.hash).replace('#', '')))

const delta = 5

let scrolled
let lastItem
let lastParagraph
let lastScrollTop = 0

window.addEventListener('scroll', () => {
scrolled = true
})

function toggleSettingsButton(event) {
event.target.setAttribute('aria-pressed', event.target.getAttribute('aria-pressed') === 'false')
setInterval(() => {
if (scrolled) {
scrollHandler()
scrolled = false
}
}, 200)

function scrollHandler() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop

// set text on paragraph button
const passedParagraph = articleParagraphs.filter((item) => item.offsetParent.offsetTop < scrollTop)
const currentParagraph = passedParagraph[passedParagraph.length - 1]?.id

if (currentParagraph !== undefined && currentParagraph !== lastParagraph) {
document.getElementById('paragraph-button').innerHTML =
document.getElementById(currentParagraph).firstChild.textContent
lastParagraph = currentParagraph
}

// highlight the correct toc item
const passedItems = articleItems.filter((item) => item.offsetParent.offsetTop < scrollTop)
const currentItem = passedItems[passedItems.length - 1]?.id

if (currentItem !== undefined && currentItem !== lastItem) {
document.querySelector(`.subnav a[href$='#${lastItem}']`)?.classList.remove('active')
document.querySelector(`.subnav a[href$='#${currentItem}']`)?.classList.add('active')
lastItem = currentItem
}
}
Loading

0 comments on commit 67f4ad1

Please sign in to comment.