Skip to content

Commit 5de1931

Browse files
committed
Add support for "fa-rotate-by"
Fixes #454
1 parent 7fe2a58 commit 5de1931

File tree

8 files changed

+87
-8
lines changed

8 files changed

+87
-8
lines changed

docs/usage/features.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ The following features are available as part of Font Awesome. Note that the synt
2626
<fa-icon [icon]="['fas', 'coffee']" [rotate]="90"></fa-icon>
2727
<fa-icon [icon]="['fas', 'coffee']" [rotate]="180"></fa-icon>
2828
<fa-icon [icon]="['fas', 'coffee']" [rotate]="270"></fa-icon>
29+
<!-- Or any value supported by the rotate() transform can be specified. -->
30+
<fa-icon [icon]="['fas', 'coffee']" rotate="45deg"></fa-icon>
2931
```
3032

3133
### Flip

projects/demo/e2e/src/app.e2e-spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Angular FontAwesome demo', () => {
1010
});
1111

1212
it('should render all icons', async () => {
13-
expect(await appPage.icons.count()).toBe(46);
13+
expect(await appPage.icons.count()).toBe(47);
1414
});
1515

1616
it('should only add styles once', async () => {

projects/demo/src/app/app.component.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ <h3>Change color</h3>
4444

4545
<h3>Rotate</h3>
4646

47-
<fa-icon icon="user" [rotate]="90"></fa-icon>
47+
<p><fa-icon icon="user" [rotate]="90"></fa-icon></p>
48+
49+
<p><fa-icon icon="user" rotate="45deg"></fa-icon></p>
4850

4951
<h3>Animations</h3>
5052

src/lib/icon/icon.component.spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,57 @@ describe('FaIconComponent', () => {
378378
'fa-stack. Example: <fa-icon stackItemSize="2x"></fa-icon>.',
379379
);
380380
});
381+
382+
it('should be able to set predefined rotate as a number', () => {
383+
@Component({
384+
selector: 'fa-host',
385+
standalone: false,
386+
template: '<fa-icon icon="user" [rotate]="90"></fa-icon>',
387+
})
388+
class HostComponent {
389+
constructor(iconLibrary: FaIconLibrary) {
390+
iconLibrary.addIcons(faUser, faUserRegular);
391+
}
392+
}
393+
394+
const fixture = initTest(HostComponent);
395+
fixture.detectChanges();
396+
expect(queryByCss(fixture, '.fa-rotate-90')).toBeTruthy();
397+
});
398+
399+
it('should be able to set predefined rotate as a string', () => {
400+
@Component({
401+
selector: 'fa-host',
402+
standalone: false,
403+
template: '<fa-icon icon="user" rotate="90"></fa-icon>',
404+
})
405+
class HostComponent {
406+
constructor(iconLibrary: FaIconLibrary) {
407+
iconLibrary.addIcons(faUser, faUserRegular);
408+
}
409+
}
410+
411+
const fixture = initTest(HostComponent);
412+
fixture.detectChanges();
413+
expect(queryByCss(fixture, '.fa-rotate-90')).toBeTruthy();
414+
});
415+
416+
it('should be able to set customer rotate', () => {
417+
@Component({
418+
selector: 'fa-host',
419+
standalone: false,
420+
template: '<fa-icon icon="user" rotate="45deg"></fa-icon>',
421+
})
422+
class HostComponent {
423+
constructor(iconLibrary: FaIconLibrary) {
424+
iconLibrary.addIcons(faUser, faUserRegular);
425+
}
426+
}
427+
428+
const fixture = initTest(HostComponent);
429+
fixture.detectChanges();
430+
const svg = queryByCss(fixture, '.fa-rotate-by');
431+
expect(svg).toBeTruthy();
432+
expect(getComputedStyle(svg).getPropertyValue('--fa-rotate-angle')).toBe('45deg');
433+
});
381434
});

src/lib/icon/icon.component.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import {
1111
PullProp,
1212
RotateProp,
1313
SizeProp,
14+
Styles,
1415
Transform,
1516
} from '@fortawesome/fontawesome-svg-core';
1617
import { FaConfig } from '../config';
1718
import { FaIconLibrary } from '../icon-library';
1819
import { faWarnIfIconDefinitionMissing } from '../shared/errors/warn-if-icon-html-missing';
1920
import { faWarnIfIconSpecMissing } from '../shared/errors/warn-if-icon-spec-missing';
2021
import { AnimationProp, FaProps } from '../shared/models/props.model';
21-
import { faClassList } from '../shared/utils/classlist.util';
22+
import { faClassList, isKnownRotateValue } from '../shared/utils/classlist.util';
2223
import { ensureCss } from '../shared/utils/css';
2324
import { faNormalizeIconSpec } from '../shared/utils/normalize-icon-spec.util';
2425
import { FaStackItemSizeDirective } from '../stack/stack-item-size.directive';
@@ -59,7 +60,7 @@ export class FaIconComponent implements OnChanges {
5960
@Input() border?: boolean;
6061
@Input() inverse?: boolean;
6162
@Input() symbol?: FaSymbol;
62-
@Input() rotate?: RotateProp;
63+
@Input() rotate?: RotateProp | string;
6364
@Input() fixedWidth?: boolean;
6465
@Input() transform?: string | Transform;
6566

@@ -147,6 +148,11 @@ export class FaIconComponent implements OnChanges {
147148

148149
const parsedTransform = typeof this.transform === 'string' ? parse.transform(this.transform) : this.transform;
149150

151+
const styles: Styles = {};
152+
if (classOpts.rotate != null && !isKnownRotateValue(classOpts.rotate)) {
153+
styles['--fa-rotate-angle'] = `${classOpts.rotate}`;
154+
}
155+
150156
return {
151157
title: this.title,
152158
transform: parsedTransform,
@@ -156,6 +162,7 @@ export class FaIconComponent implements OnChanges {
156162
attributes: {
157163
role: this.a11yRole,
158164
},
165+
styles,
159166
};
160167
}
161168
}

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import {
77
PullProp,
88
RotateProp,
99
SizeProp,
10+
Styles,
1011
text,
1112
TextParams,
1213
Transform,
1314
} from '@fortawesome/fontawesome-svg-core';
1415
import { FaConfig } from '../config';
1516
import { faWarnIfParentNotExist } from '../shared/errors/warn-if-parent-not-exist';
1617
import { FaProps } from '../shared/models/props.model';
17-
import { faClassList } from '../shared/utils/classlist.util';
18+
import { faClassList, isKnownRotateValue } from '../shared/utils/classlist.util';
1819
import { ensureCss } from '../shared/utils/css';
1920
import { FaLayersComponent } from './layers.component';
2021

@@ -33,7 +34,7 @@ export class FaLayersTextComponent implements OnChanges {
3334
@Input() pull?: PullProp;
3435
@Input() border?: boolean;
3536
@Input() inverse?: boolean;
36-
@Input() rotate?: RotateProp;
37+
@Input() rotate?: RotateProp | string;
3738
@Input() fixedWidth?: boolean;
3839
@Input() transform?: string | Transform;
3940

@@ -72,10 +73,16 @@ export class FaLayersTextComponent implements OnChanges {
7273

7374
const parsedTransform = typeof this.transform === 'string' ? parse.transform(this.transform) : this.transform;
7475

76+
const styles: Styles = {};
77+
if (classOpts.rotate != null && !isKnownRotateValue(classOpts.rotate)) {
78+
styles['--fa-rotate-angle'] = `${classOpts.rotate}`;
79+
}
80+
7581
return {
7682
transform: parsedTransform,
7783
classes: faClassList(classOpts),
7884
title: this.title,
85+
styles,
7986
};
8087
}
8188

src/lib/shared/models/props.model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface FaProps {
1313
flip?: FlipProp;
1414
size?: SizeProp;
1515
pull?: PullProp;
16-
rotate?: RotateProp;
16+
rotate?: RotateProp | string;
1717
stackItemSize?: '1x' | '2x';
1818
}
1919

src/lib/shared/utils/classlist.util.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
import { RotateProp } from '@fortawesome/fontawesome-svg-core';
12
import { FaProps } from '../models/props.model';
23

4+
export const isKnownRotateValue = (rotate: RotateProp | string | undefined) =>
5+
rotate != null &&
6+
(rotate === 90 || rotate === 180 || rotate === 270 || rotate === '90' || rotate === '180' || rotate === '270');
7+
38
/**
49
* Fontawesome class list.
510
* Returns classes array by props.
611
*/
712
export const faClassList = (props: FaProps): string[] => {
13+
const knownRotateValue = isKnownRotateValue(props.rotate);
14+
815
const classes = {
916
[`fa-${props.animation}`]: props.animation != null && !props.animation.startsWith('spin'),
1017
'fa-spin': props.animation === 'spin' || props.animation === 'spin-reverse',
@@ -21,7 +28,8 @@ export const faClassList = (props: FaProps): string[] => {
2128
'fa-flip-horizontal': props.flip === 'horizontal' || props.flip === 'both',
2229
'fa-flip-vertical': props.flip === 'vertical' || props.flip === 'both',
2330
[`fa-${props.size}`]: props.size !== null,
24-
[`fa-rotate-${props.rotate}`]: props.rotate !== null,
31+
[`fa-rotate-${props.rotate}`]: knownRotateValue,
32+
'fa-rotate-by': props.rotate != null && !knownRotateValue,
2533
[`fa-pull-${props.pull}`]: props.pull !== null,
2634
[`fa-stack-${props.stackItemSize}`]: props.stackItemSize != null,
2735
};

0 commit comments

Comments
 (0)