Skip to content

fix(checkbox, slide-toggle): no margin if content is projected #12973

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

Conversation

devversion
Copy link
Member

Usually if the label of the checkbox or slide-toggle is empty, we remove the margin between label container and thumb/check because otherwise there would be too much spacing. Currently if developers use a component inside of the checkbox or slide-toggle in order to render the label, the margin is accidentally removed and the label looks misplaced.

This is because the cdkObserveContent event runs outside of the NgZone and no change detection round checks the checkbox or slide-toggle.

Simple demo

Why detectChanges and not ngZone.run()?

  • Creating a new zone fork e.g. ngZone.run(() => {}) causes the onMicrotaskEmpty to trigger and dirty check all components which are explicitly "markForChecked or not using OnPush`.

  • Since cdkObserveContent emits an @output() the checkbox will be automatically marked for check, but due to emitting outside of NgZone, just no change detection will run because onMicrotaskEmpty doesn't fire after callback.

So the best solution for now is:

  • Use explicitly detectChanges because it would then just check the checkbox and it's children. Since we know that only the margin class binding needs to be updated, this is way better than triggering a global dirty-check.

  • If we would use ngZone.run(() => {} it would cause Angular to check all components in the application which are marked for check or not using OnPush (which is very likely)


Fixes #4720

Usually if the label of the checkbox or slide-toggle is empty, we remove the margin between label container and thumb/check because otherwise there would be too much spacing. Currently if developers use a component inside of the checkbox or slide-toggle in order to render the label, the margin is accidentally removed and the label looks misplaced.

This is because the `cdkObserveContent` event runs outside of the `NgZone` and no change detection round _checks_ the checkbox or slide-toggle.

Fixes angular#4720
@devversion devversion added the target: patch This PR is targeted for the next patch release label Sep 4, 2018
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 4, 2018
// Instead of going back into the zone in order to trigger a change detection which causes
// *all* components to be checked (if explicitly marked or not using OnPush), we only trigger
// an explicit change detection for the checkbox view and it's children.
this._changeDetectorRef.detectChanges();
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a test for this, even if that test is doing getComputedStyle?

@mmalerba mmalerba added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Sep 5, 2018
@mmalerba mmalerba merged commit 4636a98 into angular:master Sep 18, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Md-Checkbox has no margin when using dynamic label after #2121
5 participants