{# Tungsten UI kit — the small building blocks every page reuses. Import with: {% import "tungsten/components/ui.html" as ui %} #} {# ---------------------------------------------------------------- buttons #} {% macro button_classes(color='primary', style='button', size='md', outlined=false) -%} {%- set sizes = {'sm': 'px-2.5 py-1.5 text-xs', 'md': 'px-3 py-2 text-sm', 'lg': 'px-4 py-2.5 text-sm'} -%} {%- if style == 'link' -%} inline-flex items-center gap-1.5 text-sm font-semibold {{ colors.pick(colors.LINK, color) }} {%- elif style == 'icon' -%} inline-flex h-9 w-9 items-center justify-center rounded-full bg-white ring-1 ring-gray-950/10 transition dark:bg-white/5 dark:ring-white/10 {{ colors.pick(colors.ICON_BUTTON, color if color != 'primary' else 'gray') }} {%- elif style == 'soft' -%} tw-btn {{ sizes.get(size, sizes.md) }} {{ colors.pick(colors.SOFT_BUTTON, color) }} {%- elif style == 'dropdown' -%} flex w-full items-center gap-2 rounded-xl px-2.5 py-2 text-start text-sm transition hover:bg-gray-50 dark:hover:bg-white/5 {{ colors.pick(colors.TEXT, color) if color not in ('primary', 'gray') else 'text-gray-700 dark:text-gray-200' }} {%- elif outlined -%} tw-btn {{ sizes.get(size, sizes.md) }} bg-white ring-1 ring-gray-950/10 dark:bg-transparent dark:ring-white/20 {{ colors.pick(colors.LINK, color) }} {%- else -%} tw-btn {{ sizes.get(size, sizes.md) }} {{ colors.pick(colors.BUTTON, color) }} {%- endif -%} {%- endmacro %} {% macro button(label, color='primary', icon_name=None, href=None, type='button', style='button', size='md', attrs_='', outlined=false) %} {% set label = __(label) %} {% if href %} {{ icon(icon_name, 'h-4 w-4' if size != 'lg' else 'h-5 w-5') }}{{ label }} {% else %} {% endif %} {% endmacro %} {% macro icon_button(icon_name, label, color='gray', href=None, attrs_='', bordered=true) %} {% set label = __(label) %} {% set cls = button_classes(color, 'icon') if bordered else 'tw-icon-btn ' ~ colors.pick(colors.ICON_BUTTON, color) %} {% if href %} {{ icon(icon_name, 'h-4 w-4') }} {% else %} {% endif %} {% endmacro %} {# ---------------------------------------------------------------- badges #} {% macro badge(text, color='gray', icon_name=None, size='sm') %} {{ icon(icon_name, 'h-3.5 w-3.5') }}{{ text }} {% endmacro %} {% macro count_pill(n, color='primary', tooltip=none) %} {% if n is not none and n != '' %} {{ n }} {% endif %} {% endmacro %} {# ---------------------------------------------------------------- avatar #} {% macro avatar(name, url=None, size='h-9 w-9', text='text-sm') %} {% if url %} {{ name }} {% else %} {% set parts = (name or '?').split() %} {% set tones = ['bg-primary-100 text-primary-700 dark:bg-primary-500/20 dark:text-primary-300', 'bg-info-100 text-info-700 dark:bg-info-500/20 dark:text-info-300', 'bg-success-100 text-success-700 dark:bg-success-500/20 dark:text-success-300', 'bg-purple-100 text-purple-700 dark:bg-purple-500/20 dark:text-purple-300', 'bg-pink-100 text-pink-700 dark:bg-pink-500/20 dark:text-pink-300', 'bg-teal-100 text-teal-700 dark:bg-teal-500/20 dark:text-teal-300', 'bg-warning-100 text-warning-700 dark:bg-warning-500/20 dark:text-warning-300'] %} {% set ns = namespace(h=0) %}{% for ch in (name or '?') %}{% set ns.h = ns.h + ch | string | length * loop.index * (ch | ord_value) %}{% endfor %} {{- (parts[0][0] ~ (parts[-1][0] if parts | length > 1 else '')) | upper -}} {% endif %} {% endmacro %} {# ---------------------------------------------------------------- cards #} {# Card title block: optional icon tile, heading and a muted line under it. #} {% macro card_heading(heading, description=None, icon_name=None, icon_color='primary') %}
{% if icon_name %}{{ icon(icon_name, 'h-[1.125rem] w-[1.125rem]') }}{% endif %}

{{ heading }}

{% if description %}

{{ description }}

{% endif %}
{% endmacro %} {% macro card_link(url, label='View all') %} {{ __(label) }}{{ icon('arrow-right', 'h-4 w-4') }} {% endmacro %} {% macro card(heading=None, description=None, padding='p-6', classes='', link=None, link_label='View all', icon_name=None, icon_color='primary') %}
{% if heading %}
{{ card_heading(heading, description, icon_name, icon_color) }} {% if link %}{{ card_link(link, link_label) }}{% endif %}
{% endif %}
{{ caller() }}
{% endmacro %} {# ---------------------------------------------------------------- dropdown #} {# twDropdown (tungsten.js) floats the open panel next to its trigger, so a table's scroll box can't cut it off. Picking a link or button in the panel closes it. #} {% macro dropdown(align='end', width='w-56', classes='') %}
{{ caller('trigger') }}
{{ caller('panel') }}
{% endmacro %} {# ---------------------------------------------------------------- headers #} {% macro breadcrumbs(items) %} {% if items %} {# earlier crumbs keep one line; only the last one is cut with "…" (#143) #} {% endif %} {% endmacro %} {% macro empty_state(heading, description=None, icon_name='inbox') %}
{{ icon(icon_name, 'h-6 w-6') }}

{{ heading }}

{% if description %}

{{ description }}

{% endif %} {% if caller %}
{{ caller() }}
{% endif %}
{% endmacro %} {# ---------------------------------------------------------------- stat card #} {% macro trend(value, direction='up', color=None, suffix='vs last month') %} {% set c = color or ('success' if direction == 'up' else 'danger') %}

{{ icon('arrow-up' if direction == 'up' else 'arrow-down', 'h-4 w-4') }}{{ value }} {% if suffix %}{{ suffix }}{% endif %}

{% endmacro %} {% macro sparkline(points, color='primary', id='spark') %} {% if points and points | length > 1 %} {% set lo = points | min %}{% set hi = points | max %}{% set span = (hi - lo) or 1 %} {% set n = points | length - 1 %} {% set ns = namespace(d='') %} {% for p in points %} {% set x = (loop.index0 / n * 100) | round(2) %}{% set y = (36 - (p - lo) / span * 32) | round(2) %} {% set ns.d = ns.d ~ ('M' if loop.first else ' L') ~ x ~ ' ' ~ y %} {% endfor %} {% endif %} {% endmacro %} {% macro stat_card(label, value, icon_name=None, color='primary', description=None, trend_value=None, trend_direction='up', trend_color=None, chart=None, url=None, id='stat', variant='default') %} {% set tag = 'a' if url else 'div' %} {% set description = description or (__('vs last month') if trend_value else none) %} {% set filled = variant in ('brand', 'dark') %} <{{ tag }} {% if url %}href="{{ url }}"{% endif %} class="{{ 'tw-card-brand' if variant == 'brand' else ('tw-card-dark relative overflow-hidden' if variant == 'dark' else 'tw-card') }} group relative block overflow-hidden p-5 {{ 'tw-card-hover' if url }}" data-tw-stat="{{ variant }}">

{{ label }}

{% if icon_name %} {{ icon(icon_name, 'h-[1.125rem] w-[1.125rem]') }} {% endif %}

{{ value }}

{% if trend_value %} {% set c = trend_color or ('success' if trend_direction == 'up' else 'danger') %} {{ icon('arrow-up' if trend_direction == 'up' else 'arrow-down', 'h-3 w-3') }}{{ trend_value }} {% endif %}
{% if description or chart %}{# no empty footer row under the number (#142) #}
{% if description %}

{{ description }}

{% else %}{% endif %} {% if chart %}
{{ sparkline(chart, trend_color or color, id) }}
{% endif %}
{% endif %} {% endmacro %} {# ---------------------------------------------------------------- toolbar input #} {% macro search_input(name, value='', placeholder=None, id=None, attrs_='', shortcut=None) %} {% set placeholder = placeholder or __('Search...') %} {% endmacro %} {# ================================================================ modern components Small building blocks in the style of the dashboard: pills, chips, rings, gauges, bars and lists. Every macro works in light and dark mode. See docs/ui-components.md. #} {# A trend pill on its own: ui.trend_pill('12%', 'up') #} {% macro trend_pill(value, direction='up', color=None) %} {% set c = color or ('success' if direction == 'up' else 'danger') %} {{ icon('arrow-up' if direction == 'up' else 'arrow-down', 'h-3 w-3') }}{{ value }} {% endmacro %} {# A colored dot: ui.dot('success') #} {% macro dot(color='primary') %}{% endmacro %} {# A small rounded label, optionally with an icon or a colored dot: ui.chip('June 2025', 'calendar') #} {% macro chip(text, icon_name=None, dot_color=None, href=None) %} {% set tag = 'a' if href else 'span' %} <{{ tag }} {% if href %}href="{{ href }}"{% endif %} class="tw-chip {{ 'transition hover:bg-gray-50 dark:hover:bg-white/10' if href }}"> {% if dot_color %}{{ dot(dot_color) }}{% endif %}{{ icon(icon_name, 'h-3.5 w-3.5') }}{{ text }} {% endmacro %} {# A keyboard key: ui.kbd('⌘K') #} {% macro kbd(text) %}{{ text }}{% endmacro %} {# A thin line with an optional label in the middle. #} {% macro divider(label=None) %} {% endmacro %} {# Grey loading placeholder lines. #} {% macro skeleton(lines=3) %} {% endmacro %} {# Pill navigation. items: list of dicts {label, url, active, icon, badge}. muted=true gives a grey bar. #} {% macro pill_tabs(items, muted=false, label='Tabs') %} {% endmacro %} {# A segmented control that posts with the form: ui.segmented('period', {'7': '7 days', '30': '30 days'}, '30') #} {% macro segmented(name, options, value=None) %}
{% for v, text in options.items() %} {% endfor %}
{% endmacro %} {# An on/off switch for forms (a styled checkbox). #} {% macro switch(name, checked=false, label=None, description=None, value='1') %} {% endmacro %} {# A ring that fills to `percent`, with text in the middle. #} {% macro progress_ring(percent, color='primary', size=96, stroke=10, label=None, value=None) %} {% set r = (size - stroke) / 2 %}{% set c = 2 * 3.14159265 * r %} {% set p = [[percent or 0, 0] | max, 100] | min %} {% endmacro %} {# A half-circle gauge. on_dark=true draws it for a dark or brand card. #} {% macro gauge(percent, color='primary', value=None, caption=None, label=None, on_dark=false) %} {% set p = [[percent or 0, 0] | max, 100] | min %} {% set len = 3.14159265 * 80 %} {% endmacro %} {# A labelled bar: ui.meter('Enterprise deals', 12, 'danger', '12%') #} {% macro meter(label, percent, color='primary', value=None) %} {% set p = [[percent or 0, 0] | max, 100] | min %}
{{ label }} {{ value if value is not none else (p | round | int ~ '%') }}
{% endmacro %} {# Overlapping avatars. people: list of dicts {name, avatar}. #} {% macro avatar_group(people, max=4, size='h-9 w-9') %}
{% for person in people[:max] %}{{ avatar(person.name, person.avatar, size, 'text-xs') }}{% endfor %} {% if people | length > max %}+{{ people | length - max }}{% endif %}
{% endmacro %} {# One row in a list: an avatar or icon, a title and muted text, and a value at the end. #} {% macro list_row(title, description=None, value=None, icon_name=None, color='primary', avatar_url=None, avatar_name=None, href=None, badge_text=None, badge_color='gray') %} {% set tag = 'a' if href else 'div' %} <{{ tag }} {% if href %}href="{{ href }}"{% endif %} class="flex items-center gap-3 rounded-2xl px-2 py-2 {{ 'transition hover:bg-gray-50 dark:hover:bg-white/5' if href }}"> {% if avatar_name or avatar_url %}{{ avatar(avatar_name or title, avatar_url) }} {% elif icon_name %}{{ icon(icon_name, 'h-4 w-4') }}{% endif %}

{{ title }}

{% if description %}

{{ description }}

{% endif %}
{% if badge_text %}{{ badge(badge_text, badge_color) }}{% endif %} {% if value is not none %}{{ value }}{% endif %} {% endmacro %} {# One timeline entry (use inside
    ). last=true hides the line below. #} {% macro timeline_item(title, description=None, time=None, icon_name='circle', color='primary', last=false, href=None) %}
  1. {% if not last %}{% endif %} {{ icon(icon_name, 'h-4 w-4') }}
    {% if href %}{{ title }}{% else %}

    {{ title }}

    {% endif %} {% if time %}{{ time }}{% endif %}
    {% if description %}

    {{ description }}

    {% endif %}
  2. {% endmacro %} {# Rounded columns, one highlighted. columns: list of dicts {label, display, height (0-100), active}. #} {% macro columns(cols, color='primary', height='12rem', fill=false) %} {#- fill: grow with a flex-column parent (a taller card), never shorter than height -#}
    {% for col in cols %}
    {% if col.active %}{{ col.display }}{% endif %}
    {{ col.label }}
    {% endfor %}
    {% endmacro %} {# A colored banner card. variant: soft | brand | dark #} {% macro banner(heading, text=None, button_label=None, button_url=None, eyebrow=None, variant='soft', color='primary', button_icon='arrow-up-right') %} {% set filled = variant in ('brand', 'dark') %}
    {% if eyebrow %}{{ eyebrow }}{% endif %}

    {{ heading }}

    {% if text %}

    {{ text }}

    {% endif %}
    {% if button_label %} {% endif %}
    {% endmacro %} {# The current week as a strip of days. days: list of dicts {name, day, today}. #} {% macro week_strip(days) %}
    {% for d in days %}
    {{ d.name[:2] if d.name | length > 3 else d.name }} {{ d.day }}
    {% endfor %}
    {% endmacro %} {# A message box. color: info | success | warning | danger | primary | gray #} {% macro alert(heading, text=None, color='info', icon_name=None) %} {% set icons = {'info': 'info', 'success': 'circle-check', 'warning': 'triangle-alert', 'danger': 'circle-x'} %}
    {{ icon(icon_name or icons.get(color, 'info'), 'h-5 w-5') }}

    {{ heading }}

    {% if text %}

    {{ text }}

    {% endif %}
    {% endmacro %} {# A compact number with a label and optional trend, for use inside cards. #} {% macro kpi(label, value, trend_value=None, trend_direction='up', color=None, accent=None) %}

    {{ label }}

    {{ value }} {% if trend_value %}{{ trend_pill(trend_value, trend_direction, color) }}{% endif %}
    {% endmacro %} {# ================================================================ page kit Bigger blocks for whole pages: media and project cards, payment cards, invoices, pricing, profile headers, chat, order tracking and more. Flat and crisp like the rest of the panel; light, dark and phone. See docs/ui-components.md ("Page kit"). #} {# A card with an optional picture on top: places, products, projects. people: list of {name, avatar} drawn as an avatar group in the footer. #} {% macro media_card(title, text=None, image_url=None, eyebrow=None, meta=None, value=None, href=None, people=None, button_label=None, image_alt='') %}
    {% if image_url %}
    {{ image_alt }}
    {% endif %}
    {% if eyebrow %}

    {{ eyebrow }}

    {% endif %}

    {% if href and not button_label %}{{ title }}{% else %}{{ title }}{% endif %}

    {% if text %}

    {{ text }}

    {% endif %} {% if meta or value is not none or people or button_label %}
    {% if button_label %}{{ __(button_label) }} {% elif meta %}{{ icon('map-pin', 'h-4 w-4 shrink-0') }}{{ meta }}{% endif %} {% if people %}{{ avatar_group(people, 3, 'h-8 w-8') }}{% endif %} {% if value is not none %}{{ value }}{% endif %}
    {% endif %}
    {% endmacro %} {# A bank card drawn flat: ui.payment_card('7852', 'Jack Peterson', '11/27', 'Visa') #} {% macro payment_card(last4, holder, expires=None, brand=None, variant='dark') %} {% set dark = variant == 'dark' %}
    {{ icon('wifi', 'h-5 w-5 rotate-90') }} {% if brand %}{{ brand }}{% endif %}

    •••• •••• •••• {{ last4 }}

    {{ __('Card holder') }}

    {{ holder }}

    {% if expires %}

    {{ __('Expires') }}

    {{ expires }}

    {% endif %}
    {% endmacro %} {# A saved card row with an edit button: ui.payment_method('7852', 'Mastercard', '#') #} {% macro payment_method(last4, brand=None, edit_url=None, default=false) %}
    {{ icon('credit-card', 'h-4 w-4') }}

    •••• {{ last4 }}

    {% if brand %}

    {{ brand }}

    {% endif %}
    {% if default %}{{ badge(__('Default'), 'gray') }}{% endif %} {% if edit_url %}{{ icon_button('pencil', 'Edit', 'gray', edit_url, bordered=false) }}{% endif %}
    {% endmacro %} {# A centred tile: an icon, a label, a muted line and a value under a line ("Salary +₹2,000"). #} {% macro stat_tile(label, value, description=None, icon_name=None, color='gray') %}
    {% if icon_name %}{{ icon(icon_name, 'h-5 w-5') }}{% endif %}

    {{ label }}

    {% if description %}

    {{ description }}

    {% endif %}

    {{ value }}

    {% endmacro %} {# A row of numbers split by thin lines. items: list of {label, value, trend, direction}. Wraps to 2 columns on a phone. #} {% macro stat_strip(items) %}
    {% for it in items %}

    {{ it.label }}

    {{ it.value }}

    {% if it.trend %}
    {{ trend_pill(it.trend, it.direction or 'up') }}
    {% endif %}
    {% endfor %}
    {% endmacro %} {# A big reading with its unit: ui.metric('21', '°C', 'Living room', 'Temperature') #} {% macro metric(value, unit=None, label=None, description=None) %}

    {{ value }}{% if unit %}{{ unit }}{% endif %}

    {% if label %}

    {{ label }}

    {% endif %} {% if description %}

    {{ description }}

    {% endif %}
    {% endmacro %} {# An on/off tile for a device or a feature. It turns dark while switched on (no JavaScript). #} {% macro toggle_tile(name, label, icon_name='power', checked=false, description=None, value='1') %} {% endmacro %} {# One money movement. direction: in | out | pending. ui.transaction_row('Netflix', '27 Mar, 12:30', '₹2,500', 'out') #} {% macro transaction_row(title, time=None, amount=None, direction='in') %} {% set tone = {'in': 'text-success-600 dark:text-success-400', 'out': 'text-danger-600 dark:text-danger-400'}.get(direction, 'text-gray-500 dark:text-gray-400') %}
    {{ icon({'in': 'arrow-down-left', 'out': 'arrow-up-right'}.get(direction, 'clock'), 'h-4 w-4') }}

    {{ title }}

    {% if time %}

    {{ time }}

    {% endif %}
    {% if direction == 'pending' %}{{ amount or __('Pending') }} {% elif amount %}{{ '+' if direction == 'in' else '−' }} {{ amount }}{% endif %}
    {% endmacro %} {# Label / value rows. items: list of (label, value) pairs or {label, value}. total: a last row in bold. #} {% macro description_list(items, total=None, total_label='Total') %}
    {% for it in items %} {% set label = it.label if it.label is defined else it[0] %}{% set val = it.value if it.value is defined else it[1] %}
    {{ label }}
    {{ val }}
    {% endfor %} {% if total is not none %}
    {{ __(total_label) }}
    {{ total }}
    {% endif %}
    {% endmacro %} {# A printable invoice. seller / buyer: lists of lines. items: list of {name, qty, rate, amount}. totals: (label, value) rows before the total. #} {% macro invoice(number, issued=None, due=None, seller=[], buyer=[], items=[], totals=[], total=None, note=None, brand=None) %}
    {% if brand %}

    {{ brand }}

    {% endif %} {% for line in seller %}

    {{ line }}

    {% endfor %}

    {{ __('Billed to') }}

    {% for line in buyer %}

    {{ line }}

    {% endfor %}

    {{ __('Invoice no') }}

    {{ number }}

    {% if issued %}
    {{ __('Invoice date') }}
    {{ issued }}
    {% endif %} {% if due %}
    {{ __('Due date') }}
    {{ due }}
    {% endif %}
    {% for it in items %} {% endfor %}
    {{ __('Item') }}{{ __('Qty') }} {{ __('Rate') }}{{ __('Amount') }}
    {{ it.name }}{{ it.qty }} {{ it.rate }}{{ it.amount }}
    {{ description_list(totals, total) }}
    {% if note or caller %}
    {% if note %}

    {{ note }}

    {% endif %} {% if caller %}
    {{ caller() }}
    {% endif %}
    {% endif %}
    {% endmacro %} {# The top of a profile page: a cover strip, the avatar, name, a line under it and pill tabs. With no cover_url the cover is a plain grey band. #} {% macro profile_header(name, subtitle=None, avatar_url=None, cover_url=None, tabs=None) %}
    {% if cover_url %}{% endif %}
    {{ avatar(name, avatar_url, 'h-20 w-20', 'text-xl') }}

    {{ name }}

    {% if subtitle %}

    {{ subtitle }}

    {% endif %}
    {% if tabs %}
    {{ pill_tabs(tabs, muted=true) }}
    {% endif %}
    {% endmacro %} {# One plan. features: list of strings or {text, included}. highlighted=true draws the dark card. #} {% macro pricing_card(name, price, period=None, description=None, features=[], button_label='Get started', href='#', highlighted=false, eyebrow=None) %} {% set dark = highlighted %}

    {{ __(name) }}

    {% if eyebrow %}{{ __(eyebrow) }}{% endif %}

    {{ price }}{% if period %}/ {{ __(period) }}{% endif %}

    {% if description %}

    {{ __(description) }}

    {% endif %}
      {% for f in features %} {% set text = f.text if f.text is defined else f %}{% set on = f.included if f.included is defined else true %}
    • {{ icon('check' if on else 'minus', 'h-4 w-4') }}{{ __(text) }}
    • {% endfor %}
    {{ __(button_label) }}
    {% endmacro %} {# Numbered "how it works" steps. items: list of {title, text, value, icon}. #} {% macro steps(items) %}
      {% for it in items %}
    1. {{ loop.index }} {% if it.icon %}{{ icon(it.icon, 'h-4 w-4') }}{% endif %} {{ __(it.title) }} {% if it.text %}{{ __(it.text) }}{% endif %} {% if it.value %}{{ it.value }}{% endif %}
    2. {% endfor %}
    {% endmacro %} {# A read-only value with a Copy button (referral codes, API keys, links). #} {% macro copy_field(value, label=None, hint=None, id=None) %}
    {% if label %}{% endif %}
    {% if hint %}

    {{ __(hint) }}

    {% endif %}
    {% endmacro %} {# One chat message. mine=true puts it on the right in a dark bubble. #} {% macro chat_bubble(text, time=None, mine=false, name=None, avatar_url=None) %}
    {% if not mine and name %}{{ avatar(name, avatar_url, 'h-8 w-8', 'text-xs') }}{% endif %}
    {{ text }}
    {% if time %}

    {{ time }}

    {% endif %}
    {% endmacro %} {# Stars for a score: ui.rating(4.5, count=128) #} {% macro rating(value, max=5, count=None) %} {% set v = [[value or 0, 0] | max, max] | min %} {% for i in range(max) %} {% if v >= i + 1 %}{{ icon('star', 'h-4 w-4 fill-current') }} {% elif v >= i + 0.5 %}{{ icon('star', 'h-4 w-4 text-gray-300 dark:text-white/20') }}{{ icon('star', 'h-4 w-4 fill-current') }} {% else %}{{ icon('star', 'h-4 w-4 text-gray-300 dark:text-white/20') }}{% endif %} {% endfor %} {{ v }} {% if count is not none %}({{ count }}){% endif %} {% endmacro %} {# Order tracking steps across the card (down the card on a phone). items: list of {label, time}. current: index of the step in progress; earlier ones are done. #} {% macro progress_steps(items, current=0) %}
      {% for it in items %} {% set done = loop.index0 < current %}{% set now = loop.index0 == current %}
    1. {% if not loop.last %}{% endif %} {% if done %}{{ icon('check', 'h-4 w-4') }}{% else %}{{ loop.index }}{% endif %} {{ __(it.label) }} {% if it.time %}{{ it.time }}{% endif %}
    2. {% endfor %}
    {% endmacro %}