Skip to content

Instantly share code, notes, and snippets.

/**
* @module holy-albatross
* @description
* A custom element for switching between horizontal and vertical layouts
* using Flexbox, where the switch property defines the minimum container width
* for the horizontal layout
* @property {string} switch A CSS width value
* @property {string} margin A CSS margin value
*/
// set heading level based on the closest heading before the node
const setLevel = (node, nodeSelector) => {
// Get all relevant nodes and reverse the array (destructive)
let allNodes = [...document.querySelectorAll(`h1, h2, h3, h4, h5, h6, ${nodeSelector}`)].reverse();
// Truncate the array to make the target node the first item
let index = allNodes.indexOf(allNodes.find(n => n.contains(node)));
let truncated = allNodes.slice(index);
// Find the next node thats a heading