Skip to content

Commit

Permalink
feat(checkbox): Added mixin to customize checkbox touch dimension. (#…
Browse files Browse the repository at this point in the history
…4697)

BREAKING CHANGE: Removed `$mdc-checkbox-ui-pct` sass variable from `MDCCheckbox`
  • Loading branch information
abhiomkar committed Jun 11, 2019
1 parent 5de76bc commit ff2873e
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/mdc-checkbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Mixin | Description
`mdc-checkbox-container-colors($unmarked-stroke-color, $unmarked-fill-color, $marked-stroke-color, $marked-fill-color, $generate-keyframes)` | Generates CSS classes to set and animate the stroke color and/or container fill color of a checkbox
`mdc-checkbox-ink-color($color)` | Sets the ink color of the checked and indeterminate icons
`mdc-checkbox-focus-indicator-color($color)` | Sets the color of the focus indicator
`mdc-checkbox-touch-dimension($touch-dimension)` | Sets the touch dimension of the checkbox.

The ripple effect for the Checkbox component is styled using [MDC Ripple](../mdc-ripple) mixins.

Expand Down
58 changes: 36 additions & 22 deletions packages/mdc-checkbox/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
@include mdc-ripple-surface($query);
@include mdc-states($mdc-checkbox-baseline-theme-color, $query: $query);
@include mdc-ripple-radius-unbounded($query: $query);
@include mdc-checkbox-touch-dimension($mdc-checkbox-touch-area, $query: $query);
}

.mdc-ripple-upgraded--background-focused .mdc-checkbox__background::before {
Expand All @@ -195,6 +196,32 @@
}
}

@mixin mdc-checkbox-touch-dimension($touch-dimension, $query: mdc-feature-all()) {
$feat-structure: mdc-feature-create-target($query, structure);
$checkbox-padding: ($touch-dimension - $mdc-checkbox-size) / 2;

@include mdc-feature-targets($feat-structure) {
padding: $checkbox-padding;
}

.mdc-checkbox__background {
@include mdc-feature-targets($feat-structure) {
@include mdc-rtl-reflexive-position(left, $checkbox-padding, ".mdc-checkbox");

top: $checkbox-padding;
}
}

.mdc-checkbox__background::before {
@include mdc-feature-targets($feat-structure) {
top: -$mdc-checkbox-border-width - $checkbox-padding;
left: -$mdc-checkbox-border-width - $checkbox-padding;
width: $touch-dimension;
height: $touch-dimension;
}
}
}

@mixin mdc-checkbox-container-colors(
$unmarked-stroke-color: $mdc-checkbox-border-color,
$unmarked-fill-color: transparent,
Expand Down Expand Up @@ -298,7 +325,6 @@
box-sizing: content-box;
width: $mdc-checkbox-size;
height: $mdc-checkbox-size;
padding: ($mdc-checkbox-touch-area - $mdc-checkbox-size) / 2;
line-height: 0;
white-space: nowrap;
cursor: pointer;
Expand All @@ -321,14 +347,6 @@
pointer-events: none;
}

@mixin mdc-checkbox__child--cover-parent_ {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}

@mixin mdc-checkbox__child--upgraded_ {
// Due to the myriad of selector combos used to properly style a CSS-only checkbox, all of
// which have varying selector precedence and make use of transitions, it is cleaner and more
Expand Down Expand Up @@ -455,17 +473,13 @@
$feat-structure: mdc-feature-create-target($query, structure);

@include mdc-feature-targets($feat-structure) {
@include mdc-rtl-reflexive-position(left, ($mdc-checkbox-touch-area - $mdc-checkbox-size) / 2, ".mdc-checkbox");

display: inline-flex;
position: absolute;
top: ($mdc-checkbox-touch-area - $mdc-checkbox-size) / 2;
bottom: 0;
align-items: center;
justify-content: center;
box-sizing: border-box;
width: $mdc-checkbox-ui-pct;
height: $mdc-checkbox-ui-pct;
width: $mdc-checkbox-size;
height: $mdc-checkbox-size;
// border-color is overridden by the mdc-checkbox-unmarked-stroke-color() mixin
border: $mdc-checkbox-border-width solid currentColor;
border-radius: 2px;
Expand Down Expand Up @@ -494,10 +508,7 @@
$feat-structure: mdc-feature-create-target($query, structure);

@include mdc-feature-targets($feat-structure) {
@include mdc-checkbox__child--cover-parent_;

width: 100%;
height: 100%;
position: absolute;
transform: scale(0, 0);
border-radius: 50%;
opacity: 0;
Expand All @@ -518,7 +529,7 @@
$feat-structure: mdc-feature-create-target($query, structure);

@include mdc-feature-targets($feat-structure) {
transform: scale(2.75, 2.75);
transform: scale(1);
opacity: $mdc-checkbox-focus-indicator-opacity;
}

Expand Down Expand Up @@ -550,8 +561,11 @@
$feat-structure: mdc-feature-create-target($query, structure);

@include mdc-feature-targets($feat-structure) {
@include mdc-checkbox__child--cover-parent_;

position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
opacity: 0;
}
Expand Down
1 change: 0 additions & 1 deletion packages/mdc-checkbox/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ $mdc-checkbox-baseline-theme-color: secondary !default;

$mdc-checkbox-touch-area: 40px !default;
$mdc-checkbox-size: 18px !default;
$mdc-checkbox-ui-pct: percentage($mdc-checkbox-size / $mdc-checkbox-touch-area) !default;
$mdc-checkbox-mark-stroke-size: 2/15 * $mdc-checkbox-size !default;
$mdc-checkbox-border-width: 2px !default;
$mdc-checkbox-transition-duration: 90ms !default;
Expand Down
16 changes: 12 additions & 4 deletions test/screenshot/golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@
}
},
"spec/mdc-checkbox/classes/baseline.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/10/15_16_09_688/spec/mdc-checkbox/classes/baseline.html?utm_source=golden_json",
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/15/20_30_12_828/spec/mdc-checkbox/classes/baseline.html?utm_source=golden_json",
"screenshots": {
"desktop_windows_chrome@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/10/15_16_09_688/spec/mdc-checkbox/classes/baseline.html.windows_chrome_74.png",
"desktop_windows_firefox@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/mattgoo/2018/07/18/19_29_49_289/spec/mdc-checkbox/classes/baseline.html.windows_firefox_61.png",
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/mattgoo/2018/07/18/19_29_49_289/spec/mdc-checkbox/classes/baseline.html.windows_ie_11.png"
"desktop_windows_chrome@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/15/16_09_10_398/spec/mdc-checkbox/classes/baseline.html.windows_chrome_74.png",
"desktop_windows_firefox@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/15/20_30_12_828/spec/mdc-checkbox/classes/baseline.html.windows_firefox_65.png",
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/15/16_09_10_398/spec/mdc-checkbox/classes/baseline.html.windows_ie_11.png"
}
},
"spec/mdc-checkbox/mixins/container-colors.html": {
Expand All @@ -199,6 +199,14 @@
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/advorak/2018/07/31/03_19_25_902/spec/mdc-checkbox/mixins/container-colors.html.windows_ie_11.png"
}
},
"spec/mdc-checkbox/mixins/touch-dimension.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/15/16_09_10_398/spec/mdc-checkbox/mixins/touch-dimension.html?utm_source=golden_json",
"screenshots": {
"desktop_windows_chrome@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/13/19_16_33_458/spec/mdc-checkbox/mixins/touch-dimension.html.windows_chrome_74.png",
"desktop_windows_firefox@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/13/19_16_33_458/spec/mdc-checkbox/mixins/touch-dimension.html.windows_firefox_65.png",
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/15/16_09_10_398/spec/mdc-checkbox/mixins/touch-dimension.html.windows_ie_11.png"
}
},
"spec/mdc-chips/classes/action.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2019/05/10/14_25_41_111/spec/mdc-chips/classes/action.html?utm_source=golden_json",
"screenshots": {
Expand Down
2 changes: 1 addition & 1 deletion test/screenshot/spec/mdc-checkbox/classes/baseline.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<input type="checkbox"
checked
class="mdc-checkbox__native-control"
id="checkbox-checked"/>
id="checkbox-checked" autofocus />
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
Expand Down
4 changes: 4 additions & 0 deletions test/screenshot/spec/mdc-checkbox/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
document.getElementById('checkbox-indeterminate').indeterminate = true;
document.getElementById('checkbox-indeterminate-disabled').indeterminate = true;

[].slice.call(document.querySelectorAll('.mdc-checkbox')).forEach((el) => {
mdc.checkbox.MDCCheckbox.attachTo(el);
});

window.mdc.testFixture.notifyDomReady();
4 changes: 4 additions & 0 deletions test/screenshot/spec/mdc-checkbox/fixture.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ $custom-checkbox-color: $material-color-red-300;
.custom-checkbox--ink-color {
@include mdc-checkbox-ink-color($custom-checkbox-color);
}

.test-checkbox-touch-dimension {
@include mdc-checkbox-touch-dimension(48px);
}
164 changes: 164 additions & 0 deletions test/screenshot/spec/mdc-checkbox/mixins/touch-dimension.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<!DOCTYPE html>
<!--
Copyright 2019 Google Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Checkbox Touch Dimension Mixin - MDC Web Screenshot Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../../../out/mdc.checkbox.css">
<link rel="stylesheet" href="../../../out/spec/fixture.css">
<link rel="stylesheet" href="../../../out/spec/mdc-checkbox/fixture.css">

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-118996389-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-118996389-2');
</script>
</head>

<body class="test-container">
<main class="test-viewport test-viewport--mobile">
<div class="test-layout">
<div class="test-cell test-cell--checkbox">
<div class="mdc-checkbox test-checkbox-touch-dimension">
<input type="checkbox"
class="mdc-checkbox__native-control"
id="checkbox-unchecked"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</div>

<div class="test-cell test-cell--checkbox">
<div class="mdc-checkbox test-checkbox-touch-dimension">
<input type="checkbox"
checked
class="mdc-checkbox__native-control"
autofocus
id="checkbox-checked"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</div>

<div class="test-cell test-cell--checkbox">
<div class="mdc-checkbox test-checkbox-touch-dimension">
<input type="checkbox"
checked
class="mdc-checkbox__native-control"
id="checkbox-indeterminate"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</div>

<div class="test-cell test-cell--checkbox">
<div class="mdc-checkbox mdc-checkbox--disabled test-checkbox-touch-dimension">
<input type="checkbox"
disabled
class="mdc-checkbox__native-control"
id="checkbox-unchecked-disabled"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</div>

<div class="test-cell test-cell--checkbox">
<div class="mdc-checkbox mdc-checkbox--disabled test-checkbox-touch-dimension">
<input type="checkbox"
disabled
checked
class="mdc-checkbox__native-control"
id="checkbox-checked-disabled"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</div>

<div class="test-cell test-cell--checkbox">
<div class="mdc-checkbox mdc-checkbox--disabled test-checkbox-touch-dimension">
<input type="checkbox"
disabled
class="mdc-checkbox__native-control"
id="checkbox-indeterminate-disabled"/>
<div class="mdc-checkbox__background">
<svg class="mdc-checkbox__checkmark"
viewBox="0 0 24 24">
<path class="mdc-checkbox__checkmark-path"
fill="none"
d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
</svg>
<div class="mdc-checkbox__mixedmark"></div>
</div>
</div>
</div>
</div>
</main>

<!-- Automatically provides/replaces `Promise` if missing or broken. -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fontfaceobserver/2.0.13/fontfaceobserver.standalone.js"></script>
<script src="../../../out/material-components-web.js"></script>
<script src="../../../out/spec/fixture.js"></script>
<script src="../../../out/spec/mdc-checkbox/fixture.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions test/scss/_feature-targeting-test.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
@include mdc-checkbox-focus-indicator-color(red, $query: $query);
@include mdc-checkbox-ripple($query: $query);
@include mdc-checkbox-without-ripple($query: $query);
@include mdc-checkbox-touch-dimension(40px, $query: $query);

// Chips
@include mdc-chip-core-styles($query: $query);
Expand Down

0 comments on commit ff2873e

Please sign in to comment.