This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
/** | |
* navigator.onLine behaves a little differently across browsers | |
* (https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine#Specification). | |
* For bullet-proof detection you can add to this hook using some of the suggestions | |
* mentioned here https://www.html5rocks.com/en/mobile/workingoffthegrid/. | |
* | |
* Usage: | |
* const isOnline = useNetworkConnectivity(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<details class="c-menu" data-js-menu> | |
<summary id="menu-control" class="c-menu__control" data-js-menu-control> | |
Actions | |
</summary> | |
<ul id="menu" class="c-menu__list" data-js-menu-list> | |
<li> | |
<a href="#dowload">Download</a> | |
</li> | |
<li> | |
<a href="#copy">Copy to Clipboard</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useContext, useEffect, useRef } from 'react'; | |
import PropTypes from 'prop-types'; | |
import TabContext from './TabContext'; | |
import getAttributeProps from './getAttributeProps'; | |
const propTypes = { | |
children: PropTypes.oneOfType([ | |
PropTypes.arrayOf(PropTypes.node), | |
PropTypes.node | |
]).isRequired, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import debounce from "lodash/debounce"; | |
const getPageHeight = () => | |
Math.max( | |
document.body.scrollHeight, | |
document.body.offsetHeight, | |
document.documentElement.clientHeight, | |
document.documentElement.scrollHeight, | |
document.documentElement.offsetHeight | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = { | |
accordionSelector: "[data-accordion]", | |
headingSelector: "[data-accordion-heading]", | |
buttonSelector: "[data-accordion-button]" | |
}; | |
const setup = accordion => { | |
const headings = [...accordion.querySelectorAll(config.headingSelector)]; | |
if (!headings || headings.length === 0) return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = { | |
selector: '[data-observe]', | |
isVisibleClass: 'is-visible', | |
observerOptions: { | |
threshold: 0, | |
rootId: null, | |
rootMargin: '0px 0px 0px 0px', | |
once: true | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const form = document.querySelector('form'); | |
const config = { | |
errorAttribute: 'aria-invalid', | |
errorClass: 'error' | |
}; | |
// Helpers | |
const isFormInput = target => { | |
const { nodeName } = target; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const preventOrphanWord = (string) => { | |
const words = string.trim().split(' '); | |
// Only proceed if we've got at least 3 words | |
if (words.length < 3) { | |
return string; | |
} | |
const [nextToLastWord, lastWord] = words.slice(-2); | |
const precedingWords = words.slice(0, words.length - 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ('serviceWorker' in navigator) { | |
window.addEventListener('load', function() { | |
navigator.serviceWorker | |
.register('path/to/service-worker.js') | |
.then(registration => { | |
console.log('SW registered: ', registration); | |
}) | |
.catch(registrationError => { | |
console.log('SW registration failed: ', registrationError); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {String} key | |
* @param {Any} value | |
* @param {Number} expirationDays | |
* @returns {void} | |
*/ | |
export const saveToLocalStorage = ({ key, value, expirationDays }) => { | |
// Try for localStorage support... | |
try { | |
// Convert days to milliseconds (ms) |