Step 2 — Components

In Step 2 (~10 minutes), you split the monolithic TodoApp from Step 1 into focused UI building blocks: TodoItem for a single task row and TodoToolbar for the remaining count plus customizable filter controls.

The live interactive demo and complete source for this step appear at the bottom of the page.

Defining multi-word components with @define_component()#

Call @define_component() with parentheses so WebComPy derives the Light DOM custom element tag from the PascalCase function name (TodoItem becomes <todo-item>, TodoToolbar becomes <todo-toolbar>, and TodoApp becomes <todo-app>):

from webcompy.components import ComponentContext, define_component
from webcompy.elements import html
from webcompy.signal import use_computed


@define_component()
def TodoItem(context: ComponentContext[TaskItem]):
    task = context.props
    return html.LI(
        {"class": use_computed(lambda: "todo-item done" if task["done"].value else "todo-item")},
        html.SPAN({}, task["title"]),
    )

Moving .todo-item rules onto TodoItem.scoped_style keeps item styling encapsulated alongside its markup.

Passing props and default slots into TodoToolbar#

TodoToolbar receives a remaining signal in context.props and renders custom filter controls passed through its "default" slot via context.slots("default", fallback=lambda: None):

from typing import TypedDict

from webcompy.signal import SignalBase


class ToolbarProps(TypedDict):
    remaining: SignalBase[int]


@define_component()
def TodoToolbar(context: ComponentContext[ToolbarProps]):
    remaining = context.props["remaining"]
    return html.DIV(
        {"class": "toolbar"},
        html.SPAN({"class": "remaining"}, use_computed(lambda: f"{remaining.value} remaining")),
        context.slots("default", fallback=lambda: None),
    )

In TodoApp, pass the filter button strip inside slots={"default": lambda: ...}:

TodoToolbar(
    {"remaining": remaining},
    slots={
        "default": lambda: html.DIV(
            {"class": "filters"},
            filter_button("All", "all"),
            filter_button("Active", "active"),
            filter_button("Done", "done"),
        )
    },
)

Keeping the navigation region as a slot prepares TodoToolbar for Step 3, where those buttons become router links without rewriting TodoToolbar itself.

Next steps#

  • Replace local filter buttons with client-side routes in Step 3: Navigation.
  • Learn more about component tag derivation, props, slots, and :host styling in Components.

Step 2 Live Demo

Demo
Code
from typing import TypedDict

from webcompy.app import WebComPyApp
from webcompy.components import ComponentContext, define_component
from webcompy.elements import html, repeat
from webcompy.signal import Signal, SignalBase, use_computed, use_reactive_list, use_state


class TaskItem(TypedDict):
    id: int
    title: str
    done: Signal[bool]


class ToolbarProps(TypedDict):
    remaining: SignalBase[int]


@define_component()
def TodoItem(context: ComponentContext[TaskItem]):
    task = context.props
    return html.LI(
        {"class": use_computed(lambda: "todo-item done" if task["done"].value else "todo-item")},
        html.SPAN({}, task["title"]),
    )


TodoItem.scoped_style = {
    ".todo-item": {
        "padding": "var(--space-2) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-card)",
        "color": "var(--color-fg)",
        "margin-bottom": "var(--space-2)",
    },
    ".todo-item.done": {"text-decoration": "line-through", "color": "var(--color-fg-muted)"},
}


@define_component()
def TodoToolbar(context: ComponentContext[ToolbarProps]):
    remaining = context.props["remaining"]
    return html.DIV(
        {"class": "toolbar"},
        html.SPAN({"class": "remaining"}, use_computed(lambda: f"{remaining.value} remaining")),
        context.slots("default", fallback=lambda: None),
    )


TodoToolbar.scoped_style = {
    ".toolbar": {
        "display": "flex",
        "justify-content": "space-between",
        "align-items": "center",
        "gap": "var(--space-2)",
        "margin": "var(--space-3) 0",
    },
    ".filters": {"display": "flex", "gap": "var(--space-2)"},
    ".filters button": {
        "padding": "var(--space-1) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-card)",
        "color": "var(--color-fg)",
        "cursor": "pointer",
    },
    ".filters button.active": {
        "background": "var(--color-accent)",
        "border-color": "var(--color-accent)",
        "color": "var(--color-on-accent)",
        "font-weight": "600",
    },
}


@define_component()
def TodoApp(_: ComponentContext[None]):
    tasks = use_reactive_list(
        lambda: [
            TaskItem(id=1, title="Read Getting Started", done=Signal(True)),
            TaskItem(id=2, title="Build TodoApp state", done=Signal(False)),
            TaskItem(id=3, title="Split into components", done=Signal(False)),
        ]
    )
    filter_mode = use_state(lambda: "all")

    remaining = use_computed(lambda: sum(1 for t in tasks.value if not t["done"].value))
    visible_tasks = use_computed(
        lambda: [
            t
            for t in tasks.value
            if filter_mode.value == "all"
            or (filter_mode.value == "active" and not t["done"].value)
            or (filter_mode.value == "done" and t["done"].value)
        ]
    )

    def filter_button(label: str, mode: str):
        return html.BUTTON(
            {
                "type": "button",
                "class": use_computed(lambda: "active" if filter_mode.value == mode else ""),
                "@click": lambda _ev: setattr(filter_mode, "value", mode),
            },
            label,
        )

    return html.DIV(
        {"class": "todo-shell"},
        html.H1({}, "ToDo Tutorial"),
        TodoToolbar(
            {"remaining": remaining},
            slots={
                "default": lambda: html.DIV(
                    {"class": "filters"},
                    filter_button("All", "all"),
                    filter_button("Active", "active"),
                    filter_button("Done", "done"),
                )
            },
        ),
        html.UL(
            {"class": "task-list"},
            repeat(visible_tasks, lambda task, _key: TodoItem(task), key=lambda task: task["id"]),
        ),
    )


TodoApp.scoped_style = {
    ".todo-shell": {
        "max-width": "32rem",
        "padding": "var(--space-3)",
        "font-family": "var(--font-sans)",
        "color": "var(--color-fg)",
    },
    ".task-list": {"list-style": "none", "padding": "0", "margin": "0"},
}

app = WebComPyApp(root_component=TodoApp)
app.run()