-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Improved event handling #379
Conversation
.storybook/config.js
Outdated
configure(loadStories, module); | ||
|
||
// console.log('Adding some window event handlers to the story'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove debug code
@@ -0,0 +1,135 @@ | |||
# How we use DOM events |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A completely new page explaining in painful detail how we manage events
docs/patterns/multi-drag.md
Outdated
@@ -0,0 +1,105 @@ | |||
# Mutli drag pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this file from the PR 😊
@@ -0,0 +1,48 @@ | |||
// @flow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I keep writing this code all the time so I decided to bake it in
@@ -21,7 +21,9 @@ import createMouseSensor from './sensor/create-mouse-sensor'; | |||
import createKeyboardSensor from './sensor/create-keyboard-sensor'; | |||
import createTouchSensor from './sensor/create-touch-sensor'; | |||
|
|||
const getFalse: () => boolean = () => false; | |||
const preventHtml5Dnd = (event: DragEvent) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now actually doing the right thing
@@ -36,8 +36,7 @@ export type DragHandleProps = {| | |||
|
|||
// Stop html5 drag and drop | |||
draggable: boolean, | |||
onDragStart: () => boolean, | |||
onDrop: () => boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onDrop is not actually needed now that we are doing the correct thing in onDragStart
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should all references to onDrop
be removed or do they refer to different things? Namely the onDrop
prop in the Callbacks
type above and its usages.
}; | ||
|
||
const windowBindings = { | ||
const windowBindings: EventBinding[] = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new way of bindings events that is WAY more consistent between sensors
@@ -53,8 +59,9 @@ export default ({ | |||
const stopDragging = (fn?: Function = noop, shouldBlockClick?: boolean = true) => { | |||
schedule.cancel(); | |||
unbindWindowEvents(); | |||
mouseDownMarshal.reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we need a .reset but i'll explain later
}; | ||
|
||
const onMouseDown = (event: MouseEvent): void => { | ||
if (mouseDownMarshal.isHandled()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a new mechanism to avoid parents using the same event without needing to call .preventDefault or stop the event
bindings.forEach((binding: EventBinding) => { | ||
const options: Object = { | ||
...sharedOptions, | ||
...binding.options, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bindings can have their own overrides
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I have a couple of minor comments, none of which are blocking.
.storybook/config.js
Outdated
// defaultPrevented: ${event.defaultPrevented} | ||
// `)); | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be left in? Will it be uncommented later on?
README.md
Outdated
@@ -1256,7 +1262,7 @@ The `children` function is also provided with a small amount of state relating t | |||
|
|||
### Adding an `onClick` handler to a `Draggable` or a *drag handle* | |||
|
|||
You are welcome to add your own `onClick` handler to a `Draggable` or a *drag handle* (which might be the same element). `onClick` events handlers will only be called if we do not block the click. We block click events from occurring when the user was dragging an item. See [#sloppy-clicks-and-click-blocking-](sloppy clicks and click blocking) for more information. | |||
You are welcome to add your own `onClick` handler to a `Draggable` or a *drag handle* (which might be the same element). `onClick` events handlers will only be called if we do not block the click. We block click events from occurring when the user was dragging an item. See [#sloppy-clicks-and-click-prevention-](sloppy clicks and click prevention) for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line still true? Won't onClick
event handlers now be called but with e.defaultPrevented
set to true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct!
const flag: string = '__react-beautiful-dnd-debug-hook__'; | ||
|
||
// temp | ||
// window[flag] = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be left in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll clean it up
@@ -36,8 +36,7 @@ export type DragHandleProps = {| | |||
|
|||
// Stop html5 drag and drop | |||
draggable: boolean, | |||
onDragStart: () => boolean, | |||
onDrop: () => boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should all references to onDrop
be removed or do they refer to different things? Namely the onDrop
prop in the Callbacks
type above and its usages.
} | ||
|
||
preventStandardKeyEvents(event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worthwhile adding the keycodes that we're specifically handling above into this function as well so that we don't need to add event.preventDefault
inside each conditional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these keys are ones that are prevented by standard across multiple sensors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the keyboard sensor has its own unique keyboard controls
@@ -41,7 +46,8 @@ export default ({ | |||
const isDragging = (): boolean => state.isDragging; | |||
const isCapturing = (): boolean => Boolean(state.pending || state.isDragging); | |||
const schedule = createScheduler(callbacks); | |||
const clickBlocker: ClickBlocker = createClickBlocker(); | |||
const getWindow = (): HTMLElement => getWindowFromRef(getDraggableRef()); | |||
const postDragEventPreveter: EventPreventer = createPostDragEventPreventer(getWindow); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
test/unit/view/drag-handle.spec.js
Outdated
describe('cancelled with any keydown', () => { | ||
Object.keys(keyCodes).forEach((key: string) => { | ||
describe(`with the ${key} key`, () => { | ||
it('should not call execute any callbacks', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo I think, one of call/execute should be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
test/unit/view/drag-handle.spec.js
Outdated
@@ -820,9 +958,39 @@ describe('drag handle', () => { | |||
})).toBe(true); | |||
}); | |||
|
|||
it('should not do anything if there is nothing dragging', () => { | |||
it('should block a click after a cancel', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block -> prevent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. i have also updated the other places that used the blocked language
@@ -1,5 +1,6 @@ | |||
// @flow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a big file!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. needs to be split up
Closes #375