Skip to content

Instantly share code, notes, and snippets.

View LiamChapman's full-sized avatar

Liam Chapman LiamChapman

View GitHub Profile
@LiamChapman
LiamChapman / logExports.js
Created December 16, 2020 09:37
log exports from libraries / dependencies
var myExports = require("my-library-dependency-i-want-to-see");
console.log(Object.keys(myExports));
@LiamChapman
LiamChapman / cachImages.js
Created November 25, 2020 14:32
cache image function
export const cacheImages = async (srcArray) => {
const promises = await srcArray.map((src) => {
return new Promise((resolve, reject) => {
const img = new Image();
img.src = src;
img.onload = resolve();
img.onerror = reject();
});
});
await Promise.all(promises);
// Originally inspired by David Walsh (https://davidwalsh.name/javascript-debounce-function)
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// `wait` milliseconds.
const debounce = (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
@LiamChapman
LiamChapman / dynamic_react_component.tsx
Created November 16, 2020 16:17
dynamic react component
const DynamicComponent:React.FC = ({ ComponentName, ...props}) => <ComponentName {...props} />
@LiamChapman
LiamChapman / interpolate_scroll.js
Created October 17, 2020 17:47
Interpolate values on scroll
// https://www.trysmudford.com/blog/linear-interpolation/
// ^ fantastic guide from here!!
const lerp = (x, y, a) => x * (1 - a) + y * a;
const invlerp = (a, b, v) => clamp((v - a) / (b - a))
const clamp = (v, min = 0, max = 1) => Math.min(max, Math.max(min, v));
const items = [{
scrollStart: 70, // Scroll tracking start point
scrollEnd: 120, // Scroll tracking end point
@LiamChapman
LiamChapman / react-typescript.md
Created August 16, 2020 14:57 — forked from juhaelee/react-typescript.md
React + Typescript Cheatsheet

React + Typescript Cheatsheet

Setup

If you use atom... download & install the following packages:

What are Typescript type definition files? (*.d.ts)

@LiamChapman
LiamChapman / breakpoint_mixin.scss
Created August 22, 2019 10:49
Breakpoint Mixin SCSS
@mixin breakpoint($minWidth) {
@media screen and (min-width: $minWidth) {
@content;
}
}
// @include breakpoint(650px) { width: 50%; }
// etc..
@LiamChapman
LiamChapman / year_dropdown.jsx
Created June 28, 2019 15:20
Years Dropdown
return(
<select name="years" id="years">
{ [...Array(40).keys()].map( (i, k) => i > 0 && <option key={ k } value={ i + 1 } selected={ parseInt(value, 10) === ( i + 1 ) }>{ i + 1 } years</option> ) }
</select>
);
@LiamChapman
LiamChapman / intersect.js
Created March 12, 2019 15:17
Intersection Observer Helper Function
// Polyfill
if (typeof window !== 'undefined') {
require('intersection-observer'); // eslint-disable-line global-require
}
// when elements intersect add css class
export default ({
elements,
threshold,
root = null,
rootMargin = '0px',