Skip to content

Instantly share code, notes, and snippets.

View souporserious's full-sized avatar

Travis Arnold souporserious

View GitHub Profile
import React from 'react'
import PropTypes from 'prop-types'
import Box from '../Box'
import { cloneChildren, joinChildren } from '../utils'
const BASELINE = 'baseline'
const CENTER = 'center'
const END = 'end'
const FILL = 'fill'
@souporserious
souporserious / lockScroll.js
Last active September 28, 2018 20:57
Locks scrollbars starting from an element
/**
* Locks scrollbars starting from an element
* @param {HTMLElement} node - the element to start locking scroll at
* @return {Function} unlockScroll - unlocks all scroll elements/parents
*/
function lockScroll(node, lockedNodes = []) {
const viewport = getClosestViewport(node)
if (viewport && !viewport.__locked) {
const { overflow, paddingRight } = getComputedStyle(viewport)
const previousStyles = {
@souporserious
souporserious / lockScroll.js
Created October 1, 2018 23:19
Locks all scrollbars by disabling mousewheel
/**
* Lock all scrollbars by disabling mousewheel and locking scrollbars in position
* Optionally provide an element to allow it to scroll when hovered
*/
const lockedScrolls = []
function lockScroll(node) {
const scrollCoords = new WeakMap()
const preventDefault = event => event.preventDefault()
const mouseOver = () => window.removeEventListener('wheel', preventDefault)
const mouseOut = () => window.addEventListener('wheel', preventDefault)
@souporserious
souporserious / getTextWidth.js
Created January 21, 2019 20:59
Get the width for a single line of text
/**
* Get the width for a single line of text
* @param {String} text - the string of text to be measured
* @param {Object|HTMLElement} font - optional font styles or node to pull font styles from
* @return {Number} the width of the text passed in
*/
const expando = 'text-width' + new Date().getTime()
const canvas =
typeof document === 'undefined' ? null : document.createElement('canvas')
const context = canvas && canvas.getContext('2d')
@souporserious
souporserious / FocusGroup.jsx
Last active November 13, 2019 05:19
Manage multiple React elements as if they were in one focus group.
import React, { Component, createContext } from 'react'
const SetRefContext = createContext()
function isOutsideElement({ source, target }) {
return source !== target && !source.contains(target)
}
class FocusGroup extends Component {
static defaultProps = {
@souporserious
souporserious / window-zoom.js
Created October 15, 2019 22:00
Checks if window has been zoomed.
// modified from https://stackoverflow.com/a/52008131/1461204
const zoomEvent = new Event('zoom')
let currentRatio = window.devicePixelRatio
function checkZooming() {
if (currentRatio !== window.devicePixelRatio) {
window.dispatchEvent(zoomEvent)
}
}
export function useZoom({ onZoom }) {
useEffect(() => {
let lastRatio = window.devicePixelRatio
function checkZooming(event) {
if (lastRatio !== window.devicePixelRatio) {
if (onZoom) {
onZoom(event)
}
lastRatio = window.devicePixelRatio
}
@souporserious
souporserious / machine.js
Last active October 28, 2019 05:55
Generated by XState Viz: https://xstate.js.org/viz
const highlightAction = amount =>
assign({
highlightedIndex: (context, event) =>
context.highlightedIndex === null ? 0 : context.highlightedIndex + amount
});
const highlightMachine = Machine({
id: "highlight",
initial: "idle",
context: {
highlightedIndex: null
@souporserious
souporserious / ViewPager.jsx
Last active November 22, 2019 23:10
ViewPager built with react-measure v4 iteration
import React, { useRef } from 'react'
import { useMeasure } from 'react-measure'
function getViewTargets(node) {
const targets = []
if (node) {
let target = 0
for (let index = 0; index < node.children.length; index++) {
const childNode = node.children[index]
const bounds = childNode.getBoundingClientRect()
@souporserious
souporserious / use-measure.js
Created November 22, 2019 20:25
react-measure v4 iteration
export function useMeasure(
measureRef,
getDimensions = getRect,
shouldUpdate = (prev, next) => !shallowEqual(prev, next)
) {
const [dimensions, setDimensions] = useState(
getDimensions(measureRef.current)
)
const mutationObserver = useRef(null)
const resizeObserver = useRef(null)