diff --git a/js/anchors-indicator/index.js b/js/anchors-indicator/index.js new file mode 100644 index 0000000000..d36ca2a72e --- /dev/null +++ b/js/anchors-indicator/index.js @@ -0,0 +1,19 @@ +import MenuSpy from 'menuspy' +const COMPONENT_SELECTOR = '[data-anchors-indicator]' +const ACTIVE_CLASS = 'data-active-class' + +const OPTIONS = { + enableLocationHash: false +} + +export default function () { + const components = document.querySelectorAll(COMPONENT_SELECTOR) + + components.forEach(component => { + const activeClass = component.getAttribute(ACTIVE_CLASS) + + new MenuSpy(component, Object.assign({ + activeClass: activeClass + }, OPTIONS)) + }) +} diff --git a/js/dropdown-menu/index.js b/js/dropdown-menu/index.js index f01d38f07f..cf57f2a185 100644 --- a/js/dropdown-menu/index.js +++ b/js/dropdown-menu/index.js @@ -4,12 +4,14 @@ const DROPDOWN_HEAD_INNER = '[data-ref="dropdown-menu[head-inner]"]' const DROPDOWN_BODY = '[data-ref="dropdown-menu[body]"]' const DROPDOWN_LINK = '[data-ref="dropdown-menu[link]"]' const ACTIVE_CLASS_ATTR = 'data-active-class' +const ANCHOR = '[data-ref="dropdown-menu[item]"]' export default function () { const components = document.querySelectorAll(COMPONENT_SELECTOR) components.forEach(component => { const dropdownHeadInner = component.querySelector(DROPDOWN_HEAD_INNER) + const itemActiveClass = component.getAttribute(ACTIVE_CLASS_ATTR) const dropdownHead = component.querySelector(DROPDOWN_HEAD) const dropdownHeadActive = dropdownHead.getAttribute(ACTIVE_CLASS_ATTR) @@ -24,13 +26,23 @@ export default function () { dropdownBody.classList.toggle(dropdownBodyActive) }) + const updateValue = (dest) => { + dropdownHeadInner.innerHTML = dest.innerHTML + } + links.forEach(link => { link.addEventListener('click', () => { - dropdownHeadInner.innerHTML = link.innerHTML + updateValue(link) dropdownHead.classList.remove(dropdownHeadActive) dropdownBody.classList.remove(dropdownBodyActive) }) }) + + window.addEventListener('scroll', () => { + const currentActiveIndicator = document.querySelector(`${ANCHOR}.${itemActiveClass} a`) + + updateValue(currentActiveIndicator) + }) }) } diff --git a/js/index.js b/js/index.js index b62fbc7d85..fede0e5dbc 100644 --- a/js/index.js +++ b/js/index.js @@ -6,6 +6,7 @@ import searchNavbar from './search-navbar' import menuMobile from './menu-mobile' import headingsAnchors from './headings-anchors' import dropdownMenu from './dropdown-menu' +import anchorsIndicator from './anchors-indicator' feedback() accordion() @@ -13,3 +14,4 @@ searchNavbar() menuMobile() headingsAnchors() dropdownMenu() +anchorsIndicator() diff --git a/package.json b/package.json index ba7be55dd1..d3e972e417 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,8 @@ "concurrently": "4.1.2", "webpack": "4.40.2", "webpack-cli": "3.3.9" + }, + "dependencies": { + "menuspy": "1.3.0" } } diff --git a/src/_includes/sidebar/destinations-menu-mobile.html b/src/_includes/sidebar/destinations-menu-mobile.html index 0a317a3122..a716ad7a40 100644 --- a/src/_includes/sidebar/destinations-menu-mobile.html +++ b/src/_includes/sidebar/destinations-menu-mobile.html @@ -1,6 +1,6 @@ {% assign destination_categories = site.data.catalog.destinations.destinations | group_by:"categories.primary" | sort: "name" %} -