Skip to content

Commit 2da0dce

Browse files
committed
Deprecate classes and styles inputs in all components
Fixes #373
1 parent 495d633 commit 2da0dce

File tree

8 files changed

+119
-16
lines changed

8 files changed

+119
-16
lines changed

UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ You might also be interested in the larger umbrella project [upgrade guide](http
1515
* [0.4.0 to 0.5.0](docs/upgrading/0.4.0-0.5.0.md)
1616
* [0.5.0 to 0.6.0](docs/upgrading/0.5.0-0.6.0.md)
1717
* [0.9.0 to 0.10.0](docs/upgrading/0.9.0-0.10.0.md)
18+
* [0.11.0 to 0.12.0](docs/upgrading/0.11.0-0.12.0.md)

docs/guide/styling-icon-internals.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Styling icon internals
2+
3+
**DISCLAIMER:** Styling icon internals is not recommended as it relies on the component's implementation details and may silently break after any library update.
4+
5+
For the majority of the cases, styling the icon with regular `style` and `class` properties as shown in [Custom styles](../usage/features.md#custom-styles) should be used. However, sometimes one has to attach style to one of the internal elements of the component. To achieve this, one would need to overcome the Angular [view encapsulation](https://angular.io/guide/view-encapsulation). This guide explains how to do that.
6+
7+
## Use global styles
8+
9+
As global styles are not subject to the view encapsulation, one can add styles for the `fa-icon` internals to the global `styles.css` and use it everywhere in the application.
10+
11+
```css
12+
/* styles.css */
13+
fa-icon.fancy svg path {
14+
fill: #ffffff;
15+
stroke: #ff0000;
16+
stroke-width: 10;
17+
}
18+
```
19+
20+
```angular2html
21+
<!-- app.component.html -->
22+
<fa-icon icon="user" class="fancy"></fa-icon>
23+
```
24+
25+
## Use `::ng-deep` pseudo-class selector
26+
27+
Another options is to use `:ng-deep` pseudo-class selector. This has the benefit that styles are local to the component and won't accidentally affect `fa-icon` usage in other components of the application.
28+
29+
```ts
30+
import { Component } from '@angular/core';
31+
32+
@Component({
33+
selector: 'app-root',
34+
template: '<fa-icon icon="user" class="fancy"></fa-icon>',
35+
styles: [`
36+
fa-icon.fancy ::ng-deep svg path {
37+
fill: #ffffff;
38+
stroke: #ff0000;
39+
stroke-width: 10;
40+
}
41+
`],
42+
})
43+
export class AppComponent {}
44+
```

docs/upgrading/0.11.0-0.12.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Upgrading 0.11.0 to 0.12.0
2+
3+
## Remove usage of the deprecated `styles` and `classes` inputs
4+
5+
`styles` and `classes` inputs in all components are deprecated for removal in the next release. These inputs don't work the way one would expect and cause a lot of confusion. For majority of the cases, one should use regular [class and style bindings](https://angular.io/guide/class-binding) provided by Angular. For those rare cases, when it is not enough, there is a guide on how one can style component's internal elements at their own risk - [Styling icon internals](https://github.com/FortAwesome/angular-fontawesome/blob/master/docs/guide/styling-icon-internals.md).

docs/usage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ Utilize core FontAwesome features
2626
* [Animations](./usage/features.md#animations)
2727
* [Border](./usage/features.md#border)
2828
* [Pull](./usage/features.md#pull)
29-
* [Custom Classes](./usage/features.md#custom-classes)
30-
* [Default Style](./usage/features.md#default-style)
29+
* [Custom styles](./usage/features.md#custom-styles)
3130

3231
## Features specific for Duotone icons
3332
Additional features available for Duotone icons
@@ -51,8 +50,9 @@ Take your icons to the next level with these advanced features.
5150
* [Programmatic API](./usage/features.md#programmatic-api)
5251

5352
## Guides
54-
Guides cover specific topics or use cases.
53+
Guides on specific topics or use cases.
5554

5655
* [Testing](./guide/testing.md)
5756
* [Storybook](./guide/storybook.md)
5857
* [Advanced uses](./guide/advanced-uses.md)
58+
* [Styling icon internals](./guide/styling-icon-internals.md)

docs/usage/features.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,22 @@ The following features are available as part of Font Awesome. Note that the synt
6060
<fa-icon [icon]="['fas', 'coffee']" pull="right"></fa-icon>
6161
```
6262

63-
### Custom Classes
63+
### Custom styles
6464

65-
```html
66-
<fa-icon [icon]="['fas', 'coffee']" [classes]="['my-icon-class']"></fa-icon>
65+
Simple styles can be applied using usual [class and style bindings](https://angular.io/guide/class-binding):
66+
67+
```css
68+
.red-icon {
69+
color: red;
70+
}
6771
```
6872

69-
### Default Style
7073
```html
71-
<fa-icon [icon]="['fas', 'coffee']" [styles]="{'stroke': 'red', 'color': 'red'}"></fa-icon>
74+
<fa-icon [icon]="['fas', 'coffee']" class="red-icon" [style]="{display: 'inline-block', padding: '5px'}"></fa-icon>
7275
```
7376

77+
For more advanced styling, see [Styling icon internals](../guide/styling-icon-internals.md).
78+
7479
## Duotone icons
7580

7681
### Basic use

src/lib/icon/icon.component.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Component, HostBinding, Input, OnChanges, Optional, SimpleChanges } fro
22
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
33
import {
44
FaSymbol,
5-
findIconDefinition,
65
FlipProp,
76
icon,
87
IconDefinition,
@@ -38,13 +37,23 @@ export class FaIconComponent implements OnChanges {
3837

3938
/**
4039
* Specify a title for the icon.
40+
*
4141
* This text will be displayed in a tooltip on hover and presented to the
4242
* screen readers.
4343
*/
4444
@Input() title?: string;
4545
@Input() spin?: boolean;
4646
@Input() pulse?: boolean;
4747
@Input() mask?: IconProp;
48+
49+
/**
50+
* Set `style` attribute on the SVG element rendered by the component.
51+
*
52+
* @deprecated This input breaks view encapsulation and is not recommended.
53+
* For simple cases (like colors), use `style` on the component itself, for
54+
* more complex usages, explicitly opt-in to break the view encapsulation.
55+
* This input is deprecated since 0.12.0 and will be removed in 0.13.0.
56+
*/
4857
@Input() styles?: Styles;
4958
@Input() flip?: FlipProp;
5059
@Input() size?: SizeProp;
@@ -54,6 +63,15 @@ export class FaIconComponent implements OnChanges {
5463
@Input() symbol?: FaSymbol;
5564
@Input() rotate?: RotateProp;
5665
@Input() fixedWidth?: boolean;
66+
67+
/**
68+
* Set `class` attribute on the SVG element rendered by the component.
69+
*
70+
* @deprecated This input breaks view encapsulation and is not recommended.
71+
* For simple cases (like colors), use `class` on the component itself, for
72+
* more complex usages, explicitly opt-in to break the view encapsulation.
73+
* This input is deprecated since 0.12.0 and will be removed in 0.13.0.
74+
*/
5775
@Input() classes?: string[] = [];
5876
@Input() transform?: string | Transform;
5977

@@ -86,14 +104,8 @@ export class FaIconComponent implements OnChanges {
86104
return faWarnIfIconSpecMissing();
87105
}
88106

89-
let iconToBeRendered: IconProp = null;
90-
if (this.icon == null) {
91-
iconToBeRendered = this.config.fallbackIcon;
92-
} else {
93-
iconToBeRendered = this.icon;
94-
}
95-
96107
if (changes) {
108+
const iconToBeRendered = this.icon != null ? this.icon : this.config.fallbackIcon;
97109
const iconDefinition = this.findIconDefinition(iconToBeRendered);
98110
if (iconDefinition != null) {
99111
const params = this.buildParams();

src/lib/layers/layers-counter.component.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,25 @@ import { FaLayersComponent } from './layers.component';
1414
export class FaLayersCounterComponent implements OnChanges {
1515
@Input() content: string;
1616
@Input() title?: string;
17+
18+
/**
19+
* Set `style` attribute on the SVG element rendered by the component.
20+
*
21+
* @deprecated This input breaks view encapsulation and is not recommended.
22+
* For simple cases (like colors), use `style` on the component itself, for
23+
* more complex usages, explicitly opt-in to break the view encapsulation.
24+
* This input is deprecated since 0.12.0 and will be removed in 0.13.0.
25+
*/
1726
@Input() styles?: Styles;
27+
28+
/**
29+
* Set `class` attribute on the SVG element rendered by the component.
30+
*
31+
* @deprecated This input breaks view encapsulation and is not recommended.
32+
* For simple cases (like colors), use `class` on the component itself, for
33+
* more complex usages, explicitly opt-in to break the view encapsulation.
34+
* This input is deprecated since 0.12.0 and will be removed in 0.13.0.
35+
*/
1836
@Input() classes?: string[] = [];
1937
@Input() position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
2038

src/lib/layers/layers-text.component.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,25 @@ import { FaLayersComponent } from './layers.component';
2626
export class FaLayersTextComponent implements OnChanges {
2727
@Input() content: string;
2828
@Input() title?: string;
29+
30+
/**
31+
* Set `style` attribute on the SVG element rendered by the component.
32+
*
33+
* @deprecated This input breaks view encapsulation and is not recommended.
34+
* For simple cases (like colors), use `style` on the component itself, for
35+
* more complex usages, explicitly opt-in to break the view encapsulation.
36+
* This input is deprecated since 0.12.0 and will be removed in 0.13.0.
37+
*/
2938
@Input() styles?: Styles;
39+
40+
/**
41+
* Set `class` attribute on the SVG element rendered by the component.
42+
*
43+
* @deprecated This input breaks view encapsulation and is not recommended.
44+
* For simple cases (like colors), use `class` on the component itself, for
45+
* more complex usages, explicitly opt-in to break the view encapsulation.
46+
* This input is deprecated since 0.12.0 and will be removed in 0.13.0.
47+
*/
3048
@Input() classes?: string[] = [];
3149
@Input() spin?: boolean;
3250
@Input() pulse?: boolean;

0 commit comments

Comments
 (0)