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

Extended Click Event Handlers #530

Merged
merged 2 commits into from
Jun 24, 2020
Merged
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
18 changes: 14 additions & 4 deletions src/canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export interface UseCanvasProps extends CanvasProps {

export type DomEventHandlers = {
onClick(e: any): void
onContextMenu(e: any): void
onDoubleClick(e: any): void
onWheel(e: any): void
onPointerDown(e: any): void
onPointerUp(e: any): void
Expand Down Expand Up @@ -486,17 +488,23 @@ export const useCanvas = (props: UseCanvasProps): DomEventHandlers => {
const eventObject = data.eventObject
const handlers = (eventObject as any).__handlers
if (handlers && handlers[name]) {
// Forward all events back to their respective handlers with the exception of click,
// which must must the initial target
if (name !== 'click' || state.current.initialHits.includes(eventObject)) handlers[name](data)
// Forward all events back to their respective handlers with the exception of click events,
// which must use the initial target
if (
(name !== 'click' && name !== 'contextMenu' && name !== 'doubleClick') ||
state.current.initialHits.includes(eventObject)
) {
handlers[name](data)
}
}
})
// If a click yields no results, pass it back to the user as a miss
if (name === 'pointerDown') {
state.current.initialClick = [event.clientX, event.clientY]
state.current.initialHits = hits.map((hit) => hit.eventObject)
}
if (name === 'click' && !hits.length && onPointerMissed) {

if ((name === 'click' || name === 'contextMenu' || name === 'doubleClick') && !hits.length && onPointerMissed) {
if (calculateDistance(event) <= 2) onPointerMissed()
}
},
Expand All @@ -506,6 +514,8 @@ export const useCanvas = (props: UseCanvasProps): DomEventHandlers => {
useMemo(() => {
state.current.events = {
onClick: handlePointer('click'),
onContextMenu: handlePointer('contextMenu'),
onDoubleClick: handlePointer('doubleClick'),
onWheel: handlePointer('wheel'),
onPointerDown: handlePointer('pointerDown'),
onPointerUp: handlePointer('pointerUp'),
Expand Down
4 changes: 2 additions & 2 deletions src/reconciler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ export function applyProps(instance: any, newProps: any, oldProps: any = {}, acc
// Event-handlers ...
// are functions, that
// start with "on", and
// contain the name "pointer", or "wheel", or "click"
// contain the name "Pointer", "Click", "ContextMenu", or "Wheel"
if (is.fun(newProps[key]) && key.startsWith('on')) {
return key.includes('Pointer') || key.includes('Click') || key.includes('Wheel')
return key.includes('Pointer') || key.includes('Click') || key.includes('ContextMenu') || key.includes('Wheel')
}
})
const leftOvers = accumulative ? Object.keys(oldProps).filter((key) => newProps[key] === void 0) : []
Expand Down
2 changes: 2 additions & 0 deletions src/three-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export declare namespace ReactThreeFiber {

export type EventHandlers = {
onClick?: (event: MouseEvent) => void
onContextMenu?: (event: MouseEvent) => void
onDoubleClick?: (event: MouseEvent) => void
onPointerUp?: (event: PointerEvent) => void
onPointerDown?: (event: PointerEvent) => void
onPointerOver?: (event: PointerEvent) => void
Expand Down