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

event.preventDefault() in onKeyDown does not prevent Slate's default behavior. #3994

Open
BiosSun opened this issue Jan 16, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@BiosSun
Copy link

BiosSun commented Jan 16, 2025

Description

When using event.preventDefault() inside an onKeyDown handler in a Plate plugin, it does not stop Slate's default behavior as expected. For example, consider the following plugin:

createPlatePlugin({
    key: 'myPlugin',
    handlers: {
        onKeyDown({ editor, event }) {
            if (Hotkeys.isMoveBackward(event)) {
                event.preventDefault()
                // Custom logic here
            }
        },
    },
})

I believe the issue is caused by the following line of code in pipeHandler.ts:

export const isEventHandled = <
EventType extends React.SyntheticEvent<unknown, unknown>,
>(
event: EventType,
handler?: (event: EventType) => boolean | void
) => {
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);
if (shouldTreatEventAsHandled != null) {
return shouldTreatEventAsHandled;
}
return event.isPropagationStopped();
};

Currently, the condition is:

return event.isPropagationStopped();

However, it should also check whether the default action has been prevented, like this:

return event.isDefaultPrevented() || event.isPropagationStopped();

This behavior was correct in earlier versions but was changed in this commit. I'm not sure of the rationale behind this modification, but it seems to have introduced the issue.

Reproduction URL

No response

Steps to Reproduce

  1. Create a plugin with an onKeyDown handler that calls event.preventDefault().
  2. Trigger the handler by pressing the corresponding key (e.g., a key mapped to Hotkeys.isMoveBackward).
  3. Observe that Slate's default behavior is not prevented.

Plate version

42.0.1

Slate React version

0.112.0

Browsers

No response

@BiosSun BiosSun added the bug Something isn't working label Jan 16, 2025
@BiosSun BiosSun changed the title In onKeyDown, calling event.preventDefault() cannot prevent Slate's default handling. event.preventDefault() in onKeyDown does not prevent Slate's default behavior. Jan 16, 2025
@zbeyens
Copy link
Member

zbeyens commented Jan 16, 2025

@12joan do you have some thoughts on that?

@12joan
Copy link
Collaborator

12joan commented Jan 16, 2025

It sounds like event.preventDefault() definitely ought to work the way you expect. The commit that removed this behaviour is three and a half years old, so it's quite likely that the reason for this change is no longer relevant. Shall we reinstante it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants