Conditional element attributes not working using templates

How would I conditionally add element attributes? This doesn’t seem to work with the data-color attribute (it doesn’t appear at all):

const __color = ? `data-color="${}"` : '';

return $h `
       <div class="treeview-item" data-id="${}" data-type="${child.type}"  ${__color}>

Is there a way to conditionally build up attributes on an element?

And it will not work like so, just use attributes on element: data-color=${}. If it is undefined it won’t be rendered

1 Like