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

Improved event handling #379

Merged
merged 19 commits into from
Mar 12, 2018
Merged

Improved event handling #379

merged 19 commits into from
Mar 12, 2018

Conversation

alexreardon
Copy link
Collaborator

Closes #375

configure(loadStories, module);

// console.log('Adding some window event handlers to the story');
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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

@@ -0,0 +1,105 @@
# Mutli drag pattern
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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) => {
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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

Copy link
Member

@Blasz Blasz Mar 12, 2018

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[] = [
Copy link
Collaborator Author

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();
Copy link
Collaborator Author

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()) {
Copy link
Collaborator Author

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,
Copy link
Collaborator Author

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

Copy link
Member

@Blasz Blasz left a 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.

// defaultPrevented: ${event.defaultPrevented}
// `));
// });
Copy link
Member

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.
Copy link
Member

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?

Copy link
Collaborator Author

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;
Copy link
Member

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?

Copy link
Collaborator Author

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
Copy link
Member

@Blasz Blasz Mar 12, 2018

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);
Copy link
Member

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?

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable typo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

describe('cancelled with any keydown', () => {
Object.keys(keyCodes).forEach((key: string) => {
describe(`with the ${key} key`, () => {
it('should not call execute any callbacks', () => {
Copy link
Member

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -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', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block -> prevent?

Copy link
Collaborator Author

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
Copy link
Member

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!

Copy link
Collaborator Author

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

@alexreardon alexreardon merged commit e8da50f into master Mar 12, 2018
@alexreardon alexreardon deleted the event-handling branch March 12, 2018 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants