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

[v9] fix(events): bubble up primitives #3032

Open
wants to merge 3 commits into
base: v9
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
12 changes: 11 additions & 1 deletion packages/fiber/src/core/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,17 @@ export function createEvents(store: RootStore) {
if (intersections.length) {
const localState = { stopped: false }
for (const hit of intersections) {
const state = getRootState(hit.object)
let state = getRootState(hit.object)

if (!state)
hit.object.traverseAncestors((obj) => {
const _state = getRootState(obj)
if (_state) {
state = _state
return false
}
})

if (state) {
const { raycaster, pointer, camera, internal } = state
const unprojectedPoint = new THREE.Vector3(pointer.x, pointer.y, 0).unproject(camera)
Expand Down
16 changes: 7 additions & 9 deletions packages/fiber/src/core/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,10 @@ export function applyProps<T = any>(object: Instance<T>['object'], props: Instan
// Don't mutate reserved keys
if (RESERVED_PROPS.includes(prop)) continue

const isEvent = /^on(Pointer|Click|DoubleClick|ContextMenu|Wheel)/.test(prop)

// Deal with pointer events, including removing them if undefined
if (instance && /^on(Pointer|Click|DoubleClick|ContextMenu|Wheel)/.test(prop)) {
if (instance && isEvent) {
if (typeof value === 'function') instance.handlers[prop as keyof EventHandlers] = value as any
else delete instance.handlers[prop as keyof EventHandlers]
instance.eventCount = Object.keys(instance.handlers).length
Expand Down Expand Up @@ -428,7 +430,8 @@ export function applyProps<T = any>(object: Instance<T>['object'], props: Instan
}
// Else, just overwrite the value
else {
root[key] = value
// Don't write events to the object
if (!isEvent) root[key] = value

// Auto-convert sRGB textures, for now ...
// https://github.com/pmndrs/react-three-fiber/issues/344
Expand All @@ -446,17 +449,12 @@ export function applyProps<T = any>(object: Instance<T>['object'], props: Instan
}
}

if (
instance?.parent &&
rootState?.internal &&
instance.object instanceof THREE.Object3D &&
prevHandlers !== instance.eventCount
) {
if (instance && rootState?.internal && isObject3D(instance.object) && prevHandlers !== instance.eventCount) {
// Pre-emptively remove the instance from the interaction manager
const index = rootState.internal.interaction.indexOf(instance.object)
if (index > -1) rootState.internal.interaction.splice(index, 1)
// Add the instance to the interaction manager only when it has handlers
if (instance.eventCount && instance.object.raycast !== null && instance.object instanceof THREE.Object3D) {
if (instance.eventCount && instance.object.raycast !== null && isObject3D(instance.object)) {
rootState.internal.interaction.push(instance.object)
}
}
Expand Down
Loading