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

[Checkbox] indicator is inline element #2716

Merged
merged 6 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 94 additions & 131 deletions packages/core/src/components/forms/_controls.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,63 @@ $control-checked-background-color: nth(map-get($button-intents, "primary"), 1) !
$control-checked-background-color-hover: nth(map-get($button-intents, "primary"), 2) !default;
$control-checked-background-color-active: nth(map-get($button-intents, "primary"), 3) !default;

$control-indicator-size: $pt-icon-size-standard !default;
$control-indicator-size-large: $pt-icon-size-large !default;
$control-indicator-spacing: $pt-grid-size !default;

@mixin control-checked-colors($selector: ":checked") {
input#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: $button-intent-box-shadow;
background-color: $control-checked-background-color;
background-image: $button-intent-gradient;
color: $white;
}

&:hover input#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: $button-intent-box-shadow;
background-color: $control-checked-background-color-hover;
}

input:not(:disabled):active#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: $button-intent-box-shadow-active;
background: $control-checked-background-color-active;
}

input:disabled#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: none;
background: rgba($control-checked-background-color, 0.5);
}

.#{$ns}-dark & {
input#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: $dark-button-intent-box-shadow;
}

&:hover input#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: $dark-button-intent-box-shadow;
background-color: $control-checked-background-color-hover;
}

input:not(:disabled):active#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: $dark-button-intent-box-shadow-active;
background-color: $control-checked-background-color-active;
}

input:disabled#{$selector} ~ .#{$ns}-control-indicator {
box-shadow: none;
background: rgba($control-checked-background-color-active, 0.5);
}
}
}

.#{$ns}-control {
@include control-checked-colors();

display: block;
position: relative;
margin-bottom: $pt-grid-size;
cursor: pointer;
// controls sometimes don't have text in their <label>, so we set a minimum height
min-height: $control-indicator-size;
padding-left: $control-indicator-size + $pt-grid-size;
text-transform: none;
line-height: $control-indicator-size;

&.#{$ns}-disabled {
cursor: not-allowed;
Expand All @@ -46,10 +93,10 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
}

.#{$ns}-control-indicator {
position: absolute;
top: 0;
left: 0;
margin: 0;
display: inline-block;
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems pretty major

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is, but it's also wholly contained inside the component. no API has been changed.

position: relative;
margin-top: -3px;
Copy link
Contributor

Choose a reason for hiding this comment

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

does this depend of font size?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😕 it does, but checkboxes were not designed to support arbitrary font size. that would require a different approach. i've been fiddling with a flex layout a la menu items (requiring wrapping children in a <span>) but i don't think it's worth the change just yet.

margin-right: $control-indicator-spacing;
border: none;
box-shadow: $button-box-shadow;
background-clip: padding-box;
Expand All @@ -58,54 +105,35 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
cursor: pointer;
width: $control-indicator-size;
height: $control-indicator-size;
vertical-align: middle;
user-select: none;
}

input:checked ~ .#{$ns}-control-indicator {
box-shadow: $button-intent-box-shadow;
background-color: $control-checked-background-color;
background-image: $button-intent-gradient;
color: $white;
&:hover .#{$ns}-control-indicator {
background-color: $control-background-color-hover;
}

&:hover {
.#{$ns}-control-indicator {
background-color: $control-background-color-hover;
}

input:checked ~ .#{$ns}-control-indicator {
box-shadow: $button-intent-box-shadow;
background-color: $control-checked-background-color-hover;
}
input:not(:disabled):active ~ .#{$ns}-control-indicator {
box-shadow: $button-box-shadow-active;
background: $control-background-color-active;
}

input:not(:disabled):active {
~ .#{$ns}-control-indicator {
box-shadow: $button-box-shadow-active;
background: $control-background-color-active;
}

&:checked ~ .#{$ns}-control-indicator {
box-shadow: $button-intent-box-shadow-active;
background: $control-checked-background-color-active;
}
input:disabled ~ .#{$ns}-control-indicator {
box-shadow: none;
background: $button-background-color-disabled;
cursor: not-allowed;
}

input:focus ~ .#{$ns}-control-indicator {
@include focus-outline();
}

input:disabled {
~ .#{$ns}-control-indicator {
box-shadow: none;
background: $button-background-color-disabled;
cursor: not-allowed;
}

&:checked ~ .#{$ns}-control-indicator {
box-shadow: none;
background: rgba($control-checked-background-color, 0.5);
}
// right-aligned indicator is glued to the right side of the container
&.#{$ns}-align-right .#{$ns}-control-indicator {
float: right;
margin-top: 1px;
margin-right: 0;
margin-left: $control-indicator-spacing;
}

/*
Expand All @@ -129,14 +157,17 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
*/

&.#{$ns}-checkbox {
@mixin indicator-icon($icon) {
@mixin indicator-inline-icon($icon) {
&::before {
// embed SVG icon image as backgroud-image above gradient.
// the SVG image content is inlined into the CSS, so use this sparingly.
background-image: svg("16px/#{$icon}.svg", (path: (fill: $white)));
}
}

// make :indeterminate look like :checked _for Checkbox only_
@include control-checked-colors(":indeterminate");

.#{$ns}-control-indicator {
border-radius: $pt-border-radius;

Expand All @@ -148,19 +179,12 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
}
}

input:indeterminate,
&:hover input:indeterminate {
// :indeterminate should behave like :checked with different icon
// stylelint-disable-next-line scss/at-extend-no-missing-placeholder
@extend input:checked;
}

input:checked ~ .#{$ns}-control-indicator {
@include indicator-icon("small-tick");
@include indicator-inline-icon("small-tick");
}

input:indeterminate ~ .#{$ns}-control-indicator {
@include indicator-icon("small-minus");
@include indicator-inline-icon("small-minus");
}
}

Expand Down Expand Up @@ -264,8 +288,6 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
$dark-switch-indicator-background-color-disabled: rgba($black, 0.4) !default;

&.#{$ns}-switch {
padding-left: $switch-width + $pt-grid-size;

.#{$ns}-control-indicator {
border: none;
border-radius: $switch-width;
Expand All @@ -291,11 +313,6 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
}
}

&.#{$ns}-align-right {
padding-right: $switch-width + $pt-grid-size;
padding-left: 0;
}

input:checked ~ .#{$ns}-control-indicator {
box-shadow: none;
background-color: $switch-checked-background-color;
Expand Down Expand Up @@ -349,47 +366,19 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
}
}

// right-aligned indicator is glued to the right side of the container
&.#{$ns}-align-right {
padding-right: $control-indicator-size + $pt-grid-size;
padding-left: 0;

.#{$ns}-control-indicator {
right: 0;
left: auto;
}
}

&.#{$ns}-large {
min-height: $control-indicator-size-large;
padding-left: $control-indicator-size-large + $pt-grid-size;
line-height: $control-indicator-size-large;
font-size: $pt-font-size-large;

.#{$ns}-control-indicator {
width: $control-indicator-size-large;
height: $control-indicator-size-large;
line-height: $control-indicator-size-large;
font-family: $icons20-family;
font-size: $control-indicator-size-large;
}

&.#{$ns}-align-right {
padding-right: $control-indicator-size-large + $pt-grid-size;
padding-left: 0;
}

&.#{$ns}-checkbox input:checked ~ .#{$ns}-control-indicator::before {
top: 1px;
}

&.#{$ns}-radio .#{$ns}-control-indicator {
font-size: $radio-indicator-font-size-large;
}

&.#{$ns}-switch {
padding-left: $switch-width-large + $pt-grid-size;

.#{$ns}-control-indicator {
width: $switch-width-large;
height: $switch-height-large;
Expand All @@ -400,6 +389,10 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
}
}

&.#{$ns}-align-right .#{$ns}-control-indicator {
margin-top: 0;
}

input:checked ~ .#{$ns}-control-indicator {
width: $switch-width-large;
height: $switch-height-large;
Expand All @@ -408,11 +401,6 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
left: $switch-width-large - $switch-height-large + $switch-indicator-margin;
}
}

&.#{$ns}-align-right {
padding-right: $switch-width-large + $pt-grid-size;
padding-left: 0;
}
}
}

Expand All @@ -429,55 +417,30 @@ $control-checked-background-color-active: nth(map-get($button-intents, "primary"
background-image: $dark-button-gradient;
}

input:checked ~ .#{$ns}-control-indicator {
box-shadow: $dark-button-intent-box-shadow;
}

&:hover {
.#{$ns}-control-indicator {
background-color: $dark-control-background-color-hover;
}
&:hover .#{$ns}-control-indicator {
background-color: $dark-control-background-color-hover;
}

input:not(:disabled):active {
~ .#{$ns}-control-indicator {
box-shadow: $dark-button-box-shadow-active;
background: $dark-control-background-color-active;
}

&:checked ~ .#{$ns}-control-indicator {
box-shadow: $dark-button-intent-box-shadow-active;
background-color: $control-checked-background-color-active;
}
input:not(:disabled):active ~ .#{$ns}-control-indicator {
box-shadow: $dark-button-box-shadow-active;
background: $dark-control-background-color-active;
}

input:disabled {
~ .#{$ns}-control-indicator {
box-shadow: none;
background: $dark-button-background-color-disabled;
cursor: not-allowed;
}

&:checked ~ .#{$ns}-control-indicator {
box-shadow: none;
background: rgba($control-checked-background-color-active, 0.5);
}
input:disabled ~ .#{$ns}-control-indicator {
box-shadow: none;
background: $dark-button-background-color-disabled;
cursor: not-allowed;
}

&.#{$ns}-checkbox {
input:checked {
&:disabled ~ .#{$ns}-control-indicator::before {
&.#{$ns}-checkbox input:disabled {
&:checked,
&:indeterminate {
~ .#{$ns}-control-indicator {
color: $dark-button-color-disabled;
}
}
}

&.#{$ns}-radio {
input:checked:disabled ~ .#{$ns}-control-indicator::before {
background: $dark-button-color-disabled;
}
}

&.#{$ns}-switch {
.#{$ns}-control-indicator {
box-shadow: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AlignmentSelect } from "./common/alignmentSelect";

export interface ICheckboxExampleState {
alignIndicator: Alignment;
disabled: boolean;
inline: boolean;
large: boolean;
value?: string;
Expand All @@ -20,6 +21,7 @@ export interface ICheckboxExampleState {
export class CheckboxExample extends React.PureComponent<IExampleProps, ICheckboxExampleState> {
public state: ICheckboxExampleState = {
alignIndicator: Alignment.LEFT,
disabled: false,
inline: false,
large: false,
};
Expand All @@ -28,6 +30,7 @@ export class CheckboxExample extends React.PureComponent<IExampleProps, ICheckbo
const options = (
<>
<H5>Props</H5>
<Switch checked={this.state.disabled} label="Disabled" onChange={this.handleDisabledChange} />
<Switch checked={this.state.inline} label="Inline" onChange={this.handleInlineChange} />
<Switch checked={this.state.large} label="Large" onChange={this.handleLargeChange} />
<AlignmentSelect
Expand Down Expand Up @@ -59,6 +62,7 @@ export class CheckboxExample extends React.PureComponent<IExampleProps, ICheckbo

// tslint:disable:member-ordering
private handleAlignChange = (alignIndicator: Alignment) => this.setState({ alignIndicator });
private handleDisabledChange = handleBooleanChange(disabled => this.setState({ disabled }));
private handleInlineChange = handleBooleanChange(inline => this.setState({ inline }));
private handleLargeChange = handleBooleanChange(large => this.setState({ large }));
}