Component runtime and assets
Petal Components gives Phoenix applications a shared set of server-rendered HEEx components with a small browser layer for behavior that cannot come from markup alone. The library joins three public surfaces: Elixir component entry points, an optional LiveView hook bundle, and CSS assets that supply the shared visual language. The consuming application remains responsible for loading the CSS, registering hooks, and supplying external browser engines when a component needs one.
Runtime model
Server-rendered components
The Elixir side is the main component surface. Applications render components in HEEx, while Phoenix and LiveView own the page lifecycle and send changed markup to the browser. The same component set supports live and dead views, so the server-rendered surface does not require a separate client framework. PetalComponents is the public import boundary for the core set.
References: README.md• L26-32 lib/petal_components.ex• L1-4
Client enhancement boundary
The browser layer adds behavior for components that need local DOM state, timing, focus, scrolling, animation, or a browser API. LiveView still owns the rendered component boundary; a registered hook enhances the element after mount and responds to later updates. This keeps application events and state on the LiveView side while the browser handles short-lived interaction state.
The main path is:
flowchart LR
A[Phoenix application] --> B[HEEx component call]
B --> C[LiveView-rendered markup]
C --> D{Browser behavior needed?}
D -- No --> E[CSS and LiveView.JS behavior]
D -- Yes --> F[Registered Petal hook]
F --> G[Local DOM behavior]
G --> H[LiveView events and patches]
References: README.md• L67-97 assets/…/petal_components.js• L1-8
Public component surface
Core component imports
use PetalComponents makes the core components available as unqualified function components such as <.button>, <.modal>, and <.table>. This is the normal entry point for a Phoenix web module because it gives the application one stable import instead of requiring each component module to be named in every view. The set covers layout, content, forms, actions, feedback, data display, visual effects, and confirmation dialogs.
The import also aliases PetalComponents.HeroiconsV1, which supports the library's icon-facing component API. The import list is the contract to review when a new core component becomes part of the normal application surface.
References: lib/petal_components.ex• L19-102 README.md• L111-125
Namespaced chat entry points
The PetalComponents.Chat family is a separate public entry point. It is not included in the core import because generic names such as markdown can conflict with application helpers. Applications opt in with an alias and call the chat components through that namespace, which makes the boundary clear and avoids accidental name collisions.
References: lib/petal_components.ex• L6-17 guides/streaming_chat.md• L22-31
Shared component contracts
Shared helper behavior
PetalComponents.Helpers holds small rules that must stay consistent across more than one component. Its important shared contracts are ordered composition of Phoenix.LiveView.JS operations and generation of unique, readable HTML IDs from text or iodata. These rules let components combine application actions with their own actions and create stable relationships between labels, controls, and targets without exposing a separate runtime to the application.
References: lib/…/helpers.ex• L1-16 lib/…/helpers.ex• L18-74
LiveView state ownership
Components use regular HTML attributes, slots, IDs, and LiveView events as their integration language. The application owns durable state and business events; the component owns only the presentation contract and any short-lived browser state needed to make that contract usable. This split is especially important for overlays, forms, charts, and streamed content because LiveView patches can replace or update the surrounding markup.
References: README.md• L26-32 assets/…/petal_components.js• L10-49 lib/…/chart.ex• L114-137
Browser hook bundle
Hook registration
The package ships assets/…/petal_components.js as an importable ES module. Its default export is a map of hook names, and the application merges that map into the hooks option when it creates its LiveSocket. The application can merge its own hooks at the same boundary. Registration is one application-level step rather than a script tag or inline script per component instance.
References: README.md• L84-97 assets/…/petal_components.js• L9893-9936
Hook responsibilities
The bundle covers browser work that complements server-rendered markup. Its hooks support the following broad groups:
| Group | Purpose |
|---|---|
| Live updates | Stream content, refresh charts, and keep data-table or conversation views usable as LiveView data changes. |
| Input behavior | Add password reveal, copy, clear, range display, dual-range, one-time-password, and chat attachment behavior. Clipboard files can enter a LiveView upload through the chat composer. |
| Navigation and disclosure | Coordinate popovers, dropdowns, command dialogs, combo boxes, navigation menus, and carousel behavior. |
| Feedback and effects | Run local timing, motion, number transitions, confetti, spotlight, word rotation, and typing effects. |
| Display integration | Apply local-time rendering and connect charts to a browser chart engine. |
The hooks listen to data attributes, DOM events, and LiveView pushed events defined by their component contracts. They also clean up observers, timers, and event listeners when LiveView removes an element. The detailed event and DOM contracts belong in Browser hooks.
References: assets/…/petal_components.js• L15-149 assets/…/petal_components.js• L121-139 assets/…/petal_components.js• L255-359 assets/…/petal_components.js• L1472-1480 assets/…/petal_components.js• L9893-9936
CSS asset themes
Default theme
assets/default.css is the primary style asset. It provides the component classes and a default Tailwind v4 theme so a fresh application has a complete visual baseline. The theme uses named color ramps such as primary, secondary, semantic status colors, and a gray role. The defaults are soft fallbacks, so an application's own theme can take precedence while the component styles continue to use the same token names.
References: assets/default.css• L1-16 README.md• L67-73
Gray theme override
assets/tailwind-gray.css is an optional theme choice, not a second component style sheet. The default asset maps the gray role to zinc values; importing this asset after the default asset replaces that role with Tailwind's stock gray ramp. This boundary lets an application choose its neutral palette without changing component markup or class contracts.
References: assets/default.css• L10-14 assets/tailwind-gray.css• L1-26
Visual asset integration
The CSS assets expect the consuming application's Tailwind pipeline to scan the package's component source and import the chosen theme asset. Components expose pc- classes and CSS variables as the visual contract, while application utilities and token overrides can adjust the result. SVG icons follow a separate dependency boundary: the library can use Heroicons when the consuming build supplies that asset set, and it warns when the icons are not available.
The visual asset details and icon choices belong in Styling and icon assets.
References: README.md• L67-73 lib/…/icon.ex• L6-21 lib/…/icon.ex• L44-106
Phoenix integration boundaries
Application setup
Integration has three application-owned steps: add the Hex dependency, import the CSS asset into the Tailwind entry point, and use PetalComponents in the web module. The application then registers the hook map in its LiveSocket when it uses components that need browser behavior. This setup links server components, generated CSS, and the LiveView connection without moving application state into the package.
References: README.md• L34-97 mix.exs• L82-95
External browser engines
Some features use a browser capability that the package does not bundle. The chart component is the clearest example: the application supplies Apache ECharts and exposes it as window.echarts, while the Petal hook connects the server-provided chart description to that engine. Without the engine, the component can render its server-side shell but cannot create the chart.
References: lib/…/chart.ex• L1-29 assets/…/petal_components.js• L672-679
Color-scheme contract
Color-scheme switching crosses the layout, CSS, and hook boundaries. The root layout renders the no-flash scheme script once in the document head; that script applies the initial light or dark class and exposes window.PetalColorScheme. The switch component then uses the PetalColorScheme hook to change and reflect the shared preference. CSS tokens and browser features such as charts can react to the same resolved scheme.
References: lib/…/color_scheme_switch.ex• L17-37 lib/…/color_scheme_switch.ex• L44-95 lib/…/color_scheme_switch.ex• L119-127 assets/…/petal_components.js• L688-715 assets/…/petal_components.js• L1079-1143
Child pages
- Browser hooks explains the client-side hook bundle and its interaction, focus, timing, and DOM state contracts.
- Styling and icon assets explains theme tokens, CSS expectations, SVG icons, and visual asset setup.
Change checklist
When these files or sections are changed, remember to consider these:
- When the core import list changes, check whether the component is intended to be unqualified or must remain namespaced to avoid helper name collisions.
- When a hook name or its markup markers change, update the component's
phx-hookcontract and the registration guidance together. - When chat upload support changes, check the composer file-input boundary and the LiveView upload event path because pasted files depend on both.
- When a CSS token name or theme role changes, check chart color resolution and the alternate gray theme because both consume the same token boundary.
- When an external browser engine becomes required or its global entry point changes, update the application integration boundary and the component's missing-engine behavior.
- When color-scheme storage or events change, check the layout script, all switch variants, and consumers that listen for
petal:scheme-changed.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
The app omits hooks: { ...PetalComponents } from its LiveSocket. | Components that depend on browser hooks do not gain their local behavior. | Register the package hook map and merge it with application hooks. |
| The app imports component classes without assets/default.css or an equivalent theme. | The markup renders without the library's expected visual rules and token defaults. | Import the default asset through the Tailwind entry point. |
| The app imports the default CSS and then expects stock Tailwind gray without the override asset. | The gray utility and component neutral colors use the package's zinc-based ramp. | Import tailwind-gray.css after default.css when stock gray is required. |
| A pasted file has no scoped chat upload input or the input event is not delivered to LiveView. | The composer keeps the file in the clipboard path and LiveView does not receive an upload entry. | Keep the upload input inside the composer attachment control and preserve its input event path. |
A chart is mounted without Apache ECharts at window.echarts. | The chart hook warns and does not initialize the chart engine. | Supply ECharts before the LiveSocket mounts the chart. |
A color-scheme switch is used without color_scheme_script in the root head. | The hook cannot find the shared scheme contract, so switching and no-flash initialization do not work. | Render the script once before the application JavaScript. |
| Heroicons are not available to the build when an icon is requested. | Icon rendering cannot use the generated Heroicons implementation. | Add the supported Heroicons dependency or provide the app's compatible icon setup. |
Key files
| File | Why to read it |
|---|---|
lib/petal_components.ex | Defines the main Elixir import boundary and the explicit chat namespace decision. |
lib/petal_components/helpers.ex | Defines shared ID and LiveView.JS composition contracts used across components. |
| assets/…/petal_components.js | Defines the browser hook map and the client-side integration surface. |
| assets/default.css | Defines the component style layer and default design-token fallbacks. |
| assets/tailwind-gray.css | Defines the optional neutral-palette override. |
lib/petal_components/color_scheme_switch.ex | Defines the page-level color-scheme contract between layout script and component. |
lib/petal_components/icon.ex | Defines the Heroicons asset boundary for icon rendering. |
lib/petal_components/chart.ex | Defines the server-to-browser boundary for an externally supplied chart engine. |
lib/petal_components/chat.ex | Defines the chat composer and its upload boundary for pasted and selected files. |
| README.md | Shows the expected Phoenix installation and the three main application integration steps. |