Skip to content

Instantly share code, notes, and snippets.

@tkh44
tkh44 / ModalGallery.jsx
Last active September 13, 2022 01:30
Fake shared element transitions. The height/width on lines 97 & 98 need to be calculated, not static values.
import React from 'react'
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom'
import { Motion } from 'data-driven-motion'
const STIFF_SPRING = { stiffness: 420, damping: 42, precision: 0.1 }
const BOUNCY_SPRING = { stiffness: 180, damping: 21, precision: 0.1 }
const IMAGES = [
{ id: 0, title: 'Dark Orchid', color: 'DarkOrchid' },
{ id: 1, title: 'Lime Green', color: 'LimeGreen' },
@tkh44
tkh44 / configw.js
Last active March 8, 2017 17:54
Found this on a react-motion issue. It will create a spring values based on duration for react-motion's spring.
// tl;dr ...set the duration of a spring animation
export const configw = (sec, overdamping) => {
const s = overdamping <= 0
? 1 - overdamping
: 1 / Math.sqrt(1 + Math.pow(2 * Math.PI / Math.log(1 / (overdamping * overdamping)), 2))
const ks = (2 * Math.PI / sec) / Math.max(Math.sqrt(1 - s * s), 0.5)
const c = 2 * ks * s
return [ks * ks, c]
@tkh44
tkh44 / Animation.jsx
Last active April 6, 2025 09:18
react-router v4 animated with data-driven-motion
import React from 'react'
import { BrowserRouter as Router, Route, Link, Redirect, matchPath } from 'react-router-dom'
import { Motion } from 'data-driven-motion' // https://github.com/tkh44/data-driven-motion
const WOBBLY_SPRING = { stiffness: 200, damping: 15, precision: 0.1 }
const AnimationExample = () => (
<Router>
<div>
<ul>
@tkh44
tkh44 / Eccentric.js
Last active February 27, 2017 23:26
Attempting to simplify react-motion's TransitionMotion component api
import { cloneElement, createElement as h, PropTypes, PureComponent } from 'react'
import { TransitionMotion } from 'react-motion'
export default class Eccentric extends PureComponent {
static defaultProps = {
component: h('div'),
data: []
};
static propTypes = {
@tkh44
tkh44 / Confirm.js
Last active September 13, 2022 01:28
Simple confirm prompt component. Might be handy for someone else. Swap out the Button component for your own use. ClickOutside can be found here https://github.com/tj/react-click-outside
import { PureComponent, createElement as h, DOM } from 'react'
import cn from 'classnames'
import { StyleSheet, css } from 'aphrodite/no-important'
import { colors, font } from 'style'
import ClickOutside from 'ClickOutside'
import Button from 'ui/Button'
const { div, p } = DOM
export default class Confirm extends PureComponent {
@tkh44
tkh44 / _aspect-ratio.scss
Created January 10, 2017 20:32
sass mixin for setting aspect ratio
@mixin aspect-ratio($width, $height) {
position: relative;
&:before {
display: block;
content: " ";
width: 100%;
padding-top: ($height / $width) * 100%;
}
@tkh44
tkh44 / Recompose-example-basic.js
Last active January 5, 2017 18:52
Basic example of using recompose's branch function
// https://jsfiddle.net/tkh44/ub8s4deb/5/
const h = React.createElement
const { branch, renderComponent } = Recompose
const Box = (props) => {
return h('pre', { style: { color: props.color, background: 'white', padding: 4 } },
`props: ${JSON.stringify(props, null, 2)}`
)
}
@tkh44
tkh44 / Home.js
Last active September 13, 2022 01:32
Example of using branch to conditionally render components based on auth state
import authDoor from 'hoc/auth-door'
import Loadable from 'react-loadable';
import Loading from './Loading'
const Dashboard = Loadable({
loader: () => import('pages/Dashboard'),
LoadingComponent: Loading,
// optional options...
delay: 200,
serverSideRequirePath: path.join(__dirname, 'pages/Dashboard'),
@tkh44
tkh44 / NesStatus.js
Last active November 30, 2016 23:27
Simple status bar for showing web socket connection state
import { Component, DOM } from 'react'
import { connect } from 'react-redux'
import { colors } from 'style'
const { div } = DOM
const mapStateToProps = (state) => {
const { connected, connecting } = state.nes
return { connected, connecting }
}
@tkh44
tkh44 / api.js
Last active November 4, 2016 19:36
Quickly create api actions to be consumed by some middleware to make requests
export const createApiAction = (actionType, request) => {
const actionHandler = (...args) => {
return (dispatch, getState) => {
const { auth } = getState()
const ticket = auth.user || auth.app
return dispatch({
type: actionType,
request: typeof request === 'function' ? request(...args) : request,
meta: {