Skip to content

Instantly share code, notes, and snippets.

View ajsmth's full-sized avatar

andy ajsmth

View GitHub Profile
@ajsmth
ajsmth / pager-setup.js
Created August 10, 2019 19:29
Pager Component -- Initial Setup
import React, { useState } from 'react'
// this component will be the focus of this series
// we will extend its functionality considerably throughout
// for now let's just setup the translations...
function Pager({ children, activeIndex }) {
// this will update when the activeIndex changes
// try updating activeIndex in devtools and see how the blue container changes position
@ajsmth
ajsmth / pager-transitions.js
Created August 10, 2019 19:35
Pager with Spring Transitions
import React, { useState, useEffect } from 'react'
import { useSpring, animated, interpolate } from 'react-spring'
// let's add some transitions to the pager component when the activeIndex changes
function Pager({ children, activeIndex }) {
const offset = activeIndex * 100 * -1
// translateX will now represent a spring value that we will animate
// we'll set the initial value to our first offset here:
const [{ translateX }, set] = useSpring(() => ({
@ajsmth
ajsmth / pager-drag-handlers.js
Created August 10, 2019 19:36
Pager Drag Handlers
import React, { useState, useEffect } from 'react'
import { useSpring, animated, interpolate } from 'react-spring'
import { useDrag } from 'react-use-gesture'
// let's handle gestures within our pager component
const PAGE_SIZE = 375
// arbitrary value that will determine if we should transition after dragging
const DRAG_THRESHOLD = Math.floor(PAGE_SIZE * 0.3)
@ajsmth
ajsmth / pager-config-props
Created August 23, 2019 20:39
Configurable Pager
import React, { useState, useEffect } from 'react';
import { useSpring, animated, interpolate } from 'react-spring';
import { useDrag } from 'react-use-gesture';
function Pager({
children,
activeIndex: parentIndex, // rename to parentIndex for simple refactor
onChange: parentOnChange, // rename to parentOnChange for simple refactor
initialIndex = 0, // default to index 0
adjacentChildOffset,
@ajsmth
ajsmth / pager-config-props
Created August 23, 2019 20:39
Configurable Pager
import React, { useState, useEffect } from 'react';
import { useSpring, animated, interpolate } from 'react-spring';
import { useDrag } from 'react-use-gesture';
function Pager({
children,
activeIndex: parentIndex, // rename to parentIndex for simple refactor
onChange: parentOnChange, // rename to parentOnChange for simple refactor
initialIndex = 0, // default to index 0
adjacentChildOffset,
@ajsmth
ajsmth / pager-config-props.tsx
Last active August 26, 2019 23:57
Configurable Pager
import React, { useState, useEffect } from 'react';
import { useSpring, animated, interpolate } from 'react-spring';
import { useDrag } from 'react-use-gesture';
interface PagerProps {
type: "horizontal" | "vertical";
children: any;
activeIndex?: number;
onChange?: (nextIndex: number) => void;
initialIndex?: number;
@ajsmth
ajsmth / pager-performance.js
Created August 10, 2019 21:23
pager-performance
import React, { useState, useEffect } from 'react'
import { useSpring, animated, interpolate } from 'react-spring'
import { useDrag } from 'react-use-gesture'
const PAGE_SIZE = 200
// arbitrary value that will determine if we should transition after dragging
const DRAG_THRESHOLD = Math.floor(PAGE_SIZE * 0.3)
function Pager({
children,
@ajsmth
ajsmth / App.tsx
Created February 26, 2020 19:28
Setting up scroll view containers
import React from 'react';
import {View, Animated, SafeAreaView, TouchableOpacity} from 'react-native';
function PlaylistProfile() {
return (
<SafeAreaView style={{flex: 1}}>
<Header />
<Animated.ScrollView style={{flex: 1}}>
<SearchPlaylists />
function PlaylistProfile() {
// ...
const PLAYLIST_ITEMS_OFFSET = PLAYLIST_HERO_HEIGHT + SEARCH_PLAYLISTS_HEIGHT;
const clampShuffleButton = Animated.add(
// make the button maintain its position during scroll - i.e the center of the window
scrollY.current,
// if we havent scrolled past the hero section, have the shuffle button move up with the scrollview
scrollY.current.interpolate({
@ajsmth
ajsmth / scrollview-offset.tsx
Created February 26, 2020 19:49
Setting the scrollview offset
<Animated.ScrollView
contentOffset={{y: SEARCH_PLAYLISTS_HEIGHT}}
onScroll={handleScroll}
style={{flex: 1}}>
{...}
</Animated.ScrollView>