Components
Split an interface into files: give each piece inputs (props), let it report actions (events), and pass it content (slots).
janelkit Included with janelkit. Nothing extra to install. What you will learn
- When to extract a component and how to import it.
- How props pass data in, and why a child cannot change them.
- How events report actions back to the parent.
- How slots let the parent supply content, and how styles reach a component’s root.
Why split an interface into components
A component is one .janel file: a named piece of interface with its own data and logic. You already wrote one, App.janel. Extract a new component when a piece repeats, when a file becomes hard to read, or when a piece has a clear job of its own, such as a list row, a card or an editor.
Each use of a component is an instance with its own script run and its own state. A component talks to its parent through a small, explicit contract: props are the data it receives, events are the actions it reports, and slots are the content the parent places inside it.
| Direction | Mechanism | Example |
|---|---|---|
| Parent → child: data | Prop | <Greeting name="Ada" /> |
| Child → parent: something happened | Event | <NameEditor on:saved={accept} /> |
| Parent → child: content | <slot> | <Card><text>Inside the card</text></Card> |
Your first component, used twice
Save the child in src/components and import it in the parent’s script with Python syntax. Use it as a tag whose name starts with a capital letter. The compiler finds the .janel file, compiles it once and checks every use.
The two counters below come from the same file but are separate instances: each has its own count.
<script lang="python">
from janelkit import state
count = state(0)
</script>
<row gap={8}>
<text>Count: {count}</text>
<button on:click={count += 1}>Add</button>
</row>Copy<script lang="python">
from components.Counter import Counter
</script>
<window title="Two counters" width={360} height={200}>
<column padding={24} gap={12}>
<Counter />
<Counter />
</column>
</window>CopyComplete example in 2 files · Save each one at the path shown above it.
How it works
- from components.Counter import Counter loads src/components/Counter.janel. from components import Counter does the same; from .Counter import Counter is relative to the importing file.
- Counter’s root is a <row>, not a <window>: components used inside others become part of a window.
- Each <Counter /> runs Counter’s script again, so each gets its own count.
What you should see
Two lines show Count: 0. Clicking the first Add changes only the first line to Count: 1.
Props: data the parent passes in
A prop is an input chosen by the parent. In the child, declare it at the top level of the script: name = prop() makes it required, name = prop(default) makes it optional. A type annotation such as name: str = prop() lets the compiler reject wrong literal values, and wrong values that arrive while running are logged.
In the child, read a prop like state: name.value in Python, {name} in the template. In the parent, a quoted value or a fixed literal is passed once; an expression in braces is re-evaluated, so the child updates when the parent’s data changes.
<script lang="python">
from janelkit import prop
name: str = prop() # required
excited: bool = prop(False) # optional, False by default
</script>
<text>Hello, {name}{"!" if excited else "."}</text>Copy<script lang="python">
from janelkit import state
from components.Greeting import Greeting
who = state("Ada")
</script>
<window title="Props" width={380} height={220}>
<column padding={24} gap={12}>
<textfield bind:value={who} label="Name" />
<Greeting name={who} />
<Greeting name="Grace" excited />
</column>
</window>CopyComplete example in 2 files · Save each one at the path shown above it.
How it works
- name={who} is an expression, so the first greeting follows the text field.
- name="Grace" is a constant. excited is a bare attribute, which means excited={True}.
- Leaving out a required prop, or passing a prop the child does not declare, is a compile error that names the problem.
What you should see
Hello, Ada. and Hello, Grace! at first. Replacing the name with Lin changes the first line to Hello, Lin.
Common mistakes
- Props are read-only in the child: assigning name.value raises PropError. Copy the value into local state to edit it, or ask the parent with an event.
- Declare props at the top level of the script, never inside a function.
Events: the child reports, the parent decides
A child does not change its parent’s data. It reports what happened, and the parent decides what to do. Declare saved = event() in the child and call saved.emit(value); the parent listens with on:saved={handler}, and the handler receives the emitted values. An event nobody listens to does nothing.
This keeps one owner for each piece of data. Below, the editor keeps a local draft while the user types; only Save sends the result up, and the parent updates its own state.
<script lang="python">
from janelkit import prop, event, state
name = prop()
saved = event()
draft = state(name.value) # a local copy the user can edit
def save():
saved.emit(draft.value.strip())
</script>
<row gap={8}>
<textfield bind:value={draft} label="New name" />
<button on:click={save}>Save</button>
</row>Copy<script lang="python">
from janelkit import state
from components.NameEditor import NameEditor
name = state("Ada")
def accept(value):
name.value = value
</script>
<window title="Events" width={420} height={200}>
<column padding={24} gap={12}>
<text>Saved: {name}</text>
<NameEditor name={name} on:saved={accept} />
</column>
</window>CopyComplete example in 2 files · Save each one at the path shown above it.
How it works
- draft starts as a copy of the prop. Typing changes the draft, not the parent.
- saved.emit(...) calls the parent’s handler with the cleaned-up text.
- accept is the parent’s decision: it stores the value in the parent’s own state.
What you should see
Typing Ada Byron in the field leaves Saved: Ada unchanged. Clicking Save changes it to Saved: Ada Byron.
Common mistakes
- The draft copies the prop once, when the editor is created. If the parent changes name later, the draft does not follow; recreate the editor with {#key} if that is what you need.
Slots: content the parent places inside
A prop passes a value; a slot passes a piece of template. In the child, <slot /> marks where the parent’s content goes, and <slot name="footer"> is a named slot. Content written inside a <slot> tag is the fallback, shown when the parent supplies nothing.
In the parent, elements without a slot attribute go to the default slot; an element with slot="footer" goes to that slot. <slot> content belongs to the parent: it sees the parent’s state and functions, even though the child decides where it appears.
<script lang="python">
from janelkit import prop
title = prop()
</script>
<column class="card">
<text class="title">{title}</text>
<slot />
<row gap={8}><slot name="footer"><text>No actions</text></slot></row>
</column>
<style>
.card { padding: 16px; gap: 8px; border: 1px solid var(--border); border-radius: var(--radius); }
.title { font-weight: 600; }
</style>Copy<script lang="python">
from janelkit import state
from components.Card import Card
message = state("Ready")
</script>
<window title="Slots" width={420} height={320}>
<column padding={24} gap={12}>
<Card title="Task">
<text>{message}</text>
<button slot="footer" on:click={message = "Done"}>Finish</button>
</Card>
<Card title="Empty card" />
</column>
</window>CopyComplete example in 2 files · Save each one at the path shown above it.
How it works
- <text>{message}</text> has no slot attribute, so it goes to the default <slot />.
- The <button> with slot="footer" replaces the fallback text of the footer slot.
- The second card supplies nothing, so its footer shows the fallback No actions.
- message belongs to App; the card only decides where the content appears.
What you should see
The first card shows Ready and a Finish button; the second shows No actions. Finish changes Ready to Done.
Common mistakes
- Passing content to a slot name the child does not declare is a compile error: <Card> no admite el slot 'header' ("does not accept the slot"), followed by a hint that lists the slots the child declares.
- Filling one named slot twice is also a compile error. Wrap several elements for one slot in a <row> or <column> with that slot attribute.
Style and wire a component like an element
When a component’s root is a box (flex, <column> or <row>), attributes on its tag that it does not declare as props pass to that root: class, style, id, label, flex, width, height, visible, ref and native events such as on:click. That lets you use a component like a built-in element, and the parent’s style rules, including :hover, apply to the root.
If the component declares a prop or event with the same name, the declaration wins. Setting inherit_attrs = False in the child’s script turns this off. When the root is not a box, layout attributes go to an outer box that wraps the component, and an undeclared event is a compile error.
<script lang="python">
from janelkit import prop
title = prop()
</script>
<column class="tile">
<text>{title}</text>
</column>
<style>
.tile { padding: 16px; border-radius: var(--radius); background: var(--surface); }
</style>Copy<script lang="python">
from janelkit import state
from components.Tile import Tile
opened = state(0)
</script>
<window title="Tiles" width={380} height={240}>
<column padding={24} gap={12}>
<Tile title="Reports" class="featured" on:click={opened += 1} label="Open reports" />
<text>Opened {opened} times</text>
</column>
</window>
<style>
.featured { border: 1px solid var(--primary); }
.featured:hover { background: var(--surface-alt); }
</style>CopyComplete example in 2 files · Save each one at the path shown above it.
How it works
- Tile declares only title. class, on:click and label pass to its root <column>.
- App’s .featured rule styles Tile’s root, including the :hover state.
- on:click works without Tile declaring an event, because the root is a box.
What you should see
The tile has an accent border and changes background under the pointer. Each click increases Opened 0 times.
Decide where state should live
A component keeps its local state for as long as the instance exists. Updating a prop does not run the child’s script again, so values copied from props during setup do not follow later changes. Removing a component with {#if} destroys its state; {#hidden} keeps it; {#key} recreates it on purpose.
Before building a component, decide who owns each value. Data the rest of the application needs belongs in the parent or in a shared module; temporary edits belong in the child until the user confirms them.
- List the values the parent owns and declare a prop for each.
- Keep temporary edits in local state.
- Emit an event at the moment of decision, such as Save.
- Test two instances side by side, and removing and showing the component again.
Learn moreConditions and lists ↗Sharing data ↗
Key points
- A component is a .janel file; import it by its path under src and use it as a tag.
- Props pass data in and are read-only; events report actions out.
- Slots pass content in; tag attributes reach a component’s root box.
English documentation based on the JanelKit source. For additional details, browse the source docs ↗.