-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Allow control of Slate's event handler execution in custom event handler API #4299
Allow control of Slate's event handler execution in custom event handler API #4299
Conversation
🦋 Changeset detectedLatest commit: 5ac9d88 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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 overall, just a small suggestion.
Could you check if documentation needs to be updated around this change?
.changeset/chilly-gifts-dance.md
Outdated
@@ -0,0 +1,5 @@ | |||
--- | |||
'slate-react': patch |
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 think this should be a minor
bump as it introduces new functionality rather than just a fix
Co-authored-by: Claudéric Demers <clauderic.d@gmail.com>
Thanks a lot for the review @clauderic. There is not much documentation on the |
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 looking great, thanks for adding documentation around this! Just a few final thoughts
if (shouldTreatEventAsHandled != null) { | ||
return shouldTreatEventAsHandled | ||
} | ||
|
||
return event.isDefaultPrevented() || event.isPropagationStopped() | ||
} | ||
|
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 the isDOMEventHandled
method be updated as well?
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.
Ha, very good point! I initially thought it shouldn't, but after having another look at the code I feel like it actually should behave the same way to stay consistent and not confuse people. I will adjust it.
docs/libraries/slate-react.md
Outdated
Your custom event handler can control whether or not Slate will execute its own event handler after yours depending on the return value of your event handler as described below. | ||
|
||
```javascript | ||
const onClick = useCallback(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.
Could we remove the useCallback
from here and below? It's not specifically relevant to what is being explained
Co-authored-by: Claudéric Demers <clauderic.d@gmail.com>
Hey mate, thanks for the second round of feedback. I updated the PR according to your suggestions. |
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.
Thanks for your contribution 🙏
…ler API (ianstormtaylor#4299) Co-authored-by: Georg Berecz <gberecz@palantir.com> Co-authored-by: Claudéric Demers <clauderic.d@gmail.com>
Description
From the Slate Slack channel:
We noticed that our own custom dnd behavior broke with the upgrade to 0.63 due to the changes introduced in this PR. For some context: We use react-dnd to enable users, among other things, to drag Nodes next to each other to automatically set up column layouts. Thus, while we want to use Slate's internal dnd logic in some cases, we need to prevent the default logic in others.
The root cause of the issue lies within the
state.isDraggingInternally
property that was introduced and that is set in theonDragStart
handler. Unfortunately, it is not possible to overwriteonDragStart
and prevent the default logic on the Editable component without having to callpreventDefault
orstopPropgation
on the event (which in turn would break react-dnd behavior). Otherwise, Slate's default handler is still executed.It is limiting and confusing that the differentiator whether or not an event was handled is solely based on isDefaultPrevented or isPropagationStopped.
This PR introduces an escape hatch for custom event handlers to specify whether or not the event should be treated as handled and, therefore, further processed by Slate's own handler.
Issue
Fixes: issue described above
Example
Context
This PR suggests the following change: Optionally, all custom event handlers can return a boolean to control the execution of Slate's default handler logic:
void
- Currently the only supported option and there is no change in this existing behavior. (thus, no breaking change for existing Slate editors)true
- Explicitly treat event as handled -> Do not execute Slate's default event handlerfalse
- Explicitly do not treat event as handled -> Execute Slate's default event handlerThe boolean flags will take precedence over the existing
preventDefault
andstopPropgation
checks and will, therefore, allow for more flexibility in adjusting Slate's behavior.Checks
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)