Skip to content
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

Add useScroll utility hook #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,19 @@ function App() {
</Show>
</Presence>
```

## Scroll-Linked Animations

`useScroll` provides reactive values based on Motion One's original [`scroll`](https://motion.dev/docs/scroll) function.

```ts
import { useScroll } from "solid-motionone"

// ...

const { time, scrollX, scrollY } = useScroll()

createEffect(() => console.log(scrollY.progress))
```

These values can be used in coordination with Motion animation properties to create scroll-linked/parallax animations.
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./types.js"
export {Motion} from "./motion.jsx"
export {Presence, PresenceContext} from "./presence.jsx"
export {createMotion, motion} from "./primitives.js"
export {createMotion, motion, useScroll} from "./primitives.js"
51 changes: 49 additions & 2 deletions src/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import {createMotionState, createStyles, MotionState, style} from "@motionone/dom"
import {Accessor, createEffect, onCleanup, useContext} from "solid-js"
import {
AxisScrollInfo,
createMotionState,
createStyles,
MotionState,
style,
scroll,
ScrollOptions,
} from "@motionone/dom"
import {Accessor, batch, createEffect, createSignal, onCleanup, onMount, useContext} from "solid-js"

import {PresenceContext, PresenceContextState} from "./presence.jsx"
import {Options} from "./types.js"
import {createStore, produce} from "solid-js/store"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import {createStore, produce} from "solid-js/store"


/** @internal */
export function createAndBindMotionState(
Expand Down Expand Up @@ -65,6 +74,44 @@ export function createMotion(
return state
}

export function useScroll(options?: ScrollOptions): {
time: Accessor<number>
scrollX: AxisScrollInfo

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
scrollX: AxisScrollInfo
scrollX: Accessor<AxisScrollInfo>

scrollY: AxisScrollInfo

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
scrollY: AxisScrollInfo
scrollY: Accessor<AxisScrollInfo>

} {
const [time, setTime] = createSignal(0)
const [scrollX, setScrollX] = createStore<AxisScrollInfo>({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [scrollX, setScrollX] = createStore<AxisScrollInfo>({
const [scrollX, setScrollX] = createSignal<AxisScrollInfo>({

current: 0,
offset: [],
progress: 0,
scrollLength: 0,
velocity: 0,
targetOffset: 0,
targetLength: 0,
containerLength: 0,
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})
}, { equals: false })

const [scrollY, setScrollY] = createStore<AxisScrollInfo>({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [scrollY, setScrollY] = createStore<AxisScrollInfo>({
const [scrollY, setScrollY] = createSignal<AxisScrollInfo>({

current: 0,
offset: [],
progress: 0,
scrollLength: 0,
velocity: 0,
targetOffset: 0,
targetLength: 0,
containerLength: 0,
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})
}, { equals: false })


onMount(() =>
scroll(({time, x, y}) => {
setTime(time)
setScrollX(x)
setScrollY(y)
}, options),
)

return {time, scrollX, scrollY}
}

/**
* motion is a Solid directive that makes binding to elements easier.
*
Expand Down