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-linked-elements

v1.2.0

Published

LinkedElements is an angular library for link elements within a page (eg. sections, comments, articles, etc).

Downloads

2

Readme

LinkedElements

LinkedElements is an angular library for link elements within a page (eg. sections, comments, articles, etc).

Getting Started

Installation

npm install ngx-linked-elements
yarn add ngx-linked-elements
pnpm add ngx-linked-elements

Example Usage

Import the default module within your module

my-module.component.ts

import { LinkedElementsModule } from 'ngx-linked-elements';
import { MyComponent } from './my-component.component';

@NgModule({
	declarations: [MyComponent],
	imports: [LinkedElementsModule],
})
export class MyModule {}

Then, you will need to declare LinkedElementsDirective within your component template

my-component.component.ts

<div class="..." ngxLinkedElements></div>

Or, in Angular 15 you can declare LinkedElementsDirective within your component decorator instead.

my-component.component.ts

import { LinkedElementsDirective } from 'ngx-linked-elements';

@Component({
    ...,
    templateUrl: 'my-component.component.html',
    hostDirectives: [LinkedElementsDirective, ...]
})
export class MyComponent {}

Now you are ready to define linked elements and link triggers

Click based links

my-component.component.html

<button ngxLinkTo="section-1">I am a link to section 1</button>
<button ngxLinkTo="section-2">I am a link to section 2</button>

<section ngxLinkedElement="section-1" style="height: 500px;">
	<h3>Section 1</h3>
	<p>...</p>
</section>
<section ngxLinkedElement="section-2" style="height: 500px;">
	<h3>Section 2</h3>
</section>

You can have multiple links for the same linked element but just one linked element register with the same name

<button ngxLinkTo="article-1">I am a link to article-1</button>
<button ngxLinkTo="article-1">I am a link to article-1 too!</button>

<article ngxLinkedElement="article-1" style="height: 200px;">
	<span>...</span>
</article>

Route based links

You can define linked elements and scroll to them based on the route.

First, you will have to provide the LinkedElementsRouterDirective and a valid RouteListener directive strategy (eg: FragmentListenerDirective).

my-component.component.ts

import { LinkedElementsDirective, LinkedElementsRouterDirective } from 'ngx-linked-elements';

@Component({
    ...,
    templateUrl: 'my-component.component.html',
    hostDirectives: [
			LinkedElementsDirective, 
			LinkedElementsRouterDirective, 
			FragmentListenerDirective
		]
})
export class MyComponent {}

Now, each time that the fragment route changes, the document will scroll to the matched LinkedElement.

<a routerLink="./" fragment="section-1">I am a link to article-1<a>
<section ngxLinkedElement="section-1" style="height: 500px; margin-top: 100vh;">
	<h3>Section 1</h3>
	<p>...</p>
</section>

Advanced

Most of the features of the library are highly customizable and can be overwritted by providing the tokens withing your module.

So, most of the directives are standalone so you can import them separaretly.

Also, the LinkedElementsModule is just a wrapper for the default providers and directives.

Changing the scroll strategy

You could change the scroll strategies by providing the token and a class that implements the BaseScrollStrategy.

For example, you could want to change the way that the first scroll is made.

my-module.component.ts

import {
	LinkedElementDirective,
	LinkToDirective,
	LinkedElementDirective,
	PrimaryScrollStrategy,
	SecondaryScrollStrategy,
	SmoothScrollStrategy,
} from 'ngx-linked-elements';
import { MyComponent } from './my-component.component';

@NgModule({
    ...
    imports: [
		...,
		LinkedElementDirective,
		LinkedElementsDirective,
		LinkToDirective,
		...
    ],
    providers: [
        ...,
        { provide: PrimaryScrollStrategy, useClass: SmoothScrollStrategy },
        { provide: SecondaryScrollStrategy, useClass: SmoothScrollStrategy }
    ]
})
export class MyModule {}

PrimaryScrollStrategy represents the principal strategy used to scroll when the route changes or an LinkToDirective is clicked.

SecondaryScrollStrategy represents the secondary strategy used at the page load.

SmoothScrollStrategy is the strategy that scrolls slowly and smoothly to a given LinkedElement.

This provider configuration will make it scrolls scroll smoothly to the route matched LinkedElement at the page load and each time a route changes or an LinkToDirective is clicked.