Custom
The CustomElement
class allows you to render a custom .jsx
snippet. The .jsx
file should be placed in public/elements/ELEMENT_NAME.jsx
.
Attributes
The name of the custom Element. It should match the name of your JSX file (without the .jsx
extension).
The props to pass to the JSX.
Determines how the text element should be displayed in the UI. Choices are “side”, “inline”, or “page”.
How to Write the JSX file
.jsx
for you!To implement the jsx
file for your Chainlit custom element, follow these instructions.
Component definition
Only write JSX code, no TSX. Each .jsx
file should export default one component like:
The component props
are globally injected (not as a function argument). NEVER pass them as function argument.
Use Tailwind for Styling
Under the hood, the code will be rendered in a shadcn + tailwind environment. The theme is relying on CSS variables.
Here is an example rendering a div
with a primary color background and round border:
Only Use Allowed Imports
Only use available packages for imports. Here is the full list:
react
sonner
zod
recoil
react-hook-form
lucide-react
@/components/ui/accordion
@/components/ui/aspect-ratio
@/components/ui/avatar
@/components/ui/badge
@/components/ui/button
@/components/ui/card
@/components/ui/carousel
@/components/ui/checkbox
@/components/ui/command
@/components/ui/dialog
@/components/ui/dropdown-menu
@/components/ui/form
@/components/ui/hover-card
@/components/ui/input
@/components/ui/label
@/components/ui/pagination
@/components/ui/popover
@/components/ui/progress
@/components/ui/scroll-area
@/components/ui/separator
@/components/ui/select
@/components/ui/sheet
@/components/ui/skeleton
@/components/ui/switch
@/components/ui/table
@/components/ui/textarea
@/components/ui/tooltip
@/components/ui
imports are from Shadcn.Available APIs
Chainlit exposes the following APIs globally to make the custom element interactive.
Example of a Counter Element
Full Example
Let’s build a custom element to render the status of a Linear ticket.
First, we write a small Chainlit application faking fetching data from linear:
Second we implement the custom element we reference in the Python code:
Finally, we start the application with chainlit run app.py
and send a first message in the UI.
The LinearTicket custom element rendered.
Advanced
Update Props from Python
To update a custom element props from the python code, you can store the element instance in the user session and call .update()
on it.
Call a Function from Python
If you need to call a function directly from the python code, you can use cl.CopilotFunction
.
Was this page helpful?