Skip to content

Instantly share code, notes, and snippets.

@swalters
Last active November 15, 2016 16:36
Show Gist options
  • Save swalters/8d8772d20ee375df61277d5b048e6eb2 to your computer and use it in GitHub Desktop.
Save swalters/8d8772d20ee375df61277d5b048e6eb2 to your computer and use it in GitHub Desktop.
Aurelia auto-focus attribute that I use with Aurelia Materialize bridge
import {inject, customAttribute, TaskQueue} from 'aurelia-framework'
@customAttribute('auto-focus')
@inject(Element,TaskQueue)
export class AutoFocus {
constructor(private element:Element, private taskQueue:TaskQueue) {
}
attached(){
this.taskQueue.queueTask(() => {
var inputs = this.element.getElementsByTagName('input');
if(inputs.length > 0){
let input:HTMLInputElement = inputs[0] as HTMLInputElement;
input.focus();
let label = input.nextElementSibling as HTMLLabelElement;
if (label.nodeName === "LABEL") {
this.taskQueue.queueTask(() => { label.classList.add("active"); });
}
}
else {
console.warn('No input element found for auto-focus');
}
});
}
}
@MaximBalaganskiy
Copy link

When a focused input has a label it does not move on text entry.
image

I had to add another task to set the class on it

input.focus();
let label = input.nextElementSibling as HTMLLabelElement;
if (label.nodeName === "LABEL") {
	this.taskQueue.queueTask(() => { label.classList.add("active"); });
}

@swalters
Copy link
Author

updated per @MaximBalaganskiy comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment