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

feat(checkbox): Add color attribute. #1463

Merged
merged 2 commits into from
Oct 25, 2016
Merged

feat(checkbox): Add color attribute. #1463

merged 2 commits into from
Oct 25, 2016

Conversation

mathebox
Copy link
Contributor

@mathebox mathebox commented Oct 10, 2016

Fixes #947

Do you any suggestions how improve setting the default value?
(src/lib/checkbox/checkbox.ts line 138)

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 10, 2016
.md-checkbox-background {
background-color: $checkbox-background-color-primary;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

It would be a little more concise as

.md-checkbox-indeterminate, .md-checkbox-checked {
  &.primary .md-checkbox-background {
    background: md-color($primary, 500);
  }

  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done if you meant &.md-primary instead of &.primary.
MdButton also uses the CSS classes md-primary, md-accent and md-warn.

$checkbox-background-color: md-color($accent, 500);
$checkbox-background-color-primary: md-color($primary, 500);
$checkbox-background-color-accent: md-color($accent, 500);
$checkbox-background-color-warn: md-color($warn, 500);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think these variables are necessary; you can use md-color directly where its needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

.md-ink-ripple {
background-color: md-color($primary, 0.26);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

It would also be a little more concise as

.md-checkbox-focused {
  &.primary .md-ink-ripple {
    background: md-color($primary, 0.26);
  }
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done if you meant &.md-primary instead of &.primary.
MdButton also uses the CSS classes md-primary, md-accent and md-warn.


// Animation
transition: background $swift-ease-out-duration $swift-ease-out-timing-function,
md-elevation-transition-property-value();
Copy link
Member

Choose a reason for hiding this comment

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

Why is md-elevation-transition-property-value here? This was only meant to be used with elevation

Copy link
Contributor Author

@mathebox mathebox Oct 11, 2016

Choose a reason for hiding this comment

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

Here it's used in the same way. _button-base.scss

@@ -128,11 +128,15 @@ export class MdCheckbox implements ControlValueAccessor {

private _indeterminate: boolean = false;

private _color: string;
Copy link
Member

Choose a reason for hiding this comment

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

You can just say private _color: string = 'accent';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately that alone won't do the magic. The md-checkbox DOM element has to get the class md-accent. To achieve this you have the following options:

  1. Add one of these lines to the constructor (All are somehow weird calls):
    • this.color = 'accent';
    • this._updateColor(this._color)
    • this._setElementColor(this._color, true)
    • this._renderer.setElementClass(this._elementRef.nativeElement, md-${this._color}, true);
  2. Alternative way: Modify the theme css like this (Introduce a "base" color):
.md-checkbox-indeterminate, .md-checkbox-checked {
  .md-checkbox-background {
    background-color: md-color($accent, 500);
  }
  &.md-primary .md-checkbox-background {
    background-color: md-color($primary, 500);
  }
  ...
}

Which option do you think is the best? Have a missed an option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jelbourn Any thoughts on this?

@jelbourn
Copy link
Member

LGTM

I'm not sure we'll keep all of the color attributes in the long term (vs. preferring styling via theming), but this is good for now.

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Oct 25, 2016
@kara kara merged commit 333b11e into angular:master Oct 25, 2016
@jaibatrik
Copy link

Now that this is merged, how do I actually change the color of the checkbox? I have tried doing this -

<md-checkbox [checked]="(task.state == 'STATE_CLEARED')" color="red"></md-checkbox>

All it does is to add the mat-red class to md-checkbox element. I tried putting the following styles in my Component CSS -

.mat-red .mat-checkbox-background {
  background-color: red;
}

But the selectors get converted to .mat-red[_ngcontent-c7] .mat-checkbox-background[_ngcontent-c7].

I don't want to define global styles for this.

@willshowell
Copy link
Contributor

@jaibatrik your color options are primary|accent|warn. See the docs

@jaibatrik
Copy link

Oh. But what if I want my checkbox to be colored red? Or any custom color? Is there any way to do it? Let's suppose none of primary, accent, and warn are red in my theme.

@willshowell
Copy link
Contributor

In that case, global styles or the /deep/ selector (search for it on this page) should do it for you.

If you're still unable to get the override, make sure your selectors have a higher specificity than the original selector.

@jaibatrik
Copy link

Just so that others can know, I was able to achieve this by using the /deep/ selector and constructing the selector like the following -

.mat-red.mat-checkbox-checked /deep/ .mat-checkbox-background {
  background-color: red;
}

According to the docs, \deep\ will propagate to any depth of children components; but since the checkbox doesn't have any, I'll be safe here.

Thanks!

@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 6, 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for color attribute to checkbox
6 participants