npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-autofocus

v0.0.3

Published

Automatically focus on an input field upon page load in Ember

Downloads

3

Readme

##ember-autofocus 0.0.3

Automatically focus on an input field upon page load in Ember

// myView.handlebars
   :
{{ input autofocus="true" }}
   :
{{ autofocus }}
{# end of file #}

Motivation

Setting the keyboard focus on a specific input element is a quite standard UI behaviour but rather tedious to accomplish in Ember.

Gladly, there’s this very useful recipe on the Ember web site on how you’re supposed to do that; basically, you set the focus after the didInsertElement Ember event fires - done! This, this and this Stackoverflow answer propose similar techniques.

Unfortunately, the proposed solutions require subclassing into your own Views, which I do not like so much for various reasons.

Thus, Change!

Solution

ember-autofocus bundles Ember's proposed technique into a neat package and adds a few improvements along the way:

  • Just add water. No coding. Load it, boom, hooray!
  • No more subclassing. Because, subclassing.
  • Works on any element. If you pass it a CSS selector.
  • No configuration. If you don't pass a CSS selector it takes the first text input it finds.
  • HTML5 compatible. Uses the native autofocus attribute if available.
  • HTML<5 fallback. No HTML5, no problem.
  • Tiny. 0.000000670552254 Gigabytes.
  • It’s really elegant. Elegance is a glowing inner peace.

How it works

  • Installs an autofocus attribute for InputField for HTML5 compatibility
  • Adds an {{autofocus}} View that uses jQuery to set the focus
  • learn from the source, it’s really minimal

Installing

Bower

bower install ember-autofocus

In your index.html:

<script type="text/javascript" src="bower_components/ember-autofocus/dist/ember-autofocus.min.js)"></script>

Usage

Most compatible (recommended)

  • Sets the focus on the first <input|button|textarea> element with an autofocus attribute.
  • Uses the browser’s native HTML5 functionality if available.
  • Works in pre-HTML5 browsers.
// myView.handlebars
   :
{{ input autofocus="autofocus"}}
   :
{{ autofocus }}
{# end of file #}

Laziest

Autofocus on the first <input> element.

// myView.handlebars
   :
{{ input }}
   :
{{ autofocus }}
{# end of file #}

Most Sophisticated

Use a CSS selector to autofocus on a specific element.

// myView.handlebars
   :
{{ autofocus on='#my-special-element .my-unique-class' }}
{# end of file #}

Automatic scrolling in HTML5

Safari, Chrome, Firefox and possibly other HTML5 browsers that support autofocus natively will set the keyboard focus to the specified field and will automatically scroll there. In my use case, that's not wanted because the input field may be below the screen fold and automatically scrolling there is not what users would expect.

You can choose whether or not you want the scrolling behavior with the scrolling option:

scrolling = "true|false"

The default is false, which means that the browser will not scroll to the autofocus element.

// myView.handlebars
   :
{{ autofocus scrolling="true" }}
{# end of file #}

Caveats

  • Will focus even on hidden/invisible items. Pull Requests welcome.

Thanks

I have found some very valuable advice during my research which I have built into ember-autofocus:

License

Copyright (c) 2014 Andreas Pizsa. Released under an MIT license.


Made with ❤ in Vienna, Austria.