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

Added an optional config the R3F sheet context #502

Open
wants to merge 1 commit 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
26 changes: 21 additions & 5 deletions packages/r3f/src/main/SheetProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ import {useThree} from '@react-three/fiber'
import type {ISheet} from '@theatre/core'
import {bindToCanvas} from './store'

const ctx = createContext<{sheet: ISheet}>(undefined!)
export type R3FSheetConfig = {
namespacePrefix?: string;
}

export type R3FSheetContext = {
sheet: ISheet,
config?: R3FSheetConfig
}

const ctx = createContext<R3FSheetContext>(undefined!)

const useWrapperContext = (): {sheet: ISheet} => {
const useWrapperContext = (): R3FSheetContext => {
const val = useContext(ctx)
if (!val) {
if (!val || !val.sheet) {
throw new Error(
`No sheet found. You need to add a <SheetProvider> higher up in the tree. https://docs.theatrejs.com/r3f.html#sheetprovider`,
)
Expand All @@ -25,10 +34,17 @@ export const useCurrentSheet = (): ISheet | undefined => {
return useWrapperContext().sheet
}

export const useCurrentR3FSheetConfig = (): R3FSheetConfig | undefined => {
return useWrapperContext().config
}



const SheetProvider: React.FC<{
sheet: ISheet
children: ReactNode
}> = ({sheet, children}) => {
config?: R3FSheetConfig
}> = ({sheet, children, config}) => {
const {scene, gl} = useThree((s) => ({scene: s.scene, gl: s.gl}))

useEffect(() => {
Expand All @@ -41,7 +57,7 @@ const SheetProvider: React.FC<{
bindToCanvas({gl, scene})
}, [scene, gl])

return <ctx.Provider value={{sheet}}>{children}</ctx.Provider>
return <ctx.Provider value={{sheet, config}}>{children}</ctx.Provider>
}

export default SheetProvider
7 changes: 5 additions & 2 deletions packages/r3f/src/main/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {forwardRef, useEffect, useLayoutEffect, useRef} from 'react'
import {allRegisteredObjects, editorStore} from './store'
import {mergeRefs} from 'react-merge-refs'
import useInvalidate from './useInvalidate'
import {useCurrentSheet} from './SheetProvider'
import {useCurrentR3FSheetConfig, useCurrentSheet} from './SheetProvider'
import defaultEditableFactoryConfig from './defaultEditableFactoryConfig'
import type {EditableFactoryConfig} from './editableFactoryConfigUtils'
import {makeStoreKey} from './utils'
Expand Down Expand Up @@ -75,17 +75,20 @@ const createEditable = <Keys extends keyof JSX.IntrinsicElements>(
const objectRef = useRef<JSX.IntrinsicElements[U]>()

const sheet = useCurrentSheet()!
const sheetConfig = useCurrentR3FSheetConfig()
const rafDriver = useCurrentRafDriver()

const [sheetObject, setSheetObject] = useState<
undefined | ISheetObject<$FixMe>
>(undefined)

const keyPrefix = sheetConfig?.namespacePrefix ? `${sheetConfig.namespacePrefix} / ` : ''

const storeKey = useMemo(
() =>
makeStoreKey({
...sheet.address,
objectKey: theatreKey as $IntentionalAny,
objectKey: `${keyPrefix}${theatreKey}` as $IntentionalAny,
}),
[sheet, theatreKey],
)
Expand Down