Theme
Version
GitHub PyPI Discord

Card

Use this pattern for a piece of markup you repeat across pages, such as a card, a panel, or a badge. The Card takes an accent color, wraps whatever HTML you put inside <c-Card>, and brings its own CSS. It runs with Citry alone; no web framework is needed.

from citry import Component, SlotInput

class Card(Component):
    class Kwargs:
        accent: str

    class Slots:
        default: SlotInput

    def css_data(
        self,
        kwargs: Kwargs,
        slots: Slots,
    ) -> dict[str, str]:
        return {"accent": kwargs.accent}

    template = """
      <article class="demo-card">
        <c-slot />
      </article>
    """

    css = """
      .demo-card {
        max-width: 24rem;
        padding: 1rem 1.25rem;
        border: 1px solid
          color-mix(in srgb, currentColor 20%, transparent);
        border-top: 0.25rem solid var(--accent);
        border-radius: 8px;
        background: Canvas;
        color: CanvasText;
        font-family: system-ui, sans-serif;
      }

      .demo-card__title {
        margin: 0 0 0.25rem;
        font-size: 1.1rem;
      }

      .demo-card__body {
        margin: 0;
      }
    """

The lines to notice:

  • accent sets the top border color. css_data() passes it to the CSS as var(--accent), so each Card on a page can have its own color.
  • Everything between <c-Card> and </c-Card> appears where the template has <c-slot />.
  • Citry adds the Card's styles to the page; <c-css /> marks where. Those styles apply to anything on the page with the demo-card class, so the example uses that specific name rather than a broad one such as .card.

If a template leaves out accent or the content, Citry raises an error that names what is missing as soon as it compiles that template. The accent: str annotation helps your editor and type checker, and Citry checks the value when the card renders: accent=123 raises a TypeError that names Card.Kwargs.accent.

For a guided walkthrough, read Your first component. For details, see component inputs, slots, SlotInput, and component CSS.