Class and Style binding
With Lili, the Lapikit preprocessor, a <kit:...> component behaves exactly like a native HTML element in Svelte. No new syntax to learn, write your classes and styles as you already do:
- the
classattribute accepts a string, an array or an object, with the rules of Svelte (clsx); - the
class:andstyle:directives work directly on<kit:...>tags.
Svelte only allows class:foo={...} and style:property={...} on native HTML elements. On a <kit:...> tag, Lili lifts this limit: it turns them into props the components handle, at build time.
Attribute class: Classes like on an element
As on any element in Svelte, class takes a string, an array (nested too) or an object whose keys are added when their value is truthy:
<script lang="ts">
let isActive = $state(true);
</script>
<!-- Result: class="kit-card my-card active" -->
<kit:card class={['my-card', { active: isActive }]}>
<kit:card-content>Card</kit:card-content>
</kit:card>
Directive class:: Dynamic class binding
- Boolean toggle - adds the class name when the value is
true. As in Svelte,class:activeis the shorthand ofclass:active={active}. - Number or text - appends the value to the name, joined by a dash:
class:gap={4}givesgap-4. There is no dash when the name ends or the value starts with a separator (-_:/.), which is useful for BEM modifier patterns (block--modifier). false,null,undefinedand''add nothing.
<script lang="ts">
let active = $state(true);
let isDark = $state(false);
</script>
<!-- Adds class "active" when active is true (shorthand of class:active={active}) -->
<kit:btn class:active>Click</kit:btn>
<!-- Number or text: the value is appended, "gap-4" -->
<kit:btn class:gap={4}>Click</kit:btn>
<!-- BEM modifier: "theme--dark" or "theme--light" -->
<kit:btn class:theme={isDark ? '--dark' : '--light'}>Click</kit:btn>
Directive style:: Dynamic inline styles
The part after style: is the CSS property name, CSS custom properties included.
<script lang="ts">
let isError = $state(true);
let brandColor = $state('orange');
</script>
<!-- Sets: color: red -->
<kit:btn style:color="red">Click</kit:btn>
<!-- Dynamic value -->
<kit:btn style:color={isError ? 'red' : 'inherit'}>Click</kit:btn>
<!-- CSS custom property -->
<kit:btn style:--kit-accent={brandColor}>Click</kit:btn>
Modifiers such as style:color|important are not supported on Lapikit components.
Property s-class: Dynamic class binding
s-class accepts the same forms as class, both are merged.
String property s-class
The simplest form - adds a static class name:
<kit:btn s-class="my-button">Click</kit:btn>
Array property s-class
Pass multiple class names at once:
<kit:btn s-class={['my-button', 'is-large']}>Click</kit:btn>
Object (conditional classes) property s-class
Use an object to conditionally apply class names: a key is added when its value is truthy.
<script>
let isActive = $state(true);
let isDisabled = $state(false);
</script>
<!-- Result: class="active" -->
<kit:btn s-class={{ active: isActive, disabled: isDisabled }}>Click</kit:btn>
Property s-style: Dynamic inline styles
The style attribute only accepts a string. Pass a CSS property object to s-style to apply one or more inline styles:
<kit:btn s-style={{ color: 'red', 'font-size': '14px' }}> Styled button </kit:btn>
Dynamic values work as expected:
<script>
let primaryColor = $state('#6200ea');
</script>
<kit:btn s-style={{ '--kit-accent': primaryColor }}> Themed button </kit:btn>
Values holding ;, { or } are ignored, they could add other declarations.
Without the Lapikit preprocessor
Without the preprocessor in svelte.config.js, Svelte refuses the class: and style: directives on components. Use the s-class_* and s-style_* props instead, they follow the same rules: the part after the underscore is the class name or the CSS property.
Directive property with s-class_*
- Boolean toggle - adds the class name when
true:
<script lang="ts">
let isRounded = $state(true);
</script>
<!-- Adds class "rounded" when isRounded is true -->
<kit:btn s-class_rounded={isRounded}>Click</kit:btn>
- String modifier - appends the value to the base name:
<script lang="ts">
let isDark = $state(false);
</script>
<!-- Adds class "theme--dark" -->
<kit:btn s-class_theme="--dark">Click</kit:btn>
<!-- Dynamic: "theme--dark" or "theme--light" -->
<kit:btn s-class_theme={isDark ? '--dark' : '--light'}>Click</kit:btn>
Directive property with s-style_*
<script lang="ts">
let isError = $state(true);
</script>
<!-- Sets: color: red -->
<kit:btn s-style_color="red">Click</kit:btn>
<!-- Sets: background-color: #f0f -->
<kit:btn s-style_background-color="#f0f">Click</kit:btn>
<!-- Dynamic value -->
<kit:btn s-style_color={isError ? 'red' : 'inherit'}>Click</kit:btn>
Particularly useful for CSS custom properties:
<script lang="ts">
let brandColor = $state('orange');
</script>
<kit:btn s-style_--kit-accent={brandColor}>Click</kit:btn>
Combining directives, s-class and s-style
Directives, s-class and s-style can be combined within a single component to manage both dynamic classes and inline styles. The style attribute comes last, so it wins over the other styles.
This simplifies the management of:
- Component conditional states
- Responsive styles
- Theme-based customization
- Utility classes associated with dynamic CSS variables
All binding methods work together and follow the same reactive behavior as standard Svelte templates.
<script>
let isActive = $state(false);
let theme = $state('dark');
let accentColor = $state('#6200ea');
</script>
<kit:btn
variant="filled"
s-class={['my-btn', { active: isActive }]}
class:theme={`--${theme}`}
s-style={{ 'border-radius': '99px' }}
style:--kit-accent={accentColor}
onclick={() => (isActive = !isActive)}
>
Toggle
</kit:btn>
Continue styling your Lapikit components
class:, style:, s-class and s-style are designed to make component styling closely resemble Svelte’s native behavior, while maintaining clean and flexible templates.
Whether you’re enabling or disabling utility classes, working with conditional states, or applying dynamic CSS variables, these utilities reduce repetitive styling logic and ensure consistent component styling across your entire project.
Since they act directly on Lapikit components, you can style interfaces without adding unnecessary wrappers or complex bindings.
These tools work particularly well with:
- Themes and CSS variables
- Responsive layouts
- Component states
- You can now consult the component documentation to see how they integrate with actual Lapikit components such as buttons, dialogs, cards, and form elements, for example.
