sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
77.61k stars 4.04k forks source link

Better HTMLDialogElement support for transitions #9155

Open danielniccoli opened 10 months ago

danielniccoli commented 10 months ago

Describe the problem

I've been trying to implement HTMLDialogElements with their native APIs. Adding transitions was a frustrating experience.

This is a minimal working example, without transitions. It uses no Svelte, only the native HTMLDialogElement API: showModal() displays the modal with its backdrop. close() and cancel() close the dialog. The element's methods also take care of all ARIA roles, focus and other accessibility tools. The backdrop can be styled with the dialog' s ::backdrop pseudo-class.

Problem

Describe the proposed solution

If Svelte would support transitions of elements between the display:none and other display states, and also apply them to the backdrop pseudo-class, most if not all of these problems would be solved (including the problems with the alternative solutions below)

Alternatives considered

This is a minimal example fo the workaround. By using an if-condition we remove the HTMLDialogElement from the DOM. An additional variable (e.g., visible) is used to remember if the dialogs visibility. We also have to implement a custom showModal() and closeModal() that changes not only the dialogs state, but also the independently managed visibility. Then we have to export the custom showModal() to the parent, resulting in an additional variable. All together that is quite a jump in complexity. Now we can add a transition to the dialog and it will work, because the element is actually removed and re-added to the DOM.

Problem

Importance

would make my life easier

danielniccoli commented 10 months ago

Related: #4723

msf-caesar commented 10 months ago

Try with using CSS transitions and animations to achieve the desired effects on the HTMLDialogElement and its backdrop. Apply the necessary CSS properties and values to create smooth transitions between display states, and use keyframes to define custom animations. Make sure to include the necessary selectors and pseudo-classes to target the specific elements that need the transitions.

danielniccoli commented 10 months ago

I was not asking for a workaround, but thanks for proving my point.