Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make LitHtml attributes optional?

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>`
like image 956
dork Avatar asked Jan 26 '23 09:01

dork


1 Answers

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>`
like image 100
abraham Avatar answered Mar 21 '23 05:03

abraham