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-resize

v2.0.0

Published

NgxResize Service

Downloads

5,330

Readme

NgxResize

A service that emits changes of a DOM container on resize by utilizing Resize Observer

Installation

npm install ngx-resize

Usage

There are two approaches of using ngx-resize. Both approaches results an Observable<NgxResizeResult>

export interface NgxResizeResult {
    readonly entries: ReadonlyArray<ResizeObserverEntry>;
    readonly x: number;
    readonly y: number;
    readonly width: number;
    readonly height: number;
    readonly top: number;
    readonly right: number;
    readonly bottom: number;
    readonly left: number;
    readonly dpr: number;
}

injectNgxResize()

If you're on Angular 14+ and are using inject() for Dependency Injection, you can use injectNgxResize() to grab the Observable<NgxResizeResult>

@Component({})
export class SomeComponent {
    readonly resizeResult$ = injectNgxResize(); // Observable<NgxResizeResult>
}

injectNgxResize() accepts a Partial<NgxResizeOptions> and will be merged with the default global options.

export interface NgxResizeOptions {
    /* "box" options that is passed in new ResizeObserver */
    box: ResizeObserverBoxOptions;
    /* time in ms to debounce the events; single number for resize; can also support scroll and resize separately */
    debounce: number | { scroll: number; resize: number };
    /* whether to observe resize on scroll */
    scroll: boolean;
    /* use offset size instead */
    offsetSize: boolean;
    /* emit in NgZone or not. Default to "true" */
    emitInZone: boolean;
    /* emit the initial DOMRect of nativeElement. Default to "false" */
    emitInitialResult: boolean;
}

export const defaultResizeOptions: NgxResizeOptions = {
    box: 'content-box',
    scroll: false,
    offsetSize: false,
    debounce: { scroll: 50, resize: 0 },
    emitInZone: true,
    emitInitialResult: false,
};

With Output

Instead of getting the Observable<NgxResizeResult>, you can assign injectNgxResize() to an Output directly

@Component({})
export class SomeComponent {
    @Output() resize = injectNgxResize(); // resize emits everytime NgxResize emits
}
<some-component (resize)="onResize($event)"></some-component>
<!-- $event is of type NgxResizeResult -->

NgxResize

If you're not using inject(), you can use the NgxResize directive

<some-component (ngxResize)="onResize($event)"></some-component>
<some-component
    (ngxResize)="onResize($event)"
    [ngxResizeOptions]="optionalOptions"
></some-component>

With hostDirectives

With Angular 15, you can also use NgxResize as a hostDirectives and expose ngxResize Output

@Component({
    hostDirectives: [{ directive: NgxResize, outputs: ['ngxResize'] }],
})
export class SomeComponent {
    @HostListener('ngxResize', ['$event'])
    onResize(event: NgxResizeResult) {
        // listen for resize event from NgxResize
    }
}

Credit to @denisyilmaz for this usage

Provide global NgxResizeOptions

You can use provideNgxResizeOptions() to provide global options for a specific Component tree. If you call provideNgxResizeOptions() in bootstrapApplication() (for Standalone) and AppModule (for NgModule) then the options is truly global.

Contributions

All contributions of any kind are welcome.