Skip to content

feat: add "config.step" prop #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/src/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,15 +645,16 @@ export class Controller<State extends Indexable = any> {
animated,
animatedValues: Array.from(animated.getPayload()),
immediate,
duration: config.duration,
easing: withDefault(config.easing, linear),
decay: config.decay,
mass: withDefault(config.mass, 1),
tension: withDefault(config.tension, 170),
friction: withDefault(config.friction, 26),
initialVelocity: withDefault(config.velocity, 0),
clamp: withDefault(config.clamp, false),
precision: withDefault(config.precision, 0.005),
step: config.step,
decay: config.decay,
duration: config.duration,
easing: withDefault(config.easing, linear),
config,
}
}
Expand Down
25 changes: 23 additions & 2 deletions packages/core/src/FrameLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,9 @@ export class FrameLoop {
active = true
}

animated.setValue(position)
animated.lastPosition = position
const roundPosition = roundToStep(position, config.step)
animated.setValue(roundPosition)
animated.lastPosition = animated.done ? roundPosition : position
}

if (changes && changed) {
Expand All @@ -223,3 +224,23 @@ export class FrameLoop {
return active
}
}

function roundToStep(n: number, step?: number) {
if (step) {
n = Math.round(n / step) * step
const p = getPrecision(step)
if (p) {
const q = Math.pow(10, p)
return Math.round(q * n + q / 1e16) / q
}
}
return n
}

function getPrecision(n: number) {
if (n % 1) {
const s = String(n)
return s.length - s.indexOf('.') - 1
}
return 0
}
2 changes: 1 addition & 1 deletion packages/core/src/types/spring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export interface SpringConfig {
velocity?: number
clamp?: boolean
precision?: number
delay?: number
step?: number
decay?: number | boolean
duration?: number
easing?: EasingFunction
Expand Down
19 changes: 14 additions & 5 deletions targets/web/src/animated.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { withAnimated, extendAnimated } from '@react-spring/animated'
import { CSSProperties, ForwardRefExoticComponent } from 'react'
import { SpringValue, ElementType, ComponentPropsWithRef, Merge } from 'shared'
import { SpringValue, ElementType, Merge } from 'shared'
import { elements, JSXElements } from './elements'

// Avoid https://github.com/microsoft/TypeScript/issues/29949
type ComponentPropsWithRef<
T extends ElementType
> = T extends React.ComponentClass<infer P>
? React.PropsWithoutRef<P> & { ref?: any }
: React.PropsWithRef<React.ComponentProps<T>>

type DOMComponents = {
[Tag in JSXElements]: AnimatedComponent<Tag>
}
Expand All @@ -23,10 +30,12 @@ export { animated as a }
export type AnimatedComponent<
T extends ElementType
> = ForwardRefExoticComponent<
AnimatedProps<Merge<ComponentPropsWithRef<T>, { style?: StyleProps }>> & {
scrollTop?: SpringValue<number> | number
scrollLeft?: SpringValue<number> | number
}
AnimatedProps<
Merge<
ComponentPropsWithRef<T>,
{ style?: StyleProps; scrollTop?: number; scrollLeft?: number }
>
>
>

/** The props of an `animated()` component */
Expand Down