Skip to content

Commit c935423

Browse files
committed
fix: textfield labels and notches show incorrect state and position
Fixes #236
1 parent b13b2a0 commit c935423

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

packages/floating-label/FloatingLabel.svelte

+11-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
<script>
4141
import { MDCFloatingLabelFoundation } from '@material/floating-label';
42-
import { ponyfill } from '@material/dom';
4342
import { onMount, getContext } from 'svelte';
4443
import { get_current_component } from 'svelte/internal';
4544
import {
@@ -48,7 +47,6 @@
4847
useActions,
4948
dispatch,
5049
} from '@smui/common/internal.js';
51-
const { estimateScrollWidth } = ponyfill;
5250
5351
const forwardEvents = forwardEventsBuilder(get_current_component());
5452
@@ -84,7 +82,17 @@
8482
instance = new MDCFloatingLabelFoundation({
8583
addClass,
8684
removeClass,
87-
getWidth: () => estimateScrollWidth(getElement()),
85+
getWidth: () => {
86+
const el = getElement();
87+
const clone = el.cloneNode(true);
88+
el.parentNode.appendChild(clone);
89+
clone.classList.add('smui-floating-label--remove-transition');
90+
clone.classList.add('smui-floating-label--force-size');
91+
clone.classList.remove('mdc-floating-label--float-above');
92+
const scrollWidth = clone.scrollWidth;
93+
el.parentNode.removeChild(clone);
94+
return scrollWidth;
95+
},
8896
registerInteractionHandler: (evtType, handler) =>
8997
getElement().addEventListener(evtType, handler),
9098
deregisterInteractionHandler: (evtType, handler) =>

packages/floating-label/_style.scss

+9
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
@use '@material/floating-label/mdc-floating-label';
2+
3+
.smui-floating-label--remove-transition {
4+
transition: unset !important;
5+
}
6+
7+
.smui-floating-label--force-size {
8+
position: absolute !important;
9+
transform: unset !important;
10+
}

packages/textfield/Textfield.svelte

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
'mdc-text-field--outlined': variant === 'outlined',
2323
'smui-text-field--standard': variant === 'standard' && !textarea,
2424
'mdc-text-field--no-label': noLabel || (label == null && !$$slots.label),
25-
'mdc-text-field--label-floating': value != null && value !== '',
25+
'mdc-text-field--label-floating':
26+
focused || (value != null && value !== ''),
2627
'mdc-text-field--with-leading-icon':
2728
withLeadingIcon === uninitializedValue
2829
? $$slots.leadingIcon
@@ -335,6 +336,16 @@
335336
instance.setDisabled(disabled);
336337
}
337338
339+
// React to changes of value from outside component.
340+
let previousValue = value;
341+
$: if (instance && valued && previousValue !== value) {
342+
previousValue = value;
343+
// Check the data is flowing down.
344+
if (instance.getValue() !== value) {
345+
instance.setValue(value);
346+
}
347+
}
348+
338349
if (addLayoutListener) {
339350
removeLayoutListener = addLayoutListener(layout);
340351
}

0 commit comments

Comments
 (0)