Last active
December 20, 2018 14:08
-
-
Save z5h/dde93737ed962ae2eff12001c6155ee7 to your computer and use it in GitHub Desktop.
autofocus in Elm that actually works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
customElements.define('focus-callback', class extends HTMLElement { | |
constructor(){ | |
super() | |
this._callBackId = null; | |
} | |
set callBackId(value){ | |
this._callBackId = value; | |
} | |
connectedCallback() { | |
try { | |
document.getElementById(this._callBackId).focus(); | |
} catch (error) { | |
// skip | |
} finally { | |
this.dispatchEvent(new CustomEvent('attempted')); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input [ | |
focus (item.id) (RequiresFocus_ Nothing) | |
else | |
none | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
focus elementId msg = | |
Element.el [ Element.transparent True ] | |
(Html.node "focus-callback" | |
[ Html.Attributes.property "callBackId" (Encode.string elementId) | |
, Html.Events.on "attempted" (Decode.succeed msg) | |
] | |
[] | |
|> Element.html | |
) | |
|> Element.onRight | |
none = | |
Html.Attributes.style "" "" |> Element.htmlAttribute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment