Index āŗ 11. Components and fragments
Learn how to break up your pages and apps into smaller, well-encapsulated, and easily maintainable components and fragments.
Topics covered
- Refactoring pages to use components and fragments.
- Components (.component.js files).
- Fragments (.fragment.js files).
- Importing components and fragments.
- How to scope styles.
- Component and fragment properties (props).
- Passing HTML attributes to components and fragments.
- Non-JavaScript components and fragments (HTML, CSS, and Markdown).
The Fetchiverse example in the last tutorial is about 100 lines of code in a single file, with a 60/40 split of CSS and Kitten HTML.
While thatās fine for something so simple, in larger projects, it would help us to maintain our code if we break it up into smaller components and fragments.
Letās start with the obvious refactor: pulling out our styles into an external stylesheet.
There are two ways we can work with external stylesheets in Kitten:
The traditional way: use a regular external .css file and import it on the client.
The Kitten way: use a CSS fragment (a .fragment.css file), import it on the server and embed it in the page.
The major advantage of the first way of doing things is that itās a well-known pattern that everyone knows. The major disadvantage is that the browser has to make as many HTTP calls as you have external stylesheets and this can impact the performance of your page.
The major advantage of the second way of doing things is that you send everything over in one HTTP call.
Hereās how you can implement the traditional way:
Create a file called index.css and cut and paste the styles from the previous tutorial:
body { font-family: sans-serif; font-size: 1.25em; padding-left: 1.5em; padding-right: 1.5em; } h1 { font-size: 2.5em; text-align: center; } p { line-height: 1.5; } p:first-of-type { margin-top: 0; } a:not(.avatar-link) { background-color: #8bdaff; color: black; text-decoration: none; padding: 0.25em; border-radius: 0.25em; /* Make sure posts donāt overflow their containers. */ overflow-wrap: anywhere; } ul { padding: 0; } li { display: flex; align-items: flex-start; column-gap: 1em; padding: 1em; margin-bottom: 1em; background-color: #ccc; border-radius: 1em; } .avatar { width: 8em; border: 6px solid #aaa; border-radius: 1em; } .content { /* Grow to fill all remaining space in the row. */ flex: 1; } .image { max-width: 100%; }Then, in your index page, remove the now-empty
<style>tag and, instead, import the stylesheet at runtime using the<link>tag:export default async function () { const postsResponse = await fetch('https://mastodon.ar.al/api/v1/timelines/public') const posts = await postsResponse.json() return kitten.html` <link rel='stylesheet' href='./index.css'> <h1>Aralās Public Fediverse Timeline</h1> <ul> ⦠</ul> ` }
While that works, itās not ideal.
If you open up the web inspector/developer tools panel of your browser, you will see that the <link> tag gets added to the body of the page.

Thatās not what we want as thatās not valid HTML (browsers are forgiving but writing valid HTML will save you headaches down the line).
So letās tell Kitten to place that tag in the <head> of our page instead.
We can do that using the Kitten-specific <content for=''>ā¦</content> tag:
export default async function () {
const postsResponse = await fetch('https://mastodon.ar.al/api/v1/timelines/public')
const posts = await postsResponse.json()
return kitten.html`
<content for='${kitten.page.head}'>
<link rel='stylesheet' href='./index.css'>
</content>
<h1>Aralās Public Fediverse Timeline</h1>
<ul>
ā¦
</ul>
`
}
You will get a proper introduction to the <content> tag in the Special page slots tutorial.
kitten.page.head is a constant provided by Kitten to aid you while authoring. If you have type-safety enabled for your project, you can type kitten.page. in your editor and see all the constants that refer to different parts of the page that you can add content to using the <content> tag. You can also use magic strings ā the values of those constants ā directly, but you wonāt get warnings if you type the wrong thing in. So the above example will still work if you write <content for='HEAD'>ā¦</content> but itās not recommended to do so.

Fragments are pieces of content stored in external files that you can import and add to your Kitten pages.
Fragments might contain dynamic content (like JavaScript) or static content (like CSS).
Letās refactor our example to use a server-side CSS fragment instead of an client-side CSS stylesheet to save us one HTTP call on the page. Itās very easy to do:
Rename your
index.cssfile toindex.fragment.css.In your index page, import the fragment file as if you were importing a JavaScript file and then interpolate it into your Kitten HTML:
import styles from './index.fragment.css' export default async function route () { const postsResponse = await fetch('https://mastodon.ar.al/api/v1/timelines/public') const posts = await postsResponse.json() return kitten.html` <${styles} /> <h1>Aralās Public Fediverse Timeline</h1> <ul> ⦠</ul> ` }
When the page refreshes in your browser, you should see the styles exactly as they were before.
However, instead of being loaded in via a separate HTTP call by the browser, you can find them in a style tag in the head of the page.

Wait a minute⦠how did Kitten know to place our styles in the <head> when we didnāt use the <content> tag to specify that they should be placed there?
Kitten automatically hoists any <style> tags it finds in your pages to the head of the page, maintaining the order in which they found them (so that it doesnāt affect the CSS cascade).
There will be times, however, when you donāt want Kitten to do that. Specifically, you might want to include an inline style tag if you want to use the @scope at-rule to add scoped styles to your components. You create inline style tags by adding the inline attribute on a style tag:
<style inline>
ā¦
</style>
The actual attribute name you use doesnāt matter. The rule is that Kitten only moves tags that match the exact regular expression <style>.*?<\/style>.
When Kitten encounters inline style tags, it leaves them where they are and doesnāt hoist them to the head of the page.
Right, now that weāve separated the presentation (our styles; CSS) from the structure and functionality of our page (our Kitten HTML), letās next look at how we can break down structure and functionality the page into smaller pieces (components and fragments) that are easier to reason about and maintain.
Letās start by examining the layout of our list:
<ul>
${posts.map(post => (
kitten.html`
<li>
<!-- Avatar. -->
<a class='avatar-link' href='${post.account.url}'>
<img
class='avatar'
src='${post.account.avatar}'
alt='${post.account.username}ās avatar'
>
</a>
<!-- Content -->
<div class='content'>
${kitten.safelyAddUntrustedHtml(post.content)}
${post.media_attachments.map(media => (
media.type !== 'image' ? '' : kitten.html`
<img
class='image'
src='${media.url}'
alt='${media.description ?? ''}'>
`
))}
</div>
</li>
`
))}
</ul>
We have two major elements in each list item: the authorās avatar and the post content itself. These are prime candidates to make into separate fragments or components.
So letās do that and, for the time being, just pull out those components into the same file, simplifying them when we can and inlining the styles that pertain to each:
import styles from './index.fragment.css'
const avatar = ({ post }) => kitten.html`
<a href='${post.account.url}'>
<img src='${post.account.avatar}' alt='${post.account.username}ās avatar' />
<style inline>
@scope {
img {
width: 8em;
border-radius: 1em;
}
}
</style>
</a>
`
const content = ({ post }) => kitten.html`
<div>
${kitten.safelyAddUntrustedHtml(post.content)}
${post.media_attachments.map(media => (
media.type === 'image' && kitten.html`
<img src='${media.url}' alt='${media.description ?? ''}'>
`
))}
<style inline>
@scope {
/* Grow to fill all remaining space in the row. */
flex: 1;
p { line-height: 1.5; }
p:first-of-type { margin-top: 0; }
a {
background-color: #8bdaff;
color: black;
text-decoration: none;
padding: 0.25em;
border-radius: 0.25em;
/* Make sure posts donāt overflow their containers. */
overflow-wrap: anywhere;
}
img { max-width: 100%; }
}
</style>
</div>
`
export default async function () {
const postsResponse = await fetch('https://mastodon.ar.al/api/v1/timelines/public')
const posts = await postsResponse.json()
return kitten.html`
<${styles} />
<h1>Aralās Public Fediverse Timeline</h1>
<ul>
${posts.map(post => kitten.html`
<li>
<${avatar} post=${post} />
<${content} post=${post} />
</li>
`)}
</ul>
`
}
body {
font-family: sans-serif;
font-size: 1.25em;
padding-left: 1.5em;
padding-right: 1.5em;
}
h1 {
font-size: 2.5em;
text-align: center;
}
ul {
padding: 0;
}
li {
display: flex;
align-items: flex-start;
column-gap: 1em;
padding: 1em;
margin-bottom: 1em;
background-color: #ccc;
border-radius: 1em;
}
Notice how we split up the styles also and encapsulated those that pertain to the components in the components themselves, scoping them to the component itself using the @scope CSS at-rule.
While @scope is great for components as it allows you to easily customise the appearance of individual instances of components and encapsulate your styles especially when other people might use your components in their projects, it must be used with inline styles.
You cannot use @scope with regular style tags as they get hoisted to the head by Kitten.
If you donāt need to customise the styles of individual instances of your components, you can use regular style tags in your components, in which case Kitten will intelligently deduplicate the styles and only include one style tag in the head. So if you have 1,000 of the same component, you wonāt get 1,000 copies of the styles also. (If you use <style inline>, you will, by design.)
Once youāve separated your page into components and fragments, thereās no rule that says they must all remain in the same file. Since theyāre just snippets of JavaScript, you can put each one in its own file and import them in to make your project easier to organise and maintain.
In Kitten, we call a custom HTML element that can be included as a custom tag a component and any other snippet of HTML, CSS, or JavaScript a fragment. They go in .component.js and .fragment.js files, respectively.
A good rule of thumb is that if an element will be reused in multiple places on a page and/or in multiple pages, itās a component. If it is a part of page that might be rendered separately from it (or makes sense to separate for organisational reasons to make maintenance easier), itās a fragment.
If youāre using a component for layout, you can also use a .layout.js extension. Like every other Kitten-specific extension, these are just JavaScript files but Kitten knows that these are server-side routes and should not be served as client-side JavaScript.
So hereās one way we could organise the code:
import styles from './index.fragment.css'
import avatar from './avatar.component.js'
import content from './content.component.js'
export default async function () {
const postsResponse = await fetch('https://mastodon.ar.al/api/v1/timelines/public')
const posts = await postsResponse.json()
return kitten.html`
<${styles} />
<h1>Aralās Public Fediverse Timeline</h1>
<ul>
${posts.map(post => kitten.html`
<li>
<${avatar} post=${post} />
<${content} post=${post} />
</li>
`)}
</ul>
`
}
export default function ({ post }) {
return kitten.html`
<a href='${post.account.url}'>
<img
src='${post.account.avatar}'
alt='${post.account.username}ās avatar'
>
<style inline>
@scope {
img {
width: 8em;
border: 6px solid #aaa;
border-radius: 1em;
}
}
</style>
</a>
`
}
export default function ({ post }) {
return kitten.html`
<div>
${kitten.safelyAddUntrustedHtml(post.content)}
${post.media_attachments.map(media => (
media.type === 'image' && kitten.html`
<img src='${media.url}' alt='${media.description ?? ''}'>
`
))}
<style inline>
@scope {
/* Grow to fill all remaining space in the row. */
flex: 1;
p { line-height: 1.5; }
p:first-of-type { margin-top: 0; }
a {
background-color: #8bdaff;
color: black;
text-decoration: none;
padding: 0.25em;
border-radius: 0.25em;
/* Make sure posts donāt overflow their containers. */
overflow-wrap: anywhere;
}
img { max-width: 100%; }
}
</style>
</div>
`
}
body {
font-family: sans-serif;
font-size: 1.25em;
padding-left: 1.5em;
padding-right: 1.5em;
}
h1 {
font-size: 2.5em;
text-align: center;
}
ul {
padding: 0;
}
li {
display: flex;
align-items: flex-start;
column-gap: 1em;
padding: 1em;
margin-bottom: 1em;
background-color: #ccc;
border-radius: 1em;
}
Separating your pages into components and fragments should make your sites easier to maintain but this does come at the expense of locality of behaviour.
Locality of behaviour is about keeping related functionality together so you can easily read through the code in a linear fashion. So there might be times you might keep simple components and fragments in the same file as your page routes.
Donāt be afraid to experiment with how you organise your own projects. Soon, youāll develop a knack for knowing when youāve hit the sweet spot.
The component idiom in Kitten is for your components to have a single root element.
You can have a component with multiple root elements, but Kitten canāt always stream it (see Streaming HTML):
When you send a stateless component with multiple root elements with the pageās
send()method or a componentāssendToPage()method, Kitten sends nothing and logs the following error:ā Error: Cannot stream component update as component does not have a single root DOM node.When a stateful component with multiple root elements updates itself, Kitten only morphs the first of them into place. It sends the update and logs the following warning:
ā ļø <component>: html() returns <number> root elements. update() only morphs the first into place. Wrap them in one root element.
In addition to the custom properties (or āpropsā) you can define and set on your components, you can also pass all other props (HTML attributes) to an element of your choosing within your component.
This is very useful as it means that you can use regular HTML attributes to listen for events and use frameworks like htmx and Alpine.js with your components.
Kittenās Streaming HTML workflow actually uses htmx under the hood and Kitten also has first-class support for Alpine.js if you ever find yourself having to write custom client-side JavaScript. (You can, of course, always use vanilla client-side JavaScript in your pages.)
Take this simple example of a custom button. Notice how weāre passing the rest of the props ā obtained using the spread operator ā to the button element. This allows us to listen for the onclick event and display an alert dialogue in response to it.
export default function ({label = 'Missing label prop (label="ā¦")', ...props}) {
return kitten.html`
<div>
<button ...${props}>${label}</button>
<style inline>
@scope {
button {
border: 2px solid darkviolet;
color: darkviolet;
border-radius: 0.25em;
padding: 0.25em 0.5em;
background: transparent;
font-size: 2em;
}
}
</style>
</div>
`
}
import myButton from './myButton.component.js'
export default () => kitten.html`
<h1>My button</h1>
<${myButton} label='Press me!' onclick='alert("Thank you for pressing me!")' />
`
If, instead you wanted to use Alpine.js, this is how your index page would look:
import myButton from './myButton.component.js'
export default () => kitten.html`
<page alpinejs>
<h1>My button</h1>
<${myButton}
label='Press me!'
x-data
@click='alert("Thank you for pressing me!")'
/>
`
For more advanced components, you can, of course, use htmx and Alpine.js inside your component and expose just your custom interface.
In addition to regular (JavaScript) components and fragments, you can also create HTML, CSS, and Markdown fragments.
These are plain old HTML, CSS, and Markdown files that you can nevertheless import as if they were JavaScript functions. (Kitten uses the magic of ES Module Loaders to convert them to JavaScript modules.
You place HTML fragments into files that have a .fragment.html extension, CSS fragments into files that have a .fragment.css extension, and Markdown files into ā you guessed it ā files that have a .fragment.md extension.
You already used the CSS fragment in this example.
Like JavaScript fragments, HTML, CSS, and Markdown fragments are ignored by the router.
For an example of their use, see the examples/html-and-css-fragments and examples/fragment-loaders in the Kitten source code.
Now that you have a taste for what you can do with components and fragments, letās leave the Fetchiverse example aside for the time being and take a look at a powerful feature of components and fragments that you havenāt used yet: composition using slots.
Next tutorial: Slots