Skip to content

Commit

Permalink
fix stagger speed
Browse files Browse the repository at this point in the history
  • Loading branch information
aholachek committed Jun 24, 2019
1 parent a913d6e commit 6d34691
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/src/StaggeredList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class AnimatedList extends Component {
staggerConfig={{
card: {
reverse: this.state.focused !== null ? true : false,
speed: 0.5
speed: 0
}
}}
decisionData={this.state.focused}
Expand Down
24 changes: 24 additions & 0 deletions src/flip/animateFlippedElements/spring/__tests__/spring.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { normalizeSpeed } from '../index'

describe('normalizeSpeed', () => {
it('if not provided a number, it returns 1.1', () => {
expect(normalizeSpeed(null)).toBe(1.1)
expect(normalizeSpeed(undefined)).toBe(1.1)
expect(normalizeSpeed('string')).toBe(1.1)
})
it('if provided 1, returns 6', () => {
expect(normalizeSpeed(1)).toBe(6)
})

it('if provided 10, returns 6', () => {
expect(normalizeSpeed(10)).toBe(6)
})

it('if provided 0, returns 1', () => {
expect(normalizeSpeed(0)).toBe(1)
})

it('if provided -1, returns 1', () => {
expect(normalizeSpeed(-1)).toBe(1)
})
})
9 changes: 6 additions & 3 deletions src/flip/animateFlippedElements/spring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const createSpring = (flipped: FlipData) => {
}
}

export const normalizeSpeed = (speedConfig: number | undefined) => {
if (typeof speedConfig !== 'number') return 1.1
return 1 + Math.min(Math.max(speedConfig * 5, 0), 5)
}

export const staggeredSprings = (
flippedArray: FlipDataArray,
staggerConfig: StaggerConfigValue = {}
Expand All @@ -54,9 +59,7 @@ export const staggeredSprings = (
flippedArray.reverse()
}

const normalizedSpeed = staggerConfig.speed
? 1 + Math.max(Math.min(staggerConfig.speed, 0), 1)
: 1.1
const normalizedSpeed = normalizeSpeed(staggerConfig.speed)

const nextThreshold = 1 / Math.max(Math.min(flippedArray.length, 100), 10)

Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const Flipped: React.SFC<FlippedProps>

export interface StaggerConfigValue {
reverse?: boolean
/** A number between 0 (for a slower stagger) and 1 (for a faster stagger) */
speed: number
}

Expand Down

0 comments on commit 6d34691

Please sign in to comment.