-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmwc-floating-label-directive.js
29 lines (29 loc) · 1.22 KB
/
mwc-floating-label-directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { MDCFloatingLabelFoundation } from '@material/floating-label/foundation.js';
import { directive } from 'lit-html';
const createAdapter = (labelElement) => {
return {
addClass: (className) => labelElement.classList.add(className),
removeClass: (className) => labelElement.classList.remove(className),
getWidth: () => labelElement.scrollWidth,
registerInteractionHandler: (evtType, handler) => {
labelElement.addEventListener(evtType, handler);
},
deregisterInteractionHandler: (evtType, handler) => {
labelElement.removeEventListener(evtType, handler);
},
};
};
const partToFoundationMap = new WeakMap();
export const floatingLabel = directive(() => (part) => {
const lastFoundation = partToFoundationMap.get(part);
if (!lastFoundation) {
const labelElement = part.committer.element;
labelElement.classList.add('mdc-floating-label');
const adapter = createAdapter(labelElement);
const foundation = new MDCFloatingLabelFoundation(adapter);
foundation.init();
part.setValue(foundation);
partToFoundationMap.set(part, foundation);
}
});
//# sourceMappingURL=mwc-floating-label-directive.js.map