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

Allow control of Slate's event handler execution in custom event handler API #4299

Merged
5 changes: 5 additions & 0 deletions .changeset/chilly-gifts-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
Copy link
Collaborator

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

---

Allow custom event handlers on Editable component to return boolean flag to specify whether the event can be treated as being handled
13 changes: 10 additions & 3 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1191,14 +1191,21 @@ export const isEventHandled = <
EventType extends React.SyntheticEvent<unknown, unknown>
>(
event: EventType,
handler?: (event: EventType) => void
handler?: (event: EventType) => void | boolean
) => {
if (!handler) {
return false
}
// The custom event handler may return a boolean to specify whether the event
// shall be treated as being handled or not.
const shouldTreatEventAsHandled = handler(event)

handler(event)
return event.isDefaultPrevented() || event.isPropagationStopped()
return (
(shouldTreatEventAsHandled === true ||
event.isDefaultPrevented() ||
event.isPropagationStopped()) &&
shouldTreatEventAsHandled !== false
)
}

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

/**
Expand Down