Is there a way to make LitHtml attributes optional?
Asked Answered
D

1

6

I'm not talking about boolean attributes, attributes like class if you don't want to add an empty class attribute if there's no CSS class.

html`<span class=${hasClass ? 'my-class' : ''}></span>`
Doha answered 30/4, 2019 at 1:56 Comment(0)
O
11

There is an ifDefined directive that does what you want. If the value is undefined the attribute won't be rendered.

import {ifDefined} from 'lit-html/directives/if-defined';

html`<span class=${ifDefined(hasClass ? 'my-class' : undefined)}></span>`
Ornithine answered 30/4, 2019 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.