Skip to content

Commit

Permalink
feat: drag scrollbar
Browse files Browse the repository at this point in the history
  • Loading branch information
bbohlender committed Aug 26, 2024
1 parent 9e1cc20 commit 3170664
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 59 deletions.
1 change: 1 addition & 0 deletions examples/uikit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"type": "module",
"dependencies": {
"@pmndrs/pointer-events": "^6.2.1",
"@preact/signals-core": "^1.5.1",
"@react-three/drei": "^9.96.1",
"@react-three/uikit": "workspace:^",
Expand Down
15 changes: 12 additions & 3 deletions examples/uikit/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentRef, StrictMode, Suspense, useEffect, useMemo, useRef, useState } from 'react'
import { Canvas } from '@react-three/fiber'
import { Canvas, useThree } from '@react-three/fiber'
import { Box, OrbitControls, OrthographicCamera, RenderTexture } from '@react-three/drei'
import { signal } from '@preact/signals-core'
import {
Expand All @@ -16,12 +16,12 @@ import {
FontFamilyProvider,
ComponentInternals,
ImageProperties,
canvasInputProps,
Video,
useMeasureText,
} from '@react-three/uikit'
import { Texture } from 'three'
import { Skeleton } from '../../../packages/kits/default/src/skeleton.js'
import { forwardHtmlEvents } from '@pmndrs/pointer-events'

export default function App() {
const texture = useMemo(() => signal<Texture | undefined>(undefined), [])
Expand All @@ -43,11 +43,12 @@ export default function App() {
<source src="./video.mp4" />
</video>
<Canvas
{...canvasInputProps}
events={() => ({ enabled: false, priority: 0 })}
frameloop="demand"
style={{ height: '100dvh', touchAction: 'none' }}
gl={{ localClippingEnabled: true }}
>
<SwitchToXRPointerEvents />
<MeasureText />
<OrbitControls />
<Box />
Expand Down Expand Up @@ -349,3 +350,11 @@ function MeasureText() {
</DefaultProperties>
</Root>
*/

export function SwitchToXRPointerEvents() {
const domElement = useThree((s) => s.gl.domElement)
const camera = useThree((s) => s.camera)
const scene = useThree((s) => s.scene)
useEffect(() => forwardHtmlEvents(domElement, camera, scene), [domElement, camera, scene])
return null
}
5 changes: 4 additions & 1 deletion packages/uikit/src/components/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { createInteractionPanel } from '../panel/instanced-panel-mesh.js'
import { Initializers } from '../utils.js'
import { darkPropertyTransformers } from '../dark.js'
import { getDefaultPanelMaterialConfig } from '../panel/index.js'
import { computedInheritableProperty } from '../internals.js'

export type InheritableContainerProperties = WithClasses<
WithConditionals<
Expand Down Expand Up @@ -119,6 +120,7 @@ export function createContainer(
const scrollPosition = createScrollPosition()
applyScrollPosition(childrenContainer, scrollPosition, parentContext.root.pixelSize, initializers)
const childrenMatrix = computedGlobalScrollMatrix(scrollPosition, globalMatrix, parentContext.root.pixelSize)
const scrollbarWidth = computedInheritableProperty(mergedProperties, 'scrollbarWidth', 10)
createScrollbars(
mergedProperties,
scrollPosition,
Expand All @@ -128,6 +130,7 @@ export function createContainer(
parentContext.clippingRect,
orderInfo,
parentContext.root.panelGroupManager,
scrollbarWidth,
initializers,
)
const interactionPanel = createInteractionPanel(
Expand All @@ -142,7 +145,7 @@ export function createContainer(
parentContext.anyAncestorScrollable,
flexState,
object,
interactionPanel,
scrollbarWidth,
properties,
parentContext.root,
initializers,
Expand Down
4 changes: 3 additions & 1 deletion packages/uikit/src/components/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export function createImage(
const scrollPosition = createScrollPosition()
applyScrollPosition(childrenContainer, scrollPosition, parentContext.root.pixelSize, initializers)
const childrenMatrix = computedGlobalScrollMatrix(scrollPosition, globalMatrix, parentContext.root.pixelSize)
const scrollbarWidth = computedInheritableProperty(mergedProperties, 'scrollbarWidth', 10)
createScrollbars(
mergedProperties,
scrollPosition,
Expand All @@ -165,6 +166,7 @@ export function createImage(
parentContext.clippingRect,
orderInfo,
parentContext.root.panelGroupManager,
scrollbarWidth,
initializers,
)
const interactionPanel = createImageMesh(
Expand All @@ -182,7 +184,7 @@ export function createImage(
parentContext.anyAncestorScrollable,
flexState,
object,
interactionPanel,
scrollbarWidth,
properties,
parentContext.root,
initializers,
Expand Down
4 changes: 3 additions & 1 deletion packages/uikit/src/components/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export function createRoot(
const scrollPosition = createScrollPosition()
applyScrollPosition(childrenContainer, scrollPosition, pixelSize, initializers)
const childrenMatrix = computedGlobalScrollMatrix(scrollPosition, identityMatrix, pixelSize)
const scrollbarWidth = computedInheritableProperty(mergedProperties, 'scrollbarWidth', 10)
createScrollbars(
mergedProperties,
scrollPosition,
Expand All @@ -182,6 +183,7 @@ export function createRoot(
undefined,
orderInfo,
panelGroupManager,
scrollbarWidth,
initializers,
)

Expand Down Expand Up @@ -210,7 +212,7 @@ export function createRoot(
undefined,
flexState,
object,
interactionPanel,
scrollbarWidth,
properties,
ctx,
initializers,
Expand Down
5 changes: 4 additions & 1 deletion packages/uikit/src/components/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { SVGLoader, SVGResult } from 'three/examples/jsm/loaders/SVGLoader.js'
import { darkPropertyTransformers } from '../dark.js'
import { PanelGroupProperties, computedPanelGroupDependencies, getDefaultPanelMaterialConfig } from '../panel/index.js'
import { KeepAspectRatioProperties } from './image.js'
import { computedInheritableProperty } from '../internals.js'

export type InheritableSvgProperties = WithClasses<
WithConditionals<
Expand Down Expand Up @@ -170,6 +171,7 @@ export function createSvg(
const scrollPosition = createScrollPosition()
applyScrollPosition(childrenContainer, scrollPosition, parentContext.root.pixelSize, initializers)
const childrenMatrix = computedGlobalScrollMatrix(scrollPosition, globalMatrix, parentContext.root.pixelSize)
const scrollbarWidth = computedInheritableProperty(mergedProperties, 'scrollbarWidth', 10)
createScrollbars(
mergedProperties,
scrollPosition,
Expand All @@ -179,6 +181,7 @@ export function createSvg(
parentContext.clippingRect,
orderInfo,
parentContext.root.panelGroupManager,
scrollbarWidth,
initializers,
)

Expand All @@ -195,7 +198,7 @@ export function createSvg(
parentContext.anyAncestorScrollable,
flexState,
object,
interactionPanel,
scrollbarWidth,
properties,
parentContext.root,
initializers,
Expand Down
Loading

0 comments on commit 3170664

Please sign in to comment.