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-component-focus

v0.2.3

Published

An Ember addon for managing focus within components.

Downloads

81

Readme

Ember Component Focus

This ember-cli addon provides a mixin for adding methods to your Ember components that help you manage the currently focused element.

Proper focus management is essential for making dynamic single-page applications accessible to screen readers and other assistive technologies. For example, if you're writing a todo app, you might want to move focus from a creation form to a new todo item when the user submits the form. This addon makes it easy to move focus within a component by handling issues like setting tabindex on elements that aren't focusable by default and removing tabindex="-1" on blur.

Installation

Run the following inside your Ember application to install this addon.

ember install ember-component-focus

Or you can install directly from npm:

npm install --save ember-component-focus

Demo

This todo app makes use of ember-component-focus to create an accessible UI.

Usage

To use the focus management methods in your component, you need to mix-in the focusable-component mixin:

// app/components/your-component.js
import Ember from 'ember';
import FocusableComponent from 'ember-component-focus/mixins/focusable-component';

export default Ember.Component.extend(FocusableComponent, {
  // Your component's definition...
});

The mixin adds two properties and two methods to your component.

focusNode Property

The focusNode property allows you to specify the selector of one of your component's child elements that you want to receive focus when one of the methods added by the mixin is invoked. It defaults to null, so override it in your component's definition if you want to set a default element to focus.

componentFocusManager Property

This is a reference to the Focus Manager service that handles interaction with the DOM and listening for DOM events. You probably won't need to use it directly.

focus() Method

The focus() method sets focus on a child element immediately. The element to focus defaults to the value of focusNode, but you can also pass in the child element to focus or a string selector for the element to focus. If you don't pass anything and focusNode is null, focus will move to the component's top element (component.element). This method returns the element that ended up receiving focus.

Example

// When this component is first inserted into the DOM, it will set focus to its
// header element.
export default Ember.Component.extend(FocusableComponent, {
  focusNode: 'h1',

  // ...

  didInsertElement() {
    this.focus();
  },

  // ...
});

focusAfterRender() Method

This method works just like focus, accepting the same arguments, but it schedules setting focus for after the next render cycle (using the Ember Run Loop's afterRender queue). This method is most useful for when you want to move focus to a child element that is not yet rendered but will be after the next render cycle. Simply pass it the selector of the element to be rendered. It returns a promise that will be resolved with the element that ends up receiving focus.

Example

// This component will focus the element for a new todo after the model object
// for that todo has been saved and the element representing the todo has rendered.
export default Ember.Component.extend(FocusableComponent, {
  actions: {
    addTodo() {
      let todoName = this.get('todoName');
      let todo = this.store.createRecord('Todo', {name: todoName});
      todo.save().then(() => this.focusAfterRender(`[data-id=todo-${todo.id}]`));
    }
  },

  // ...
});

License

Copyright 2015 LinkedIn Corp. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

Notice

ember is a trademark of Tilde Inc. and is used with permission.