Lit Directives API reference for Semantic UI's Lit directives code Guide

Lit Directives

Semantic UI builds on Lit’s directive system to provide reactive data binding, conditional rendering, iteration, and template rendering. These directives are used internally by the template system and are automatically applied based on your template syntax.

Advanced Use - It is not recommended to use these directives directly unless you are building custom template functionality. The template syntax will automatically use the appropriate directive.

Import

import { reactiveData } from '@semantic-ui/component';
import { reactiveConditional } from '@semantic-ui/component';
import { reactiveEach } from '@semantic-ui/component';
import { renderTemplate } from '@semantic-ui/component';

Directives

reactiveData

Handles reactive data bindings in templates. Updates the DOM automatically when reactive values change.

reactiveData(expression, options)

Parameters

NameTypeRequiredDescription
expressionObjectYesExpression configuration object
optionsObjectNoDirective options

Expression Object

PropertyTypeRequiredDescription
valueFunctionYesFunction that returns the value
expressionstringYesOriginal template expression

Options Object

PropertyTypeDefaultDescription
ifDefinedbooleanfalseUse ifDefined directive
unsafeHTMLbooleanfalseUse unsafeHTML directive

Example

// Template: {{ count }}
const expr = reactiveData({
expression: 'count',
value: () => count.get()
});
// With options
const expr = reactiveData({
expression: 'content',
value: () => content.get()
}, {
unsafeHTML: true
});

reactiveConditional

Handles conditional rendering in templates. Re-renders content when conditions change.

reactiveConditional(options)

Parameters

NameTypeRequiredDescription
optionsObjectYesConditional rendering options

Options Object

PropertyTypeRequiredDescription
conditionFunctionYesCondition evaluation function
contentFunctionYesContent rendering function
branchesArrayNoAdditional else/elseif branches

Branch Object

PropertyTypeRequiredDescription
typestringYes’else’ or ‘elseif’
conditionFunctionNoCondition for ‘elseif’
contentFunctionYesContent rendering function

Example

// Template: {{#if isVisible}} content {{/if}}
const conditional = reactiveConditional({
condition: () => isVisible.get(),
content: () => html`<div>Visible Content</div>`,
branches: [
{
type: 'else',
content: () => html`<div>Hidden Content</div>`
}
]
});

reactiveEach

Handles array iteration in templates. Re-renders when array content changes.

reactiveEach(options)

Parameters

NameTypeRequiredDescription
optionsObjectYesIteration options

Options Object

PropertyTypeRequiredDescription
overFunctionYesFunction returning array
contentFunctionYesItem rendering function
asstringNoVariable name for each item

Example

// Template: {{#each items}} {{name}} {{/each}}
const list = reactiveEach({
over: () => items.get(),
content: (item) => html`<li>${item.name}</li>`,
as: 'item'
});

renderTemplate

Renders sub-templates within a template. Manages template lifecycle and cleanup.

renderTemplate(options)

Parameters

NameTypeRequiredDescription
optionsObjectYesTemplate rendering options

Options Object

PropertyTypeRequiredDescription
subTemplatesObjectYesAvailable templates
templateNamestringYesName of template to render
getTemplateFunctionNoDynamic template getter
dataObjectNoTemplate data context
parentTemplateTemplateNoParent template reference

Example

// Template: {{> header data=headerData}}
const template = renderTemplate({
subTemplates: {
header: HeaderTemplate
},
templateName: 'header',
data: {
title: 'Page Header'
}
});

Usage in Templates

While these directives are typically used through template syntax, understanding how they work can be helpful for debugging or custom implementations.

Data Binding

// Template syntax
{{ value }}
// Equivalent directive
reactiveData({
expression: 'value',
value: () => value.get()
})

Conditional Rendering

// Template syntax
{{#if condition}}
content
{{else}}
alternate
{{/if}}
// Equivalent directive
reactiveConditional({
condition: () => condition.get(),
content: () => html`content`,
branches: [{
type: 'else',
content: () => html`alternate`
}]
})

Iteration

// Template syntax
{{#each items as item}}
{{ item.name }}
{{/each}}
// Equivalent directive
reactiveEach({
over: () => items.get(),
content: (item) => html`${item.name}`,
as: 'item'
})

Sub-templates

// Template syntax
{{> header data=headerData}}
// Equivalent directive
renderTemplate({
templateName: 'header',
data: headerData,
subTemplates: availableTemplates
})