Components
Caspian components are just Python functions. They encapsulate logic, styles, and structure. Start simple with basic strings, or scale up to fully type-safe, reactive UI elements.
1. The Basics
A standard component handles prop merging and returns an HTML string. Use
merge_classes to ensure
Tailwind classes don't conflict.
Developer Speed Tip
Don't write boilerplate. If you have the VS Code Extension installed, just type:
caspcom
+
Tab
It automatically generates the standard structure seen below.
2. Type-Safe Props
Want TypeScript-like validation? Use Python's
Literal
type. Your editor will autocomplete variants, and linter will catch typos.
3. HTML Templates & Reactivity
For complex layouts or reactive state, separate your code into an HTML
file. Use render_html to bridge
them.
from casp.component_decorator import component, render_html @component def Counter(**props): return render_html("Counter.html", **props)
<div> <h3>[[label]]</h3> <!-- Prop --> <button onclick="inc()"> {count} <!-- State --> </button> </div>
Passing Context Dictionaries
When you need precise control over variable names, or when passing state
handlers that don't map 1:1 to props, you can pass a dictionary explicitly
as the second argument to render_html.
4. Server-Side Logic (RPC)
Components can also host server-side logic using the
@rpc
decorator. This allows you to encapsulate business logic directly within
the component file.
Important Note
RPCs in components are Global. Unlike Pages (which are scoped to the URL), component RPCs are registered by their function name. Ensure unique names to avoid conflicts.
5. Component Composition
Much like React, PulsePoint allows you to easily compose custom components together, passing down props and state handlers.
Crucial Rule: Every component HTML template must return a single parent element. The props intended for reuse within the component should be mapped directly to this root element using the [[propName]] syntax.