Skip to content

Commit

Permalink
Rename Mover component to ScrollBody
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcetinkaya committed Feb 23, 2020
1 parent 410c535 commit 5a78809
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 57 deletions.
2 changes: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions src/__tests__/mover.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
import { Mover } from '../components/mover'
import { ScrollBody } from '../components/scrollBody'
import { Vector1D } from '../components/vector1d'

let mover: Mover
let scrollBody: ScrollBody
let target: Vector1D

beforeEach(() => {
mover = Mover({ location: Vector1D(0), speed: 10, mass: 1 })
scrollBody = ScrollBody({
location: Vector1D(0),
speed: 10,
mass: 1,
})
target = Vector1D(10)
})

describe('Mover', () => {
describe('ScrollBody', () => {
describe('Seek', () => {
test('Seeks given target Vector', () => {
mover.seek(target).update()
expect(mover.location.get()).toBeGreaterThan(0)
scrollBody.seek(target).update()
expect(scrollBody.location.get()).toBeGreaterThan(0)
})
test('Has direction value of 1 when it seeks given positive target Vector', () => {
mover.seek(target).update()
expect(mover.direction.get()).toBe(1)
scrollBody.seek(target).update()
expect(scrollBody.direction.get()).toBe(1)
})
test('Has direction value of -1 when it seeks given negative target Vector', () => {
target.setNumber(-10)
mover.seek(target).update()
expect(mover.direction.get()).toBe(-1)
scrollBody.seek(target).update()
expect(scrollBody.direction.get()).toBe(-1)
})
})

describe('Settle', () => {
test('Settle is true when diff to given target Vector is <= 0.001', () => {
mover.location.setNumber(9.999)
expect(mover.settle(target)).toBe(true)
scrollBody.location.setNumber(9.999)
expect(scrollBody.settle(target)).toBe(true)
})
test('Settle is false when diff to given target Vector is > 0.001', () => {
mover.location.setNumber(9.99)
expect(mover.settle(target)).toBe(false)
scrollBody.location.setNumber(9.99)
expect(scrollBody.settle(target)).toBe(false)
})
})
})
8 changes: 4 additions & 4 deletions src/__tests__/scrollBounds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { Animation } from '../components/animation'
import { ScrollBounds } from '../components/scrollBounds'
import { Vector1D } from '../components/vector1d'
import { Limit } from '../components/limit'
import { Mover } from '../components/mover'
import { ScrollBody } from '../components/scrollBody'

let scrollBounds: ScrollBounds
let location: Vector1D
let mover: Mover
let scrollBody: ScrollBody
const animation = Animation(() => {})
const limit = Limit({ min: 0, max: 10 })
const tolerance = 50

beforeEach(() => {
location = Vector1D(0)
mover = Mover({ location, mass: 1, speed: 10 })
scrollBody = ScrollBody({ location, mass: 1, speed: 10 })
scrollBounds = ScrollBounds({
limit,
location,
mover,
scrollBody,
animation,
tolerance,
})
Expand Down
10 changes: 5 additions & 5 deletions src/components/dragBehaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Direction } from './direction'
import { EventDispatcher } from './eventDispatcher'
import { EventStore } from './eventStore'
import { Limit } from './limit'
import { Mover } from './mover'
import { Pointer } from './pointer'
import { ScrollBody } from './scrollBody'
import { ScrollTo } from './scrollTo'
import { Vector1D } from './vector1d'

Expand All @@ -18,7 +18,7 @@ type Params = {
location: Vector1D
animation: Animation
scrollTo: ScrollTo
mover: Mover
scrollBody: ScrollBody
index: Counter
limit: Limit
loop: boolean
Expand All @@ -34,7 +34,7 @@ export type DragBehaviour = {
}

export function DragBehaviour(params: Params): DragBehaviour {
const { target, mover, dragFree, animation } = params
const { target, scrollBody, dragFree, animation } = params
const { element, pointer, location, events, limit } = params
const { direction } = pointer
const focusNodes = ['INPUT', 'SELECT', 'TEXTAREA']
Expand Down Expand Up @@ -129,7 +129,7 @@ export function DragBehaviour(params: Params): DragBehaviour {
pointer.down(evt)
dragStartLocation.set(target)
target.set(location)
mover.useDefaultMass().useSpeed(80)
scrollBody.useDefaultMass().useSpeed(80)
addInteractionEvents()
animation.start()
startX.set(pointer.read(evt, 'x'))
Expand Down Expand Up @@ -170,7 +170,7 @@ export function DragBehaviour(params: Params): DragBehaviour {
state.preventScroll = false
state.isDown = false
interactionEvents.removeAll()
mover.useSpeed(movementSpeed())
scrollBody.useSpeed(movementSpeed())
seekTargetBy(force)
events.dispatch('dragEnd')
}
Expand Down
22 changes: 11 additions & 11 deletions src/components/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { Counter } from './counter'
import { DragBehaviour } from './dragBehaviour'
import { EventDispatcher } from './eventDispatcher'
import { Limit } from './limit'
import { Mover } from './mover'
import { Options } from './options'
import { Pointer } from './pointer'
import { PxToPercent } from './pxToPercent'
import { ScrollBody } from './scrollBody'
import { ScrollBounds } from './scrollBounds'
import { ScrollBy } from './scrollBy'
import { ScrollContain } from './scrollContain'
Expand All @@ -30,7 +30,7 @@ export type Engine = {
index: Counter
indexPrevious: Counter
indexGroups: number[][]
mover: Mover
scrollBody: ScrollBody
pointer: DragBehaviour
slideLooper: SlideLooper
target: Vector1D
Expand Down Expand Up @@ -98,24 +98,24 @@ export function Engine(
const direction = (): number => {
return pointer.isDown()
? pointer.direction.get()
: engine.mover.direction.get()
: engine.scrollBody.direction.get()
}

// Draw
const update = (): void => {
engine.mover.seek(target).update()
engine.scrollBody.seek(target).update()
if (!pointer.isDown()) {
if (!loop) engine.scrollBounds.constrain(target)
if (engine.mover.settle(target)) engine.animation.stop()
if (engine.scrollBody.settle(target)) engine.animation.stop()
}
if (loop) {
engine.scrollLooper.loop(direction())
engine.slideLooper.loop(slides)
}
if (engine.mover.location.get() !== target.get()) {
if (engine.scrollBody.location.get() !== target.get()) {
events.dispatch('scroll')
}
engine.translate.to(engine.mover.location)
engine.translate.to(engine.scrollBody.location)
engine.animation.proceed()
}

Expand All @@ -124,7 +124,7 @@ export function Engine(
const startLocation = scrollSnaps[index.get()]
const location = Vector1D(startLocation)
const target = Vector1D(startLocation)
const mover = Mover({ location, speed, mass: 1 })
const scrollBody = ScrollBody({ location, speed, mass: 1 })
const scrollTo = ScrollTo({
animation,
events,
Expand Down Expand Up @@ -153,8 +153,8 @@ export function Engine(
limit,
location,
loop,
mover,
pointer: Pointer(pxToPercent),
scrollBody,
scrollTo,
snapSizes,
target,
Expand All @@ -166,13 +166,13 @@ export function Engine(
index,
indexGroups,
indexPrevious,
mover,
pointer,
scrollBody,
scrollBounds: ScrollBounds({
animation,
limit,
location,
mover,
scrollBody,
tolerance: 50,
}),
scrollBy: ScrollBy({
Expand Down
26 changes: 13 additions & 13 deletions src/components/mover.ts → src/components/scrollBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ type Params = {
mass: number
}

export type Mover = {
export type ScrollBody = {
location: Vector1D
direction: Direction
update: () => void
seek: (target: Vector1D) => Mover
seek: (target: Vector1D) => ScrollBody
settle: (target: Vector1D) => boolean
useSpeed: (newSpeed: number) => Mover
useDefaultSpeed: () => Mover
useMass: (newMass: number) => Mover
useDefaultMass: () => Mover
useSpeed: (newSpeed: number) => ScrollBody
useDefaultSpeed: () => ScrollBody
useMass: (newMass: number) => ScrollBody
useDefaultMass: () => ScrollBody
}

export function Mover(params: Params): Mover {
export function ScrollBody(params: Params): ScrollBody {
const roundToTwoDecimals = roundToDecimals(2)
const { location, speed, mass } = params
const velocity = Vector1D(0)
Expand All @@ -40,7 +40,7 @@ export function Mover(params: Params): Mover {
acceleration.add(force)
}

function seek(target: Vector1D): Mover {
function seek(target: Vector1D): ScrollBody {
attraction.set(target).subtract(location)
const mag = attraction.magnitude()
const m = map(mag, 0, 100, 0, state.speed)
Expand All @@ -61,27 +61,27 @@ export function Mover(params: Params): Mover {
return hasSettled
}

function useSpeed(desired: number): Mover {
function useSpeed(desired: number): ScrollBody {
state.speed = desired
return self
}

function useDefaultSpeed(): Mover {
function useDefaultSpeed(): ScrollBody {
useSpeed(speed)
return self
}

function useMass(desired: number): Mover {
function useMass(desired: number): ScrollBody {
state.mass = desired
return self
}

function useDefaultMass(): Mover {
function useDefaultMass(): ScrollBody {
useMass(mass)
return self
}

const self: Mover = {
const self: ScrollBody = {
direction,
location,
seek,
Expand Down
8 changes: 4 additions & 4 deletions src/components/scrollBounds.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Animation } from './animation'
import { Limit } from './limit'
import { Mover } from './mover'
import { ScrollBody } from './scrollBody'
import { Vector1D } from './vector1d'

type Params = {
limit: Limit
location: Vector1D
mover: Mover
scrollBody: ScrollBody
animation: Animation
tolerance: number
}
Expand All @@ -16,7 +16,7 @@ export type ScrollBounds = {
}

export function ScrollBounds(params: Params): ScrollBounds {
const { limit, location, mover, animation, tolerance } = params
const { limit, location, scrollBody, animation, tolerance } = params
const { min, max, reachedMin, reachedMax } = limit
const state = { timeout: 0 }

Expand All @@ -31,7 +31,7 @@ export function ScrollBounds(params: Params): ScrollBounds {
const constraint = limit.constrain(v.get())
state.timeout = window.setTimeout(() => {
v.setNumber(constraint)
mover.useSpeed(10).useMass(3)
scrollBody.useSpeed(10).useMass(3)
animation.start()
state.timeout = 0
}, tolerance)
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function EmblaCarousel(
const newSlider = Object.assign(slider, engine)
eventStore.add(window, 'resize', debouncedResize)
slides.forEach(slideFocusEvent)
slider.translate.to(slider.mover.location)
slider.translate.to(slider.scrollBody.location)

if (options.loop && slides.length === 1) {
return activate({ loop: false })
Expand Down Expand Up @@ -170,24 +170,24 @@ export function EmblaCarousel(

function scrollNext(): void {
const next = slider.index.clone().add(1)
slider.mover.useDefaultMass().useDefaultSpeed()
slider.scrollBody.useDefaultMass().useDefaultSpeed()
slider.scrollTo.index(next.get(), -1)
}

function scrollPrev(): void {
const prev = slider.index.clone().add(-1)
slider.mover.useDefaultMass().useDefaultSpeed()
slider.scrollBody.useDefaultMass().useDefaultSpeed()
slider.scrollTo.index(prev.get(), 1)
}

function scrollBy(progress: number): void {
const distance = slider.scrollBy.distance(progress)
slider.mover.useDefaultMass().useDefaultSpeed()
slider.scrollBody.useDefaultMass().useDefaultSpeed()
slider.scrollTo.distance(distance, false)
}

function scrollTo(index: number): void {
slider.mover.useDefaultMass().useDefaultSpeed()
slider.scrollBody.useDefaultMass().useDefaultSpeed()
slider.scrollTo.index(index, 0)
}

Expand Down

0 comments on commit 5a78809

Please sign in to comment.