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 = child.data.color ? `data-color="${child.data.color}"` : '';

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

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=${child.data.color}. If it is undefined it won’t be rendered

1 Like