Skip to content

Instantly share code, notes, and snippets.

View mattgperry's full-sized avatar

Matt Perry mattgperry

View GitHub Profile
@mattgperry
mattgperry / index.js
Created May 20, 2015 09:04
requirebin sketch
var h = require('virtual-dom/h');
var diff = require('virtual-dom/diff');
var patch = require('virtual-dom/patch');
var createElement = require('virtual-dom/create-element');
var redshift = require('redshift');
var renderAction = redshift.newAction();
// 1: Create a function that declares what the DOM should look like
function render(count) {
return h('div', {
@mattgperry
mattgperry / useAnimationLoop.js
Last active August 8, 2019 17:14
Use Framesync via a render loop
import sync, { cancelSync } from "framesync";
import { useEffect } from "react";
/**
The code inside useAnimationLoop is guaranteed to run once a frame
before Framer Motion/Popmotion/Pose renders
useAnimationLoop(({ delta, timestamp }) => {
// do stuff
})
Hi [recruiter's name],
Thanks for getting in touch. Sorry, but I'm not open to roles at Facebook.
Your company's current stance on political advertising is a conscious choice that's dangerously stoking divisions in society in the name of profit.
Also, while I understand the legal framework that allows them to do so, I disagree with massive profit-making American companies like Facebook coming over here and structuring themselves in a way that allows them to avoid local taxation. These companies then underpay European developers in relation to their west coast counterparts. Given our government's aptitude for taxing salaried individuals over corporations, this is another two methods your company uses to withhold the profits we generate for you.
Please exclude me from your searches until your company discovers its moral compass
Thanks
const Component = () => {
const frameCount = useRef(0)
const prevValues = useRef()
return <motion.div transformValues={values => {
frameCount.current++
const valuesToReturn = isEven(frameCount.current) ? prevValues.current : values
prevValues.current = values
@mattgperry
mattgperry / drag-test-utils.tsx
Created March 31, 2020 07:50
Framer Motion drag utils
import * as React from "react"
import sync from "framesync"
import { MotionPlugins } from "../../motion/context/MotionPluginContext"
import { act } from "react-dom/test-utils"
import { fireEvent } from "@testing-library/dom"
export type Point = {
x: number
y: number
}
@mattgperry
mattgperry / test.ts
Created March 31, 2020 07:51
Framer Motion example test
test("drag handlers aren't frozen at drag session start", async () => {
let count = 0
const onDragEnd = deferred()
const Component = () => {
const [increment, setIncrement] = useState(1)
return (
<MockDrag>
<motion.div
drag
onDragStart={() => {
@mattgperry
mattgperry / useMotionTemplate.ts
Last active May 14, 2020 13:58
useMotionTemplate: Combine multiple motion values into a new one using a template literal
import { useEffect } from "react";
import sync from "framesync";
import { useMotionValue, MotionValue } from "framer-motion";
/**
* Combine multiple motion values into a new one using a template literal.
*
* Updates in source motion values are batched to once per frame.
*
* ```javascript
@mattgperry
mattgperry / gist:c4d23752a0fae7888596c4ff6d92733a
Created August 10, 2020 13:36
useTransform tuple types
class MotionValue<T> {
constructor(public current: T) {}
}
type UnwrapMotionValueArray<V extends MotionValue<any>[]> = { [K in keyof V ]: V[K] extends MotionValue<infer T> ? T : never }
function unwrap<T extends MotionValue<any>[]>(value: [...T]): UnwrapMotionValueArray<typeof value> {
return value.map(v => v.current) as [...UnwrapMotionValueArray<T>]
}
@mattgperry
mattgperry / pointerEvents.tsx
Created August 26, 2020 15:19
Disable pointer-events at low opacity in Framer Motion
import { motion, useMotionValue, useTransform } from "framer-motion"
export function Overlay({ isVisible }) {
const opacity = useMotionValue(0)
const pointerEvents = useTransform(
opacity,
latest => latest < 0.5 ? "none" : "auto"
)
return (
import { useEffect } from "react";
function handleSpace(event: KeyboardEvent) {
if (event.key === " ") {
event.preventDefault();
const delta = event.shiftKey ? -window.innerHeight : window.innerHeight;
window.scrollTo(0, window.scrollY + delta);
}
}