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

ngx-router-outlet-class

v1.0.0

Published

A directive to add CSS class(es) to the activated/attached component's host element (hereinafter, the `route element`) of a router-outlet.

Downloads

31

Readme

NgxRouterOutletClass

A directive to add CSS class(es) to the activated/attached component's host element (hereinafter, the route element) of a router-outlet.

Reason

Ever wonder why the class(es) set on router-outlet is not applied to the route element?

This is because, in Angular, the router-outlet is not directly replaced by the route element, while in some other frameworks, the route element replaces the router outlet/view placeholder. In Angular, the route element is inserted next to the router-outlet.

You may wish to add a uniform class(es) to each route element based on the overall layout and styling to form a uniform look for your application regardless of the route's path. It would be less work if you could bind class(es) to router-outlet and add the class(es) on the fly to each route element instead of adding class(es) to them individually.

This directive adds class binding to the router-outlet to the route element. It is a workaround for the missing functionality.

Usage

Simply install and import the module.

Then set or bind a string to the componentClass attribute of the router-outlet element, just like you would do to the class attribute of any native element. The directive will automatically add the class(es) to the route element.

A dynamicClassGetter function attribute is also provided to get the dynamic class(es) for the route element. This function should take the activated component as the only parameter and return the class(es) as a string.

Install

npm install ngx-router-outlet-class

Import

import { NgxRouterOutletClassModule } from 'ngx-router-outlet-class';

@NgModule({
  imports: [
    NgxRouterOutletClassModule,
  ],
})
export class AppModule {
}

Use

Component Class Binding

You can simply set the componentClass attribute to a string, which will be added to the route element's class list.

  • Static class(es)

<router-outlet componentClass="content-container"></router-outlet>
  • Dynamic class(es)

<router-outlet [componentClass]="componentClass" #myOutlet="outlet"></router-outlet>
class AppComponent {

  @ViewChild('myOutlet') myOutlet: RouterOutlet;

  get componentClass() {
    return `content-container ${
      this.myOutlet.component?.isAboutComponent
        ? 'theme-aero'
        : 'theme-violet'
    }`;
  }

}

High Performance Dynamic Class(es)

Performance has already been taken into consideration when handling bound value changes.

But in the dynamic component class binding above, there is still a performance issue, since the addition and deletion of class(es) happens not only on change of the bound value, but also (inside the directive) on change of the component.

Use dynamicClassGetter function attribute to solve the issue, with componentClass used for static or less changeable class(es) only.

This function is only called by the directive when the component changes, making it a high-performance way to get and apply the component-specific class(es).

Here's an example of how to use it:


<router-outlet [dynamicClassGetter]="myClassGetter"></router-outlet>
class AppComponent {

  myClassGetter(component: any) {
    return component?.isAbout
      ? 'theme-aero'
      : 'theme-violet';
  }

}

Limitation

Because the private property activated of RouterOutlet is used in the directive, there is no guarantee that the directive will work in the future versions of Angular.

Upvote My Feature Request

If you find this workaround helpful, please vote my feature request for the Angular team, then we may have built-in functionality in the future.