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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eurusik/ngx-autofocus

v1.0.4

Published

Intelligent autofocus directive for Angular that works everywhere — even on iOS!

Readme

🔍 NgxAutofocus

✨ Features

  • 🚀 Simple to use — just add the ngxAutofocus directive to any element
  • 📱 Works on iOS — special iOS handler solves common focus issues
  • 🎯 Smart focusing — supports conditional focus and dynamic content
  • 🎭 Animation aware — automatically waits for animations to complete
  • Synchronous mode — optional immediate focus without delays
  • 🧪 Fully tested — comprehensive test coverage for reliability

📦 Installation

npm install @eurusik/ngx-autofocus --save

or

yarn add @eurusik/ngx-autofocus

Compatibility

This library is compatible with Angular 18.0.0 and above. It uses modern Angular features including the standalone components architecture and the new Input API introduced in Angular 18.

🚀 Quick Start

Import the directive

import { NgxAutofocusDirective } from '@eurusik/ngx-autofocus';

@Component({
  // ...
  standalone: true,
  imports: [NgxAutofocusDirective],
})
export class YourComponent {}

Basic usage

<!-- Simple autofocus -->
<input ngxAutofocus>

<!-- Conditional autofocus -->
<input [ngxAutofocus]="shouldFocus">

🔧 Advanced Features

Focus Handlers API

NgxAutofocus comes with three specialized handlers to manage different focus scenarios:

1. NgxDefaultAutofocusHandler

The default handler provides smart focus with animation awareness:

  • Automatically waits for Angular animations to complete
  • Uses a race condition between animation detection and timeout
  • Runs outside Angular zone for better performance
import { NGX_AUTOFOCUS_HANDLER, NgxDefaultAutofocusHandler } from '@eurusik/ngx-autofocus';

@Component({
  // ...
  providers: [
    {
      provide: NGX_AUTOFOCUS_HANDLER,
      useClass: NgxDefaultAutofocusHandler
    }
  ]
})

2. NgxIosAutofocusHandler

Specialized handler for iOS devices that solves common focus issues:

  • Creates a temporary invisible input to trigger the keyboard
  • Copies relevant attributes from the original input
  • Uses precise timing to overcome iOS focus limitations
  • Properly cleans up temporary elements

This handler is automatically used when an iOS device is detected.

3. NgxSynchronousAutofocusHandler

Simplified handler for immediate focus without delays:

import { NGX_AUTOFOCUS_HANDLER, NgxSynchronousAutofocusHandler } from '@eurusik/ngx-autofocus';

@Component({
  // ...
  providers: [
    {
      provide: NGX_AUTOFOCUS_HANDLER,
      useClass: NgxSynchronousAutofocusHandler
    }
  ]
})

Custom Focus Options

Configure focus behavior with custom options:

import { NGX_AUTOFOCUS_OPTIONS } from '@eurusik/ngx-autofocus';

@Component({
  // ...
  providers: [
    {
      provide: NGX_AUTOFOCUS_OPTIONS,
      useValue: { 
        delay: 300,             // Delay in ms before focusing
        preventScroll: true,    // Prevent automatic scrolling
        query: 'input, button'  // Custom query for finding focusable elements
      }
    }
  ]
})

API Reference

| Input | Type | Default | Description | |-------|------|---------|-------------| | ngxAutofocus | boolean | true | Controls whether the element should be focused |

Creating Custom Handlers

You can create your own focus handler by extending the AbstractNgxAutofocusHandler class:

import { ElementRef } from '@angular/core';
import { AbstractNgxAutofocusHandler, NgxAutofocusOptions } from '@eurusik/ngx-autofocus';

export class MyCustomHandler extends AbstractNgxAutofocusHandler {
  constructor(
    protected override readonly el: ElementRef<HTMLElement>,
    protected override readonly options: NgxAutofocusOptions
  ) {
    super(el, options);
  }

  public setFocus(): void {
    // Your custom focus implementation
    console.log('Custom focus logic');
    this.element.focus({ preventScroll: this.options.preventScroll });
  }
}

🌟 Demo

Online Demo

View the live demo at https://ngx-autofocus.vercel.app/

Note for iOS users: Due to a WebKit limitation, programmatic focus on iOS doesn't automatically trigger the keyboard. Elements will receive focus correctly, but you'll need to tap once to make the keyboard appear. Apple is working on fixing this in future iOS versions.

Local Development

The project includes a demonstration application showcasing various NgxAutofocus scenarios:

git clone https://github.com/eurusik/ngx-autofocus.git
cd ngx-autofocus
npm install
npm start

Open your browser and navigate to http://localhost:4200/.

iOS Demo

The demo includes a special iOS simulation mode that demonstrates how the iOS handler works:

  • Shows the temporary input element creation process
  • Visualizes the focus sequence
  • Allows testing iOS behavior on non-iOS devices

🧪 Testing

The directive has comprehensive test coverage to ensure reliability across different scenarios:

npm test

🤝 Contributing

Contributions are welcome! Please create an issue or pull request on GitHub: https://github.com/eurusik/ngx-autofocus

📄 License

MIT