infinnie / infinnie.github.io

https://infinnie.github.io/
Apache License 2.0
0 stars 1 forks source link

hyperapp createPortal #14

Closed lifeiscontent closed 4 years ago

lifeiscontent commented 6 years ago

Hi @infinnie I read your post here: https://medium.com/@joanxie/use-hyperapp-enjoy-javascript-again-ab9633f7066f

I was curious if you had a solution to dealing with displaced content within hyperapp.

infinnie commented 6 years ago

Yes.

infinnie commented 6 years ago

See also https://github.com/hyperapp/hyperapp/issues/637

infinnie commented 6 years ago

An example:

var showDialog = (function () {
    var root = $("<div>").addClass("portal").appendTo("body"), dlg = app({
        text: "Default text"
    }, {
        changeText: function (text) {
            return { text: text };
        }
    }, function (state) {
        return (<div class="dialog portal__dialog">
            <p class="dialog__text">{state.text}</p>
            <div class="dialog__buttons">
                <button type="button" class="btn btn--primary" onclick={ function () {
                    root.trigger("ok"); } }>OK</button>
                <button type="button" class="btn btn--secondary" onclick={ function () {
                    root.trigger("cancel"); } }>Cancel</button>
            </div>
        </div>);
    }, root.get(0));
    return function (text) {
        dlg.changeText(text);
        root.addClass("portal--active");
        return new Promise(function (resolve, reject) {
            root.on("ok", function () {
                root.off("ok").off("cancel");
                resolve();
            }).on("cancel", function () {
                root.off("ok").off("cancel");
                reject();
            });
        });
    };
})();
lifeiscontent commented 6 years ago

Wow! Thank you so much! You rock @infinnie 👍