To begin with, I am not an enormous fan of modal home windows in an online utility. InVision used a ton of modal home windows; and it felt like we painted ourselves right into a nook that we could not get out of. My ambiguity on the matter has solely been hardened by articles comparable to “we use too many rattling modals“, “modalz modalz modalz“, and “who’s afraid of a tough web page load“. That mentioned, modals—and modal-adjacent experiences—do have their place. And, as I begin to consider this expertise within the context of an HTMX-powered internet app, I wished to give you a set of purposeful necessities that I may intention for.
I am not saying that each one of those necessities should be met. Solely that these may be met in a good world. And, if I’ve to commerce one requirement off for one more, I need to have the ability to accomplish that in a cognizant means, acknowledging that I am making a sacrifice (on behalf of the person’s expertise).
Practical Necessities of a Modal Window
-
Maybe the only most necessary requirement is that the person’s Again Button has to work. Again button performance is really the hill that I am keen to die on. Nothing frustrates a person greater than hitting the again button and being whisked away to a very surprising location.
-
Aside from really transient dialogs, comparable to prompts, confirmations, and error messages, a modal window must be deep linkable. That’s, the URL ought to mirror that the modal window is open in the identical means that the URL ought to mirror any space of an utility that the person can navigate to.
-
Refreshing the browser web page ought to deliver the person again to the identical web page and modal window. If the modal window is deep linkable, this may naturally occur.
-
If a hyperlink triggered the modal window, the person ought to be capable to right-click or CMD+Click on on mentioned hyperlink to open the page-and-modal in a brand new browser tab. If the modal window is deep linkable, this may naturally occur.
-
As soon as the modal window is open, hitting the Escape key ought to shut the modal window, return the person again to the earlier URL (ie, the originating web page URL much less the URL-based modal indicator).
-
One modal window ought to be capable to hyperlink to a different modal window. For instance, a “paywall modal” ought to be capable to hyperlink throughout to an “improve subscription” modal.
-
When a modal window is open, the first web page scrolling ought to in all probability be deactivated. And, whereas the content material of a modal ought to intention to suit solely throughout the viewport, if it does have to scroll vertically, double-scrollbars must be prevented.
Gripe: that is particularly laborious for Mac-based builders who suppose that the world lives in a magic mouse, hidden scrollbar world. It does not – the remainder of the world sees scrollbars mainly on a regular basis.
-
The content material of the modal window ought to be capable to change the URL as wanted with out destroying the underlying web page. For instance, a modal window with a search enter could have to push a
key phrases
worth into the URL because the person sorts. -
The modal window ought to be capable to change the window title to assist within the browser historical past person expertise (ie, what the person will see if they appear of their record of beforehand visited pages).
-
If the modal window modified the window title, hitting the Escape ought to each shut the modal window and reset the window title to the title utilized by the originating / underlying web page.
Bear in mind, this can be a “good world” implementation. I believe getting all of those proper is a really excessive bar; and, might be why modal home windows ought to typically be prevented in an online utility. That mentioned, I’ve seen a number of modal home windows out within the wild that hardly fulfill a single considered one of these core purposeful necessities.
On a subjective be aware, discover that I’ve no requirement for animating modal home windows into or out of existence. To me, animating a modal window is simply this facet of ineffective. And, ought to solely be a priority after nearly all of these different necessities have been glad. When you really feel the necessity to animate a modal window, it ought to solely be carried out with the least quantity of effort attainable (comparable to a CSS animation keyframe). If you might want to begin wrapping DOM nodes in different DOM nodes simply to energy animations, you’ve got already gotten too complicated.
Modal Home windows In an HTMX Utility
In an Angular utility, this record of purposeful necessities is a excessive bar, even with the entire client-side scripting logic that Angular affords. In an HTMX utility, this turns into even more durable. That is very true as a result of HTMX is a little bit fussy about sure management flows. For instance:
-
HTMX will not inherently “increase” a hyperlink that has each an
href
and anhx-get
verb utilized to it. -
HTMX will not permit CMD+Click on hyperlinks to open in a brand new browser tab if the hyperlink has each an
href
and anhx-get
on it (as a result of means it decide when to name.preventDefault()
on the occasion). -
HTMX handles
href
andhx-push-url
attributes otherwise by way of the browser historical past cache (it quietly normalizes thehref
worth to incorporate the complete useful resource path however takes thehx-push-url
worth as-is, resulting in surprising historical past cache misses). -
HTMX will not cache anchor-based URLs, so trying to drive modal home windows off of the URL hash / fragment is mainly unattainable.
-
HTMX triggers the
load
occasion onhx-trigger
attributes even when rendering the web page out of the historical past cache (comparable to when hitting the Again / Ahead buttons within the browser).
I am nonetheless studying HTMX by a number of trial and error; so nailing down why one thing is working the best way that it is working consists of a number of opening up the HTMX supply code and inserting console.log()
statements. Thank goodness the HTMX code is so readable! That mentioned, I hope that I am not getting any of the mechanics incorrect within the earlier record; however, please be affected person if I’m.
The extra I take into consideration modal home windows, particularly in an HTMX context, the much less I really need to use them. Modal window are straightforward to do; however, they’re extremely laborious to do accurately.
https://bennadel.com/4802