I am working with Lit components. I want to create two buttons and toggle between two different components. What is the best approach to achieve that?
Asked
Active
Viewed 123 times
1
-
Please add an executable StackOverflow Snippet to your post. It will help readers execute your code with one click. And help create answers with one click. See [How to add a StackOverflow snippet](https://meta.stackoverflow.com/questions/269753/feedback-requested-runnable-code-snippets-in-questions-and-answers) – Danny '365CSI' Engelman Nov 01 '22 at 16:51
1 Answers
2
The best way depends on your use case. In this case it is probably the simplest way.
- Use a reactive property to maintain the state of which component to render. When a button is pressed, this state can change, and a reactive update is scheduled resulting in an efficient template update.
- Have a parent that contains the two buttons and the state of which view to render, and then renders that view.
For more sophisticated component composition setups see lit.dev documentation on Component Composition. There is also a great video by the Lit core team on Event communication between components.
Here's a runnable example showing a small example of two buttons toggling between two different components:
<script type="module">
import {html, LitElement} from "https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js"
class ContainerEl extends LitElement {
static properties = {
toggleComponent: {type: Boolean},
};
constructor() {
super();
this.toggleComponent = false;
}
render() {
// Using a ternary to choose which template to use. Could also
// do this with an `if` statement.
const view = this.toggleComponent ?
html`<view-b></view-b>` : html`<view-a></view-a>`;
return html`
<button @click=${() => {this.toggleComponent = false;}}>
View A
</button>
<button @click=${() => {this.toggleComponent = true;}}>
View B
</button>
${view}
`;
}
}
class ViewA extends LitElement {
render() { return html`<b>Element A</b>`; }
}
class ViewB extends LitElement {
render() { return html`<i>Element B</i>`; }
}
customElements.define('container-el', ContainerEl);
customElements.define('view-a', ViewA);
customElements.define('view-b', ViewB);
</script>
<container-el></container-el>
In specific more complicated cases, the following directives could also be useful (but not in a simple situation such as this one):

YouCodeThings
- 590
- 3
- 13
-
Thank you so much for your answer. That solution is what I was looking for, I just tested out in my project and everything works as planned. – tech27 Nov 02 '22 at 15:16
-
I am also trying to add green background-color to the button when button is active. Would you have some great solution to that one? I am trying with styleMap but it's not working as I want it to work. – tech27 Nov 02 '22 at 15:55
-
Could you make a new question showing how you're using styleMap and I can help there? Thanks! – YouCodeThings Nov 02 '22 at 16:18
-
1https://stackoverflow.com/questions/74300942/change-button-background-color-when-button-is-clicked-with-lit-components-stylem – tech27 Nov 03 '22 at 09:47