Skip to content

Instantly share code, notes, and snippets.

View ajsmth's full-sized avatar

andy ajsmth

View GitHub Profile
@ajsmth
ajsmth / scroll-listener.tsx
Last active February 26, 2020 19:59
Adding a scroll listener
function PlaylistProfile() {
// this will track the scroll value of the Animated.ScrollView
// use a ref so it doesn't get reset on rerenders
const scrollY = React.useRef(new Animated.Value(0));
// standard boilerplate for listening to scroll events
// useNativeDriver means the scroll value will be updated on the native thread (more efficient)
// this limits what you can do with the Animated.Value - style properties are restricted to transform and opacity
const handleScroll = Animated.event(
[{nativeEvent: {contentOffset: {y: scrollY.current}}}],
@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>
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 / 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 />
@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 / 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-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-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-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(() => ({