Good time How to style dynamically in lit? My main goal is to change the color of an element according to the user's input in the input element.
Asked
Active
Viewed 2,700 times
1
-
This will help: https://stackoverflow.com/questions/71240044/how-to-ensure-css-class-styles-are-rendered-on-custom-html-elements/71241290#71241290 – Danny '365CSI' Engelman May 15 '22 at 19:02
1 Answers
1
You can't use ${}
in lit css tag function!
But you can select element and then change style
import {html, css, LitElement} from 'lit';
import {customElement, property, query} from 'lit/decorators.js';
@customElement('dynamic-style')
export class DynamicStyle extends LitElement {
static styles = css`
label {
color: #023047;
}
`;
@property()
color: string;
@query('input') input: HTMLSelectElement | undefined;
@query('label') label: HTMLSelectElement | undefined;
render() {
return html`
<label for="color-input">HEX color: </label>
<input id="color-input" placeholder="#023047" />
`;
}
firstUpdated() {
this.input.addEventListener('input', () => {
this.label.style.color = this.input.value;
});
}
}
Also read: Dynamic classes and styles | Lit Doc
Simply! It is good to search more and then ask

njfamirm
- 164
- 2
- 12