|
| 1 | +# @ngx-href [](https://www.npmjs.com/package/ngx-href) |
| 2 | + |
| 3 | +[](https://www.buymeacoffee.com/widness) |
| 4 | + |
| 5 | +A library that allows href to understand Angular's router while retaining its default functionality. |
| 6 | +1. Use `router.navigate()` for [internal link](#angular-routing) |
| 7 | +2. Support scroll with the `#` attributes and let you configure the [scrolling logic](#scroll-logic) |
| 8 | +3. Automatically append `rel="nooepener"` & `target="_blank"` to external link [if wished so](#installation) |
| 9 | + |
| 10 | +- [@ngx-href ](#ngx-href-) |
| 11 | + - [Installation](#installation) |
| 12 | + - [Angular routing](#angular-routing) |
| 13 | + - [Scroll logic](#scroll-logic) |
| 14 | + - [Behavior](#behavior) |
| 15 | + - [defaultOffset](#defaultoffset) |
| 16 | + - [navbarOffset](#navbaroffset) |
| 17 | + - [External link](#external-link) |
| 18 | + - [Rel attribute](#rel-attribute) |
| 19 | + - [Target attribute](#target-attribute) |
| 20 | + - [target attribute](#target-attribute-1) |
| 21 | + - [Usage](#usage) |
| 22 | + - [Directive](#directive) |
| 23 | + - [Service](#service) |
| 24 | + - [Authors and acknowledgment](#authors-and-acknowledgment) |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +```sh |
| 29 | +npm install ngx-href |
| 30 | +``` |
| 31 | + |
| 32 | +Inside your `app.module.ts` file. |
| 33 | +```typescript |
| 34 | +import { ngxHrefModule, ngxHrefService } from 'ngx-href' |
| 35 | + |
| 36 | + imports: [ |
| 37 | + /** Default |
| 38 | + * behavior="smooth" |
| 39 | + * defaultOffset="30" |
| 40 | + * navbarOffset="60" |
| 41 | + * rel="noopener" |
| 42 | + * target="_blank" |
| 43 | + **/ |
| 44 | + ngxHrefModule.forRoot({}), |
| 45 | + |
| 46 | + // Or |
| 47 | + ngxHrefModule.forRoot({ |
| 48 | + behavior:"instant", |
| 49 | + defaultOffset:"30", |
| 50 | + navbarOffset:"60", |
| 51 | + rel:"noopener nofollow", |
| 52 | + target:"_self", |
| 53 | + }), |
| 54 | + ], |
| 55 | +``` |
| 56 | + |
| 57 | +## Angular routing |
| 58 | +Nothing to do it should work out of the box |
| 59 | + |
| 60 | +## Scroll logic |
| 61 | +### Behavior |
| 62 | +**Default:** `"smooth"` |
| 63 | +**Accepted value:** `ScrollBehavior` // ("auto" | "instant" | "smooth") |
| 64 | + |
| 65 | +Can also be passed individually directly through html |
| 66 | +```html |
| 67 | +<a href="https://my-external-url.com" behavior="instant"> |
| 68 | +``` |
| 69 | + |
| 70 | +### defaultOffset |
| 71 | +The standard offset to be added to your website `scrollTo` logic |
| 72 | + |
| 73 | +**Default:** `30` |
| 74 | +**Accepted value:** `number` |
| 75 | +Together with the `navbarOffset` will be the total offset for the scroll. |
| 76 | + |
| 77 | +### navbarOffset |
| 78 | +An additional offset calculated base on your navbar height |
| 79 | + |
| 80 | +**Default:** `60` |
| 81 | +**Accepted value:** `number` |
| 82 | +Together with the `defaultOffset` will be the total offset for the scroll. |
| 83 | + |
| 84 | +You can update this value after the navbar is rendered. |
| 85 | + |
| 86 | +```html |
| 87 | +<navbar #navbar> |
| 88 | + <!-- My html code --> |
| 89 | +</navbar> |
| 90 | +``` |
| 91 | + |
| 92 | +```typescript |
| 93 | +@ViewChild('navbar', { static: true }) navbar: ElementRef |
| 94 | + |
| 95 | +constructor( |
| 96 | + private _ngxHrefService: NgxHrefService, |
| 97 | +) {} |
| 98 | + |
| 99 | +ngAfterContentInit(): void { |
| 100 | + this._ngxHrefService.navbarOffset = this.navbar.nativeElement.offsetHeight |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | +## External link |
| 106 | +### Rel attribute |
| 107 | +**Default:** `"noopener"` |
| 108 | +**Accepted value:** [string](https://developer.mozilla.org/fr/docs/Web/HTML/Attributes/rel) |
| 109 | + |
| 110 | +Can also be passed individually directly through html |
| 111 | +```html |
| 112 | +<a href="https://my-external-url.com" rel="noopener nofollow"> |
| 113 | +``` |
| 114 | + |
| 115 | +### Target attribute |
| 116 | +**Default:** `"_blank"` |
| 117 | +**Accepted value:** [string](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target) |
| 118 | + |
| 119 | +Can also be passed individually directly through html |
| 120 | +```html |
| 121 | +<a href="https://my-external-url.com" target="_self"> |
| 122 | +``` |
| 123 | + |
| 124 | +### target attribute |
| 125 | + |
| 126 | + |
| 127 | +## Usage |
| 128 | +Wherever you plan to use the href directive |
| 129 | + |
| 130 | +```typescript |
| 131 | +import { ngxHrefModule } from 'ngx-href' |
| 132 | + |
| 133 | +imports: [ |
| 134 | + ngxHrefModule, |
| 135 | +] |
| 136 | +``` |
| 137 | + |
| 138 | +Then you can use it as you would normally use an `a` element |
| 139 | + |
| 140 | +### Directive |
| 141 | +Normal use |
| 142 | +```html |
| 143 | +<!-- Angular router --> |
| 144 | +<a href="/angular/router/link"> |
| 145 | + My internal link |
| 146 | +</a> |
| 147 | + |
| 148 | +<!-- External link --> |
| 149 | +<a href="https://external-url.com"> |
| 150 | + An external link |
| 151 | +</a> |
| 152 | + |
| 153 | + |
| 154 | +<!-- Scroll --> |
| 155 | +<a href="#myAnchor"> |
| 156 | + My scroll to anchor |
| 157 | +</a> |
| 158 | + |
| 159 | +<!-- Scroll in different page --> |
| 160 | +<a href="/angular/router#link"> |
| 161 | + My internal link with anchor |
| 162 | +</a> |
| 163 | +``` |
| 164 | + |
| 165 | + |
| 166 | +### Service |
| 167 | +```typescript |
| 168 | +// foo.component.ts |
| 169 | +import { ngxHrefService } from 'ngx-href' |
| 170 | + |
| 171 | +// ... |
| 172 | + constructor(public ngxHrefService: ngxHrefService) {} |
| 173 | +``` |
| 174 | + |
| 175 | +Normal use |
| 176 | +```html |
| 177 | +<button (click)="ngxHrefService.scrollTo(#myAnchor)"> |
| 178 | + Scroll to #myAnchor |
| 179 | +</button> |
| 180 | + |
| 181 | +<!-- some html --> |
| 182 | + |
| 183 | +<h2 id="myAnchor">A title</h2> |
| 184 | +``` |
| 185 | + |
| 186 | +## Authors and acknowledgment |
| 187 | +* maintainer [Raphaël Balet](https://github.com/rbalet) |
0 commit comments