Introducing a new way to review your PRs.

Read the announcement

petalframework / petal_components

Phoenix + Live View HEEX Components

View on GitHub

24 documents · Updated Sep 2, 2026

docs / content-and-identity-primitives

Content and identity primitives

Petal Components provides shared primitives for expressing meaning and identity in Phoenix LiveView screens. Buttons and links guide action or movement. Typography establishes reading order. Badges, avatars, and icons add status and identity. Chat and local-time components adapt content to live conversation and to the person reading it. These primitives fill the page structures described in Layout, navigation, and content, while larger route and frame patterns are covered in Structural layout and navigation.

Action and navigation primitives

Action primitives give each control a clear role. Use a button when the user changes state or starts an event. Use a link when the user moves to another place. The shared components keep those choices consistent across page content and navigation.

Buttons

The button primitive presents an action with a shared visual language for size, color, emphasis, loading, disabled state, and optional icons. It can also carry a destination, which lets an application keep the same action surface while choosing link navigation or an event control. Icon-only buttons need an accessible label because the icon does not state the action by itself. (lib/petal_components/button.ex#L8-L79)

Buttons can work as a fused group when several related controls form one rail. A button group can contain buttons, inputs, text segments, and separators. This supports patterns such as view switches, split actions, and an address field with a visit action without making each child invent its own joining rules. (lib/petal_components/button_group.ex#L6-L45)

References: lib/…/button.ex• L8-79 lib/…/button.ex• L95-167 lib/…/button_group.ex• L6-45

The link primitive expresses both browser movement and LiveView movement through one interface. It supports a normal route, a patch that changes the current view, a LiveView navigation, or a button-like control when the interaction is not navigation. A disabled link becomes an inert button because an anchor has no native disabled state. (lib/petal_components/link.ex#L4-L61)

This shared contract lets buttons and navigation patterns use the same destination model. It also keeps disabled behavior honest: a control that looks disabled must not still open its destination. For how these links become breadcrumbs, tabs, menus, and step journeys, see Structural layout and navigation.

References: lib/…/link.ex• L4-61 lib/…/button.ex• L41-79

Text and status primitives

Text and status primitives make content easy to scan. Typography gives content a reading order and a set of supporting tones. Badges compress a short state or label into a small, meaningful marker.

Typography

The typography primitives provide semantic headings from h1 through h5, paragraphs, lists, prose, lead text, muted text, large and small text, quotations, inline code, and separators. Their purpose is to keep hierarchy stable while the page supplies the words. Heading levels should follow the page structure rather than the visual size a screen happens to need. (lib/petal_components/typography.ex#L4-L18, lib/petal_components/typography.ex#L20-L131, lib/petal_components/typography.ex#L142-L258)

These text styles work inside cards, dialogs, messages, and page sections. A lead style introduces a section. Muted or small text carries supporting detail. Prose and list styles support longer content without requiring a separate visual system. (lib/petal_components/typography.ex#L126-L185, lib/petal_components/typography.ex#L198-L258)

References: lib/…/typography.ex• L4-18 lib/…/typography.ex• L20-131 lib/…/typography.ex• L142-258

Badges

The badge primitive marks a short state, category, or note without taking the place of the main content. It combines a semantic color with a light, dark, soft, or outline treatment. A status dot can reinforce the label, but the label must still carry the meaning because the dot is decorative. (lib/petal_components/badge.ex#L4-L21, lib/petal_components/badge.ex#L39-L62)

Badges support note and status roles. Use a role that matches how the surrounding content should be announced. Keep one semantic meaning in the badge and its dot; two conflicting colors make the state hard to read. (lib/petal_components/badge.ex#L12-L20, lib/petal_components/badge.ex#L23-L37)

References: lib/…/badge.ex• L4-21 lib/…/badge.ex• L23-62

Identity and icon primitives

Identity primitives answer “who or what is this?” in a compact form. Avatars represent people and groups. Icons represent actions or concepts. Brand icons represent external providers and must follow a different accessibility and usage rule from general-purpose icons.

Avatars

The avatar primitive represents a person, team, or workspace with a photo, initials, a placeholder, or generated art. It can also show presence such as online, busy, away, or offline. Circle is the normal shape for a person. Rounded shape suits an organization, team, or workspace. (lib/petal_components/avatar.ex#L6-L47)

When a photo is not available, a name can produce stable initials or a stable visual treatment. An avatar group shows several identities and can replace hidden members with an overflow count. The accessible label must still identify the represented person or group when the image or art does not provide that information. (lib/petal_components/avatar.ex#L66-L145, lib/petal_components/avatar.ex#L148-L193)

References: lib/…/avatar.ex• L6-47 lib/…/avatar.ex• L51-145 lib/…/avatar.ex• L148-193

Icons

The icon primitive supplies a shared Heroicon vocabulary for actions, states, and small visual cues. An icon is most useful when it supports nearby text or provides a familiar cue inside a labeled control. The component accepts named outline, solid, mini, and micro forms from the Heroicons set. (lib/petal_components/icon.ex#L44-L107)

Decorative icons should remain hidden from assistive technology. When an icon stands alone, give its control or image an accessible name instead of asking the icon shape to carry meaning. This keeps icon-only buttons, chat actions, and status cues usable without sight. (lib/petal_components/icon.ex#L24-L40, lib/petal_components/icon.ex#L84-L107)

References: lib/…/icon.ex• L1-40 lib/…/icon.ex• L44-107

Brand icons

The brand icon primitive displays a known provider mark such as Google, GitHub, Apple, or Microsoft. It is intended for provider identity rather than general interface decoration. Marks are monochrome by default and can use the provider’s colors when the surrounding design calls for them. (lib/petal_components/brand_icon.ex#L1-L19, lib/petal_components/brand_icon.ex#L46-L110)

Brand marks are decorative when another label already names the provider. When a mark stands alone, give it an image role and an accessible label. The consuming application remains responsible for each provider’s logo rules. (lib/petal_components/brand_icon.ex#L17-L19, lib/petal_components/brand_icon.ex#L55-L75)

References: lib/…/brand_icon.ex• L1-19 lib/…/brand_icon.ex• L46-75 lib/…/brand_icon.ex• L79-110

Provider and conversation content

Provider and conversation components combine identity with a task. Social buttons turn a provider mark into a sign-in action. Chat components turn text, actions, status, and identity into a conversation that can update while a response is in progress.

Social buttons

The social button primitive provides a consistent provider sign-in action. It joins a brand icon to the same size, focus, and shape language as other buttons. The outline form keeps the surface neutral and shows the provider mark. The solid form uses the provider’s brand treatment. (lib/petal_components/social_button.ex#L1-L21, lib/petal_components/social_button.ex#L41-L87)

A social button becomes a link when it has a destination such as an OAuth route. Without a destination it remains a button for an in-page event. Its default text names the provider and its icon-only form keeps that text as the accessible name. (lib/petal_components/social_button.ex#L50-L65, lib/petal_components/social_button.ex#L67-L103)

References: lib/…/social_button.ex• L1-21 lib/…/social_button.ex• L41-120

Chat

The chat family provides a LiveView-native model for a conversation. A conversation holds a scrollable thread and an optional composer. Messages can identify a user, assistant, or system role and can carry avatars, actions, attachments, and generated content. The family also covers source-grounded answers, tool work, structured questions, reasoning blocks, status markers, suggestions, copy actions, and error recovery. (lib/petal_components/chat.ex#L1-L26, lib/petal_components/chat.ex#L52-L186, lib/petal_components/chat.ex#L301-L384, lib/petal_components/chat.ex#L826-L1141, lib/petal_components/chat.ex#L1143-L1315, lib/petal_components/chat.ex#L1596-L1803, lib/petal_components/chat.ex#L1833-L1944)

These chat features connect server-owned conversation state to visible reader context:

  • Streaming text adds generated output as it arrives and can render markdown as a response grows.
  • Tool-call cards show pending, running, complete, and failed work. A completed call can expose its payload or a rendered result, while an error can offer a retry action.
  • Markdown citations connect inline source markers to a source list. The source list groups repeated URLs and only turns trusted web schemes into links.
  • The composer supports prompt entry, stop and edit states, suggestions, and ordinary LiveView file uploads. Questionnaires keep a structured question and its resolved answer in the transcript.

The conversation keeps the reader’s place as history is added above and follows the latest message only when the reader is already at the live edge. Streaming output then updates the active message without replacing text that has already arrived. This separates server events from reader position and makes long or live replies usable. (lib/petal_components/chat.ex#L52-L112, assets/…/petal_components.js• L10-49, assets/…/petal_components.js• L178-253)

Chat markdown is rendered as content rather than trusted live markup. Rich text can also map named widget blocks to application components, which lets a response include an interactive result while the application controls what names are allowed. The chat family is explicit rather than part of the normal component import because its generic names may conflict with application helpers. (lib/petal_components/chat.ex#L14-L26, lib/petal_components/chat.ex#L214-L243, lib/petal_components/chat.ex#L696-L807)

References: lib/…/chat.ex• L1-49 lib/…/chat.ex• L52-186 lib/…/chat.ex• L214-243 lib/…/chat.ex• L301-384 lib/…/chat.ex• L696-807 lib/…/chat.ex• L826-1141 lib/…/chat.ex• L1143-1315 lib/…/chat.ex• L1596-1803 lib/…/chat.ex• L1833-2076 assets/…/petal_components.js• L10-49 assets/…/petal_components.js• L178-253

Reader-local time

The local-time primitive keeps one timestamp meaningful across readers in different places. The server supplies a semantic time value with its UTC instant. The browser then formats that instant with the reader’s locale and time zone. (lib/petal_components/local_time.ex#L1-L10, lib/petal_components/local_time.ex#L66-L105)

The component supports absolute date and time forms, custom date-format options, and relative phrases such as “yesterday” or “in 3 weeks.” Relative values update over time and change to an absolute form after a threshold. Before browser formatting runs, the UTC value remains visible as a sortable fallback. (lib/petal_components/local_time.ex#L12-L31, assets/…/petal_components.js• L2244-2355)

This behavior makes the timestamp useful in server-rendered output, reader modes, and views where JavaScript does not run. A DateTime, a UTC-assumed NaiveDateTime, or an ISO 8601 string can supply the instant. (lib/petal_components/local_time.ex#L25-L31, lib/petal_components/local_time.ex#L35-L52)

References: lib/…/local_time.ex• L1-31 lib/…/local_time.ex• L35-105 assets/…/petal_components.js• L2244-2355

Primitive assembly

These primitives work best when each one adds one kind of meaning to a larger content flow. A page can establish text hierarchy, identify the subject, show its state, and then offer an action or a live response without mixing those responsibilities.

The following diagram shows how a content surface can combine identity, status, action, conversation, and time:

flowchart LR
    A[Text hierarchy] --> B[Identity cue]
    B --> C[Status badge]
    C --> D[Action or provider button]
    D --> E[Conversation or result]
    E --> F[Reader-local time]

Use the primitives in relation to the content around them:

  • Pair a visible label with an icon when the action is important.
  • Use an avatar for identity and a badge for a short state. Do not make color alone carry either meaning.
  • Use a brand icon for a provider and a social button for the provider action.
  • Use chat roles and message actions to show who spoke and what the reader can do next.
  • Use local time when the instant should adapt to the reader rather than to the server’s time zone.

References: lib/petal_components.ex• L1-81 lib/…/button.ex• L69-91 lib/…/avatar.ex• L51-64 lib/…/badge.ex• L44-62 lib/…/social_button.ex• L67-103 lib/…/chat.ex• L82-186 lib/…/local_time.ex• L66-93

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the element role and disabled behavior aligned between Link, Button, social buttons, and any navigation component that reuses them.
  • Keep icon-only controls paired with an accessible name when changing button, chat action, avatar, or brand-icon markup.
  • Keep badge labels self-sufficient when changing dot colors or status treatments.
  • Keep provider names, supported brand marks, and social-button defaults aligned when adding a sign-in provider.
  • Keep chat server events, browser hooks, scroll behavior, and streamed-content fallbacks aligned when changing conversation or composer behavior.
  • Keep tool-call lifecycle states aligned with their visible status, payload panels, result slots, and retry actions.
  • Keep attachment upload events aligned with the host LiveView upload config, cancel event, and upload consumption flow.
  • Keep questionnaire field types, names, resolution state, and server event payloads aligned when changing structured questions.
  • Keep citation markers, source identity, deduplication, and allowed URL schemes aligned when changing grounded chat answers.
  • Keep the UTC datetime value and browser format options aligned when changing local-time output or its client hook.

Important failure modes

TriggerConsequenceGuard
A disabled link is left as an active anchorThe control looks disabled but still navigatesUse the shared link behavior so the disabled case becomes an inert button.
An icon-only action has no accessible nameA person using assistive technology cannot identify the actionPut the name on the control or pass the required label to the primitive.
A badge dot carries the only state meaningThe state is lost when color or the dot is not perceivedWrite the state in the badge label.
A photo or generated avatar has no useful nameThe identity cue has no meaning outside the visual imageSupply an alt or name that identifies the represented person or group.
Chat hooks are not available for a streaming or composer flowTokens do not appear correctly or input behavior does not workRegister the bundled component hooks when using interactive chat features.
A stream sends markdown HTML to the text mode or token text to the markdown modeThe response displays the wrong content form while it streamsKeep the server push payload and the streaming_text format aligned.
A chat upload form has no change eventLiveView cannot report upload progress correctlyPass phx-change when the composer uses an upload config.
A required questionnaire multi-select is trusted to browser validationThe form can submit with no selected optionValidate required multi-select answers in the parent application.
A local-time element loses its valid UTC datetime valueThe browser cannot format the instant and the display can fall back to the wrong contentPreserve the semantic ISO timestamp and its matching client behavior.
Untrusted chat text is inserted as live markupModel output can change the page instead of remaining contentUse the chat markdown path so content is sanitized before display.

Key files

FileWhy to read it
lib/petal_components.exShows which content and identity primitives are available through the core component import and why chat is namespaced.
lib/petal_components/button.exDefines the shared action surface and its navigation, loading, disabled, and icon states.
lib/petal_components/link.exDefines the common contract for browser links, LiveView navigation, patches, and button-like actions.
lib/petal_components/typography.exDefines semantic text hierarchy and supporting content styles.
lib/petal_components/badge.exDefines compact state and note markers.
lib/petal_components/avatar.exDefines person, team, workspace, presence, and grouped identity cues.
lib/petal_components/icon.exDefines the shared general-purpose icon vocabulary.
lib/petal_components/brand_icon.exDefines provider marks and their accessible use.
lib/petal_components/social_button.exConnects provider identity to sign-in and provider actions.
lib/petal_components/chat.exDefines conversation structure, live output, grounded answers, tool work, structured input, attachments, message actions, and content safety boundaries.
assets/…/petal_components.jsDefines the browser behavior that supports streamed chat, conversation scrolling, and local-time formatting.
lib/petal_components/local_time.exDefines the semantic timestamp contract and reader-local display choices.