Skip to content

Commit

Permalink
fix(notched-outline): Fix label overflow (#4171)
Browse files Browse the repository at this point in the history
Fix the floating label from overflowing outside of the notched outline.
  • Loading branch information
williamernest authored Jan 4, 2019
1 parent cb344c3 commit 145db1f
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/mdc-floating-label/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MDCFloatingLabel extends MDCComponent {
return new MDCFloatingLabelFoundation({
addClass: (className) => this.root_.classList.add(className),
removeClass: (className) => this.root_.classList.remove(className),
getWidth: () => this.root_.offsetWidth,
getWidth: () => this.root_.scrollWidth,
registerInteractionHandler: (evtType, handler) => this.root_.addEventListener(evtType, handler),
deregisterInteractionHandler: (evtType, handler) => this.root_.removeEventListener(evtType, handler),
});
Expand Down
1 change: 1 addition & 0 deletions packages/mdc-notched-outline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Method Signature | Description
`addClass(className: string) => void` | Adds a class to the notched outline element.
`removeClass(className: string) => void` | Removes a class from the notched outline element.
`setNotchWidthProperty(width: number) => void` | Sets the width of the notch in pixels.
`removeNotchWidthProperty() => void` | Removes the width property from the notch element.

### `MDCNotchedOutlineFoundation`

Expand Down
6 changes: 6 additions & 0 deletions packages/mdc-notched-outline/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
}
}

@if ($radius > $mdc-notched-outline-leading-width) {
.mdc-notched-outline__notch {
max-width: calc(100% - #{$radius} * 2);
}
}

.mdc-notched-outline__trailing {
@include mdc-shape-radius(mdc-shape-mask-radius($radius, 0 1 1 0), $rtl-reflexive: true);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/mdc-notched-outline/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class MDCNotchedOutlineAdapter {
* @param {number} width
*/
setNotchWidthProperty(width) {}

/**
* Removes the width style property from the notch element.
*/
removeNotchWidthProperty() {}
}

export default MDCNotchedOutlineAdapter;
3 changes: 2 additions & 1 deletion packages/mdc-notched-outline/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class MDCNotchedOutlineFoundation extends MDCFoundation {
addClass: () => {},
removeClass: () => {},
setNotchWidthProperty: () => {},
removeNotchWidthProperty: () => {},
});
}

Expand Down Expand Up @@ -87,7 +88,7 @@ class MDCNotchedOutlineFoundation extends MDCFoundation {
closeNotch() {
const {OUTLINE_NOTCHED} = MDCNotchedOutlineFoundation.cssClasses;
this.adapter_.removeClass(OUTLINE_NOTCHED);
this.adapter_.setNotchWidthProperty(0);
this.adapter_.removeNotchWidthProperty();
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/mdc-notched-outline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class MDCNotchedOutline extends MDCComponent {
/** @type {!MDCNotchedOutlineAdapter} */ (Object.assign({
addClass: (className) => this.root_.classList.add(className),
removeClass: (className) => this.root_.classList.remove(className),
setNotchWidthProperty: (width) => this.notchElement_.style.setProperty('width', width > 0 ? width + 'px' : '0'),
setNotchWidthProperty: (width) => this.notchElement_.style.setProperty('width', width + 'px'),
removeNotchWidthProperty: () => this.notchElement_.style.removeProperty('width'),
})));
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/mdc-notched-outline/mdc-notched-outline.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
left: 0;
box-sizing: border-box;
width: 100%;
max-width: 100%;
height: 100%;
/* @noflip */
text-align: left;
Expand Down Expand Up @@ -68,14 +69,25 @@
}

&__notch {
flex: 0 0 auto;
width: auto;
max-width: calc(100% - #{$mdc-notched-outline-leading-width} * 2);
}

.mdc-floating-label {
display: inline-block;
position: relative;
top: 17px;
bottom: auto;
max-width: 100%;
}

.mdc-floating-label--float-above {
text-overflow: clip;
}

&--upgraded .mdc-floating-label--float-above {
max-width: calc(100% / .75);
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/screenshot/golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,15 @@
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/advorak/2018/12/13/21_05_36_273/spec/mdc-textfield/issues/3332.html.windows_ie_11.png"
}
},
"spec/mdc-textfield/issues/4170.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2018/12/14/21_55_25_855/spec/mdc-textfield/issues/4170.html?utm_source=golden_json",
"screenshots": {
"desktop_windows_chrome@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2018/12/14/21_55_25_855/spec/mdc-textfield/issues/4170.html.windows_chrome_71.png",
"desktop_windows_edge@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2018/12/14/21_55_25_855/spec/mdc-textfield/issues/4170.html.windows_edge_17.png",
"desktop_windows_firefox@latest": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2018/12/14/21_55_25_855/spec/mdc-textfield/issues/4170.html.windows_firefox_63.png",
"desktop_windows_ie@11": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2018/12/14/21_55_25_855/spec/mdc-textfield/issues/4170.html.windows_ie_11.png"
}
},
"spec/mdc-textfield/mixins/outline-shape-radius.html": {
"public_url": "https://storage.googleapis.com/mdc-web-screenshot-tests/travis/2018/11/30/05_20_17_198/spec/mdc-textfield/mixins/outline-shape-radius.html?utm_source=golden_json",
"screenshots": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
</li>
</ul>
</div>
<div class="mdc-notched-outline">
<div class="mdc-notched-outline mdc-notched-outline--notched">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<span id="screenshot-select-label4" class="mdc-floating-label mdc-floating-label--float-above">Pick a Food Group</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
</li>
</ul>
</div>
<div class="mdc-notched-outline">
<div class="mdc-notched-outline mdc-notched-outline--notched">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<span id="screenshot-select-label4" class="mdc-floating-label mdc-floating-label--float-above">Pick a Food Group</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
</li>
</ul>
</div>
<div class="mdc-notched-outline">
<div class="mdc-notched-outline mdc-notched-outline--notched">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<span id="screenshot-select-label4" class="mdc-floating-label mdc-floating-label--float-above">Pick a Food Group</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
</li>
</ul>
</div>
<div class="mdc-notched-outline">
<div class="mdc-notched-outline mdc-notched-outline--notched">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<span id="screenshot-select-label4" class="mdc-floating-label mdc-floating-label--float-above">Pick a Food Group</span>
Expand Down
105 changes: 105 additions & 0 deletions test/screenshot/spec/mdc-textfield/issues/4170.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!DOCTYPE html>
<!--
Copyright 2018 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>Textarea Text Field - Long Label - MDC Web Screenshot Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../../../out/mdc.textfield.css">
<link rel="stylesheet" href="../../../out/mdc.typography.css">
<link rel="stylesheet" href="../../../out/spec/fixture.css">
<link rel="stylesheet" href="../../../out/spec/mdc-textfield/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--textarea">
<div class="mdc-text-field mdc-text-field--textarea">
<textarea id="full-width-textarea"
class="mdc-text-field__input test-text-field__input"
rows="2"></textarea>
<div class="mdc-notched-outline">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<label for="full-width-textarea" class="mdc-floating-label">Textarea Label That Is Really Really Really Really Long</label>
</div>
<div class="mdc-notched-outline__trailing"></div>
</div>
</div>
</div>

<div class="test-cell test-cell--textarea">
<div class="mdc-text-field mdc-text-field--textarea">
<textarea id="full-width-textarea-filled"
class="mdc-text-field__input test-text-field__input"
rows="2">Xyz</textarea>
<div class="mdc-notched-outline mdc-notched-outline--notched">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<label for="full-width-textarea-filled" class="mdc-floating-label mdc-floating-label--float-above">Textarea Label That Is Really Really Really Really Long</label>
</div>
<div class="mdc-notched-outline__trailing"></div>
</div>
</div>
</div>

<div class="test-cell test-cell--textarea">
<div class="mdc-text-field mdc-text-field--textarea">
<!-- htmllint-disable -->
<textarea id="full-width-textarea-long-filled"
class="mdc-text-field__input test-text-field__input"
rows="2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur rhoncus ac risus a eleifend. Phasellus dictum luctus leo quis ultricies. Vivamus fringilla vehicula turpis eu eleifend. Donec ultricies, est a scelerisque laoreet, arcu eros commodo nunc, sit amet hendrerit diam nisi id velit. Suspendisse porta nibh orci. Donec velit nisl, accumsan vitae eros a, consectetur interdum eros. Aenean dapibus vulputate semper. Aliquam finibus nec leo sed ultricies. Integer luctus convallis risus. Etiam suscipit suscipit ante vel volutpat. Ut venenatis dapibus elit dictum fringilla.</textarea>
<div class="mdc-notched-outline mdc-notched-outline--notched">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<label for="full-width-textarea-long-filled" class="mdc-floating-label mdc-floating-label--float-above">Textarea Label That Is Really Really Really Really Long</label>
</div>
<div class="mdc-notched-outline__trailing"></div>
</div>
<!-- htmllint-enable -->
</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-textfield/fixture.js"></script>
</body>
</html>
6 changes: 3 additions & 3 deletions test/unit/mdc-notched-outline/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('exports strings', () => {

test('defaultAdapter returns a complete adapter implementation', () => {
verifyDefaultAdapter(MDCNotchedOutlineFoundation, [
'addClass', 'removeClass', 'setNotchWidthProperty',
'addClass', 'removeClass', 'setNotchWidthProperty', 'removeNotchWidthProperty',
]);
});

Expand All @@ -60,9 +60,9 @@ test('#notch adds the notched class and sets the width of the element', () => {
td.verify(mockAdapter.addClass(MDCNotchedOutlineFoundation.cssClasses.OUTLINE_NOTCHED));
});

test('#closeNotch removes the notch selector and resets the width to 0', () => {
test('#closeNotch removes the notch selector and removes the width property', () => {
const {foundation, mockAdapter} = setupTest();
foundation.closeNotch();
td.verify(mockAdapter.removeClass(MDCNotchedOutlineFoundation.cssClasses.OUTLINE_NOTCHED));
td.verify(mockAdapter.setNotchWidthProperty(0));
td.verify(mockAdapter.removeNotchWidthProperty());
});
10 changes: 6 additions & 4 deletions test/unit/mdc-notched-outline/mdc-notched-outline.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ test('adapter#addClass adds a class to the root element', () => {
test('adapter#removeClass removes a class to the root element', () => {
const {root, component} = setupTest();
component.getDefaultFoundation().adapter_.removeClass('foo');
assert.isFalse(root.classList.contains('foo'));
component.getDefaultFoundation().adapter_.setNotchWidthProperty(50);
component.getDefaultFoundation().adapter_.removeNotchWidthProperty();
const path = root.querySelector('.mdc-notched-outline__notch');
assert.equal('', path.style.width);
});

test('#adapter.setNotchWidthProperty sets the width property on the notched element', () => {
const {root, component} = setupTest();
component.getDefaultFoundation().adapter_.setNotchWidthProperty(0);
component.getDefaultFoundation().adapter_.setNotchWidthProperty(50);
const path = root.querySelector('.mdc-notched-outline__notch');
assert.equal('0px', path.style.width);
assert.equal('50px', path.style.width);
});

0 comments on commit 145db1f

Please sign in to comment.