Skip to content

Commit

Permalink
Fix infinite-scroll demo
Browse files Browse the repository at this point in the history
  • Loading branch information
rndexe committed Aug 9, 2024
1 parent 340d651 commit a6e4c6d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion demos/infinite-scroll/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dev": "vite --host",
"dev3": "e2e-dev $npm_package_name",
"build": "tsc && vite build",
"bbuild2": "tsc && e2e-build $npm_package_name",
"build2": "tsc && e2e-build $npm_package_name",
"preview": "vite preview"
},
"browserslist": [
Expand Down
16 changes: 9 additions & 7 deletions demos/infinite-scroll/src/ScrollControls.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as THREE from 'three'
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import { createRoot } from 'react-dom/client'
import { context as fiberContext, useFrame, useThree } from '@react-three/fiber'
import mergeRefs from 'react-merge-refs'

Expand Down Expand Up @@ -113,14 +113,17 @@ export function ScrollControls({

const oldTarget = typeof events.connected !== 'boolean' ? events.connected : gl.domElement
requestAnimationFrame(() => events.connect?.(el))
// @ts-ignore
const oldCompute = raycaster.computeOffsets
// @ts-ignore
raycaster.computeOffsets = ({ clientX, clientY }) => ({
offsetX: clientX - (target as HTMLElement).offsetLeft,
offsetY: clientY - (target as HTMLElement).offsetTop
})

return () => {
target.removeChild(el)
// @ts-ignore
raycaster.computeOffsets = oldCompute
events.connect?.(oldTarget)
}
Expand All @@ -135,7 +138,7 @@ export function ScrollControls({
let disableScroll = true
let firstRun = true

const onScroll = (e) => {
const onScroll = (e : any) => {
// Prevent first scroll because it is indirectly caused by the one pixel offset
if (!enabled || firstRun) return
invalidate()
Expand All @@ -161,7 +164,7 @@ export function ScrollControls({
el.addEventListener('scroll', onScroll, { passive: true })
requestAnimationFrame(() => (firstRun = false))

const onWheel = (e) => (el.scrollLeft += e.deltaY / 2)
const onWheel = (e : any) => (el.scrollLeft += e.deltaY / 2)
if (horizontal) el.addEventListener('wheel', onWheel, { passive: true })

return () => {
Expand All @@ -179,7 +182,7 @@ export function ScrollControls({
return <context.Provider value={state}>{children}</context.Provider>
}

const ScrollCanvas = React.forwardRef(({ children }, ref) => {
const ScrollCanvas = React.forwardRef(({ children } : { children?: React.ReactNode}, ref) => {
const group = React.useRef<THREE.Group>(null!)
const state = useScroll()
const { width, height } = useThree((state) => state.viewport)
Expand All @@ -203,13 +206,12 @@ const ScrollHtml = React.forwardRef(
}px,0)`
}
})
ReactDOM.render(
createRoot(state.fixed).render(
<div ref={mergeRefs([ref, group])} style={{ ...style, position: 'absolute', top: 0, left: 0, willChange: 'transform' }} {...props}>
<context.Provider value={state}>
<fiberContext.Provider value={fiberState}>{children}</fiberContext.Provider>
</context.Provider>
</div>,
state.fixed
</div>
)
return null
}
Expand Down
8 changes: 4 additions & 4 deletions demos/infinite-scroll/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ReactDOM from 'react-dom'
import { createRoot } from 'react-dom/client'
import { Logo } from '@pmndrs/branding'
import './styles.css'
import App from './App'
Expand All @@ -17,11 +17,11 @@ function Overlay() {
)
}

ReactDOM.render(
createRoot(
document.getElementById('root')).render(
<>
<App />
<Overlay />
<Logo style={{ position: 'absolute', bottom: 40, left: 40, width: 30 }} />
</>,
document.getElementById('root')
</>
)

0 comments on commit a6e4c6d

Please sign in to comment.