Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(alert): remove intermediate 'div' element #2552

Merged

Conversation

maxokorokov
Copy link
Member

Fixes #2544

ngOnChanges(changes: SimpleChanges) {
const {type} = changes;
if (type && !type.firstChange) {
this._renderer.removeClass(this._element.nativeElement, `alert-${type.previousValue}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we do this through low-level DOM manipulation? Couldn't we move the existing bindings to the host?
Or is binding overriding classes set by a user on a host?

Copy link
Member Author

@maxokorokov maxokorokov Jul 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would you do this?

[class.alert-warning]: 'type === warning',
[class.alert-success]: 'type === success',
etc?

we also currently support alert-customclass as seen in demos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[class]="'alert alert-' + type + (dismissible ? ' alert-dismissible' : '')" ?

Exactly as we used to have it, just moved to the host.
I just don't remember if Angular is merging classes on the host or not...

Copy link
Member Author

@maxokorokov maxokorokov Jul 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double-checked, and it overrides user-set classes. So I see three options:

  1. Leave as it is done currently with Renderer2
  2. hardcode all supported classes and remove the demo with alert-customclass (breaking change?):
  host: {
    'role': 'alert',
    'class': 'alert d-block',
    '[class.alert-dismissible]': 'dismissible',
    '[class.alert-success]': `type === 'success'`,
    '[class.alert-warning]': `type === 'warning'`,
    '[class.alert-info]': `type === 'info'`,
    '[class.alert-danger]': `type === 'danger'`,
    '[class.alert-primary]': `type === 'primary'`,
    '[class.alert-secondary]': `type === 'secondary'`,
    '[class.alert-light]': `type === 'light'`,
    '[class.alert-dark]': `type === 'dark'`
  },
  1. do nothing and close this :)

this.dismissible = config.dismissible;
this.type = config.type;
}

closeHandler() { this.close.emit(null); }

ngOnChanges(changes: SimpleChanges) {
const {type} = changes;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it works I find the type name confusing / not very descriptive here. A much better name would be typeChange. I would prefer to be explicit here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change

}
}

ngOnInit() { this._renderer.addClass(this._element.nativeElement, `alert-${this.type}`); }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we move this logic to ngOnChanges and set the class on firstChange? This way the whole logic would be centralised in one place

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case:

<ngb-alert>Cool!</ngb-alert>

onChanges won't be called at all, but initial value comes from the NgbAlertConfig

@pkozlowski-opensource
Copy link
Member

@maxokorokov I left few nits but generally speaking I think that we should merge it. It is not an ideal solution but this is the best we've got given current Angular...

BREAKING CHANGE: markup generated by `<ngb-alert>` was simplified, there is no more intermediate `<div>` element

Before:
```
<ngb-alert>
  <div role="alert" class="alert alert-warning">
    Hello there
  </div>
</ngb-alert>
```

After:
```
<ngb-alert role="alert" class="alert alert-warning">
  Hello there
</ngb-alert>
```

Fixes ng-bootstrap#2544
@maxokorokov maxokorokov force-pushed the alert/refactor/simplify-markup branch from f223de0 to 217d94c Compare August 3, 2018 10:08
@maxokorokov
Copy link
Member Author

@pkozlowski-opensource , fixed comments and:

  • added a BREAKING CHANGE comment about the markup change
  • replaced d-block with :host { display: block; } as it doesn't set !important flag

@maxokorokov maxokorokov merged commit c9c463d into ng-bootstrap:master Aug 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants