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

Need for synchronous PreventDefault across Tao/Wry boundary #85

Closed
jkelleyrtp opened this issue Jan 7, 2022 · 5 comments · Fixed by #2792
Closed

Need for synchronous PreventDefault across Tao/Wry boundary #85

jkelleyrtp opened this issue Jan 7, 2022 · 5 comments · Fixed by #2792
Assignees
Labels
breaking This is a breaking change desktop Suggestions related to the desktop renderer enhancement New feature or request
Milestone

Comments

@jkelleyrtp
Copy link
Member

Unfortunately, there's no way today to conditionally call PreventDefault across the Tao/Wry boundary.

Developers should be able to disable default in a way that's not just hardcoded fields - IE allowing default behavior when right-clicking links in the browser.

The FFI boundary is particularly challenging because we cannot synchronously handle the event.

I think the solution is to always trap the event and prevent default.

Then, ff 1) the event has default behavior and 2) the user didn't prevent that default behavior in their Dioxus handler, then we should craft the event again and push it back in the real dom.

@valyagolev
Copy link
Contributor

valyagolev commented Nov 7, 2023

we should craft the event again and push it back in the real dom.

from my (maybe flawed) testing in the chrome console, synthetic events still manage to have a different behaviour:

(function(a) {
    //document.body.appendChild(a);
    //a.setAttribute('href', 'https://github.com');
    a.setAttribute('target', '_blank');
    a.dispatchEvent((function(e) {
        e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, 
                         true, false, false, false, 0, null);
        return e
    }(document.createEvent('MouseEvents'))))
}(document.querySelector('a[href^=http]')))

This opens a new tab and focuses it, as opposed to opening it in the background when the cmd-click is done using the real mouse

@ealmloff
Copy link
Member

ealmloff commented Nov 7, 2023

We cannot create an event that is identical to the event triggered by the browser. Events that are triggered by a user are trusted. Events that are modified by javascript will not be trusted

@bishoyroufael
Copy link

Is there any current workaround for calling prevent default on an event conditionally? I am particularly interested for desktop platform.

@nicoburns
Copy link
Contributor

Relevant Wry issue: tauri-apps/wry#454. Looks like there are potential APIs for this, but Wry has no plans to expose them. So possibilities are:

  • Synchronous HTTP requests (not nice, but would probably work decently so long as the webviews don't remove support)
  • Waiting until webviews implement better APIs which Wry then picks up

@jkelleyrtp jkelleyrtp assigned jkelleyrtp and ealmloff and unassigned jkelleyrtp Aug 1, 2024
@ealmloff
Copy link
Member

ealmloff commented Aug 6, 2024

From the mdn docs:

Notes
Calling preventDefault() during any stage of event flow cancels the event, meaning that any default action normally taken by the implementation as a result of the event will not occur.

It sounds like it can just be a flag on Event which is set in event handlers. We don't need to stay in lock step with the browser which makes this much easier to implement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This is a breaking change desktop Suggestions related to the desktop renderer enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants