This file contains hidden or 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
| /* | |
| I frequently need to track the user’s mouse position, which means re-rendering 60+ times a second when they move the mouse. | |
| Typically, I only care about the mouse position when it’s near a particular element (eg. the "Like" button tilting at the cursor when it’s nearby). This hook allows us to track the mouse position, but only re-render when the mouse is near the element. The global event handler still fires on every mousemove, but we skip the state update if it’s not near the element. | |
| When the cursor is not near the element, x/y are both set to `null`. | |
| */ | |
| import * as React from 'react'; | |
| import { getDistanceBetweenPoints, clamp } from '@utils'; |
This file contains hidden or 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
| function findBreakoutElem(rootElem = document.body) { | |
| function checkElemWidth(elem) { | |
| if (elem.clientWidth > window.outerWidth) { | |
| console.log("The following element has a larger width than the window's outer width"); | |
| console.log(elem); | |
| console.log('<-------------------------------------------------------------------->'); | |
| } else if (elem.scrollWidth > window.outerWidth) { | |
| console.log("The following element has a larger width than the window's scroll width"); | |
| console.log(elem); | |
| console.log('<-------------------------------------------------------------------->'); |
This file contains hidden or 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
| /** | |
| `useChangeLog` - dev-mode helper hook to let you | |
| know why a memoized component re-rendered! | |
| Usage example: | |
| const YourComponent = React.memo((props) => { | |
| // Just drop this fella into your memo component's body. | |
| useChangeLog(props); |
This file contains hidden or 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
| // @flow | |
| // Reimplementation of the jQuery plugin "FitText" | |
| // https://github.com/davatron5000/FitText.js/blob/master/jquery.fittext.js | |
| import React, { PureComponent } from 'react'; | |
| type Props = { | |
| compressor: number, | |
| children: React$Node, | |
| }; |
This file contains hidden or 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 casetteStatusReducer = constructReducer('idle', () => ({ | |
| [VIEW_CASETTES]: 'selecting', | |
| [EJECT_CASETTE]: 'idle', | |
| [HIDE_CASETTES]: 'idle', | |
| [SELECT_CASETTE]: 'loaded', | |
| })); | |
| const selectedReducer = constructReducer(null, (state, action) => ({ | |
| [SELECT_CASETTE]: action.id, | |
| })); |
This file contains hidden or 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 noop = function noop() {}; | |
| export default function constructReducer(initialState, caseCreator = noop) { | |
| // Because this function creates a reducer, it needs to return a reducer. | |
| return (state = initialState, action) => { | |
| // caseCreator is a function that returns an object, representing all | |
| // of our former cases in the switch statement. It's a function so that | |
| // the cases have access to the state and the action. | |
| const cases = caseCreator(state, action); | |
This file contains hidden or 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
| function statusReducer(state = 'idle', action) { | |
| switch (action.type) { | |
| case VIEW_CASETTES: | |
| return 'selecting'; | |
| case EJECT_CASETTE: | |
| case HIDE_CASETTES: | |
| return 'idle'; | |
| case SELECT_CASETTE: | |
| return 'loaded'; | |
| default: |
This file contains hidden or 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 initialState = { | |
| casettes: {}, | |
| selectedCasette: null, | |
| casetteStatus: 'idle', // One of 'idle', 'selecting', 'loaded' | |
| casettePageNumber: 0, | |
| casettePageLimit: 3, | |
| }; | |
| export default function reducer(state = initialState, action) { | |
| switch (action.type) { |
This file contains hidden or 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 flipMatrix = matrix => ( | |
| matrix[0].map((column, index) => ( | |
| matrix.map(row => row[index]) | |
| )) | |
| ); | |
| const rotateMatrixCounterClockwise = matrix => ( | |
| flipMatrix(matrix).reverse() | |
| ); |
This file contains hidden or 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 compose = (a, b) => x => a(b(x)); | |
| const reverse = array => [...array].reverse(); | |
| // `get` is a simple accessor function, used for selecting an item in an array. | |
| const get = id => array => array[id]; | |
| // This functional version of map accepts our function first. | |
| const map = (fn, array) => array.map(fn); | |
| // `pluck` allows us to map through a matrix, gathering all the items at a |
NewerOlder