Skip to content

Commit

Permalink
Warn about missing stackItemSize on fa-icon inside fa-stack
Browse files Browse the repository at this point in the history
Previously we would silently drop elements without stackItemSize, which was confusing to users.

With this change we also stop making assumptions about the passed elements to prevent issues like FortAwesome#283, but in fa-stack.

Fixes FortAwesome#177
  • Loading branch information
devoto13 committed Dec 11, 2020
1 parent e9e0549 commit ea8d738
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/lib/icon/icon.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('FaIconComponent', () => {
it('should have title attribute, when title input is set using Angular binding syntax', () => {
@Component({
selector: 'fa-host',
template: `<fa-icon [icon]="faUser" [title]="'User John Smith'"></fa-icon> `,
template: ` <fa-icon [icon]="faUser" [title]="'User John Smith'"></fa-icon> `,
})
class HostComponent {
faUser = faUser;
Expand Down Expand Up @@ -405,4 +405,23 @@ describe('FaIconComponent', () => {
expect(queryByCss(fixture, '.fa-circle')).toBeFalsy();
expect(spy).not.toHaveBeenCalledWith();
});

it('should warn when stackItemSize attribute is missing on icon inside fa-stack', () => {
@Component({
selector: 'fa-host',
template: '<fa-stack><fa-icon [icon]="faCircle"></fa-icon></fa-stack>',
})
class HostComponent {
faCircle = faCircle;
}

const spy = spyOn(console, 'error');

const fixture = initTest(HostComponent);
fixture.detectChanges();
expect(spy).toHaveBeenCalledWith(
'FontAwesome: fa-icon and fa-duotone-icon elements must specify stackItemSize attribute when wrapped into ' +
'fa-stack. Example: <fa-icon stackItemSize="2x"></fa-icon>.',
);
});
});
11 changes: 10 additions & 1 deletion src/lib/icon/icon.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FaProps } from '../shared/models/props.model';
import { faClassList } from '../shared/utils/classlist.util';
import { faNormalizeIconSpec } from '../shared/utils/normalize-icon-spec.util';
import { FaStackItemSizeDirective } from '../stack/stack-item-size.directive';
import { FaStackComponent } from '../stack/stack.component';

@Component({
selector: 'fa-icon',
Expand Down Expand Up @@ -70,7 +71,15 @@ export class FaIconComponent implements OnChanges {
private config: FaConfig,
private iconLibrary: FaIconLibrary,
@Optional() private stackItem: FaStackItemSizeDirective,
) {}
@Optional() stack: FaStackComponent,
) {
if (stack != null && stackItem == null) {
console.error(
'FontAwesome: fa-icon and fa-duotone-icon elements must specify stackItemSize attribute when wrapped into ' +
'fa-stack. Example: <fa-icon stackItemSize="2x"></fa-icon>.',
);
}
}

ngOnChanges(changes: SimpleChanges) {
if (this.icon == null && this.config.fallbackIcon == null) {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/stack/stack.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { SizeProp } from '@fortawesome/fontawesome-svg-core';

@Component({
selector: 'fa-stack',
// TODO: See if it is better to select fa-icon and throw if it does not have stackItemSize directive
template: `<ng-content select="fa-icon[stackItemSize],fa-duotone-icon[stackItemSize]"></ng-content>`,
template: `<ng-content></ng-content>`,
})
export class FaStackComponent implements OnInit, OnChanges {
/**
Expand Down

0 comments on commit ea8d738

Please sign in to comment.