Skip to content

Instantly share code, notes, and snippets.

@z5h
Last active December 20, 2018 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z5h/dde93737ed962ae2eff12001c6155ee7 to your computer and use it in GitHub Desktop.
Save z5h/dde93737ed962ae2eff12001c6155ee7 to your computer and use it in GitHub Desktop.
autofocus in Elm that actually works
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'));
}
}
});
input [
focus (item.id) (RequiresFocus_ Nothing)
else
none
]
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