Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/spacejack/mithril-modal

Modal example for Mithril.js
https://github.com/spacejack/mithril-modal

mithriljs

Last synced: about 1 month ago
JSON representation

Modal example for Mithril.js

Awesome Lists containing this project

README

        

# Mithril Modal Examples

There are lots of ways to implement modal-style popups in a [Mithril.js](https://mithril.js.org/) app. These are a few example techniques.

## Imperative Style

Probably the most straightforward to implement and often the easiest to use. This takes the approach of conditionally rendering a single Modal component at the top level of the DOM, at the very end of the top level nodes. This placement in the DOM ensures it will render over everything else.

Visibility of the modal is controlled by a simple `isOpen` flag, which is changed by calling the `openModal` and `closeModal` functions. The `openModal` function also accepts some paramters for content that should be rendered in the Modal.

[Flems demo](https://flems.io/#0=N4IgzgpgNhDGAuEAmIBcICGAHLA6AVmCADQgBmAljEagNqgB2GAthGiLgBbzNQkiwA9g0Qj2IAL7FGLNumaCkGKASKkhIiGPQB6AFR6AOgwAC8AJ5ZkEMgAJgwY4fjwK8GKlth4AJwoMAcyd4DVF4AH5PDAZzYIAjAFcXYTBI+wokT28-QIBuW0QAD3gs338AiVoAXWCfZB8MAHc0uMFBGGjg4VgoClgAa3CACgzSnICASk8AN0EM4wkJWwB5LFcU4z0dJwZ9PVszSwh7VfWGMCWt4xh4W0E1ihTbAF57V3cITwByAFlFZS+EmuEFuFDAqy0L1sZGUkB2e2Mtn2JiwGAazBODxSS3u8DAiKuDAghSwgh8tzICQYCEeDDuVgYfyUUFsQ1xYAm9mCOh0tgAIhAIFhbEIsOYCpxjmAEjhesh6XjgmCIXTXr4EhAuljzlDgLh9eygQxnLjaWBcIlkjrXuyLUl4E9wgrzZaHedcMxsEM4i8AHysvX6uISCaczzVbm8gBK9Satji4qQNgwCSgtyp1DAIuUMB8XhlWDqYEgSCVdjZ2vNdSQDUaL1eDFTLIAPs2FWbcNXa-W1T4NZzHMb4M5mJ3Y40hhNgkajcY9rYAMI5rNduMJ2xJmGp9MMTPZqAwJC2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0tUFUPwbe7AwCwD8GsSQVQJBAA)

See the `imperative` directory in this repo for sources.

### Caveats

While you can open a modal from anywhere in your app, this approach requires that you render the Modal component itself in a root-level component. This implementation can only render one modal at a time (though it could be developed further to handle a stack of modals.)

## Declarative (Portal) Style

This technique uses a portal-like technique to append a DOM node to the end of the document body and temporarily mounts a separate vnode tree there. When the modal is removed, that DOM node and vnode tree are cleaned up.

This allows you to declaratively render a modal component nested anywhere within your vdom.

[Flems demo](https://flems.io/#0=N4IgtglgJlA2CmIBcBWAzAOgOwCYA0IAZhAgM7IDaoAdgIZiJIgYAWALmLCAQMYD21NvEHIQIAL54a9RiACyfKLVgYAVuV4ChIpv2qk2AAgVLYhgLyHCAV2o82EAQAoAlIeAAdaocMIjUPjAvDzY-Qx4WEigAJ2Fg70MAekTDAGEtWghqeGjwwIAHAWEjAHd4QzA+WyM2PkNaQ2i+PjYAWgQAN3gzABEAeTlDakV4Lx89A2NFZXTBTOzcy09qEJCOiHgSpENXCwA+cMjYGLiVtnF4n1i2a2jvZZ8fAR5Y2iEnDrcHx-Gjk+9LB0MBEorEVgkfslDABBfL5YRQeoVaZmPRseY5Qy1LEscoIwx8QiGABGigAnmMfoYAmALNS+DxrAxBMDXkIAKIIZlsJwAcigEA6vJclMhKQAKrjkaZwrBaKRSIYWPKkYRaFB4K0svVqBAwG9HOCqfSwMC5QqAHIyOm8yqmXmix4BRncjCkqBkjC0OEI1J-Jw0kUQx5QhTVJGkeD5WjRN7lABq-UGbFi5VxsUdPlNlWqAcCeCmplm6KyOSDP0kjoExPghD4sWs+SU70+7kzh1BwjpQJBxzBjsrwertfrOXglS62w+ww1bnMB2+YphMCRPHNipKEDYLCs6s1VSM8AAHludXqDQJ2zSzfLSAAZCAGL0wPmRDXC9vXW7ebIlQwABSaSBIycRYF3bHxr3VKB2S6QQHwMYQcj5WhdX1BwimoKBeQLaJyypcR8J8QcfgEWIJ3gXZFxDFIengAwmjJHFyjtZQTSxVMMEMABVUgsgAcwqDAc0EQx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gVS0IXmoNo4R2oEn9KwdQF2phWDQHKN8VC54tG7jIaIiZAyvDwOmepYp2gjDfCEEeNo1T+IjPCM0gxKDb6GAwbSAALOOQwaAnmPLAO4zx3iLJ+P4IE7YABiAAYsC9xfAjw-2VAEHqEynkwtpHcww-yACcKLQXRA1EExFqRsWGDGjAs2AToBwMnt0ae5U55jXuUgMaEgAC6BB1rUGMpQdeshIDbj-lwAgtwuBMHYGwfIjTki2HyF44EgREjspYJy7BY0MByripKrc0qSBZXCpnWQpAXgQHyHvcQtLxBAA)

See the `declarative` directory in this repo for sources.

### Caveats

The "portal" approach may have trouble if you're rendering the body tag itself with Mithril, and if the top-level dom nodes within the body are changing while the modal exists. Otherwise, it should safely co-exist with the rest of your Mithril DOM/VDOM.

## Declarative composed from re-usable parts

We might want to create a less opinionated, more re-usable declarative `Overlay` component that relies on this portal-like behaviour. Then render a `Modal` component (or anything else) within that. Here is a [Flems demo](https://flems.io/#0=N4IgtglgJlA2CmIBcBWAzAOgOwCYA0IAxgPYB2AzsQskVbAIYAO58UIBAZhAucgNqhS9MIiQgMACwAuYWO1qkp8RTRABfPIOGiQAeQBu8AE4MAnhgBWvAiUXKpNAPQAqZwB1SAFQkRyAAgNjMz8SMEYyez9GI2J9aHh-ej8OAFdYWABackIjeGUQ2OM-eAQRRQwPAEkpP18-MGIUuyg-KWI-JJZGeiN6JT8ANUcAEV0AWVbc+A7GRmUoVlb2qQlpgCNiKHMPAGEfWChc0j9yFNnYCEW2gMMTelMO3L8jhdyWiEVln38pKYqvVY3IL3AphCKKEL0Y5raakBJKFpQ0wAd1WT2REBWHz8pkaRhmjAA5OQPHF4Mi-GsUjUMelKdMXsYru0oL5GMFCkY2oxirdTFjSABzYqwFj-ZyODweWzkGqBO4PAC8ySahCkEDIAAoAJR+YBSqQIGpQYhgA1GkL7Q7KKWkNxSGVy25mHZkKT0D5FZX6u1Se1k5FIPw6vyKgB8lu41rtvrUtvtuSkKSMxx99vtZBy8D68E1+l1ab9RcIVqO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k1EULBcKOd2S0WzTY+yAkHMOQ6kRnzbFXaJkDyy-AADJ4t-OFbhdj5j6HN24wACRKJvJBs1xH8fMVGqtpHtZ-3WLx+klGNZcLxUVuRaagwCB3oSCBLAfXLtK3Vwkagl74fMVq7eBq+EEpi+JhSoppMarnXI61Q7t3ruPbaFqm1sKziOpF3Cn6-wPuhtoGgVZjyvrdPYGgkXyG6SMIKD4QZAYAG4iHdBgE1O7j3-xUUUBkSukA+VjyhOQLIxgIAcHewBT7UgsgQAAF6ZavWDRgal3tPQ8K+Kz6rnusiFEGHAxTke2gAALC1MPM7QStFLwAyNidV8zTR6mIN0QgmJTBvbtdcYADOq7M6DFe+7y2YxE-gCT3oIhydKRUuQ2nU4OeM+534XnbP2gy65+W1np8YwYAqg8DHW9MT6SDFwAxUAwdRze0QhAHBZ4PaIaLFYPPAaAwAKQm9WP1aQ9uncm7pdrQUMQmhQDDVQaiQZrt0s1DgFAKA8AR6jzHvAgNsAoG1Cbht7J7gG4QEjohFhCbqg4KYQPdhFCtvsMYE3s1+qkCp0oMAw8rZ2CMOX7Stj9cKSUtiBPKByD48NlryQtN1UYovQbinylqR+E7939XqPDPquXRkCvgpSAl4bybzHr2-C46nP9Ypfg0C7532AL39LfdgQDyQTeRggwAGIABi9+vfEDUlkCQilC53YwCgXfnfd9b78NfgATiAMfyMFeBvx2AgPl0RxOCoGgB7xn2WQyDTUux6Buyr25B5zxwAnVxDQTV438ETRQOu1uwnwn0-zx3V1PQ+gvW+hADQBQCQCvXUH6xAAuFIGWn4EfR0EgBWAnjkAIGTDkDEGkCkGYCQEbCaEYEWkFCrFNEcB4IkD4IJyvQwBULBnkMlj4JfQIE4x0CZggEYAcD6zUCAA) that uses this technique.

See the `declarative-composed` directory in this repo for sources.

*Note that the examples use ES6 and modern CSS. Sources would need to be compiled to ES5 and include style prefixes for older browsers.*