Custom Components
Learn how to render custom components in Semaphor dashboard
Overview
When embedding analytics into your app, you'll often need to display information in industry-specific ways. For example:
- Trade analytics: A specialized map to visualize imports and exports between countries.
- Financial apps: A table with precise sorting and filtering options.
- HR tools: A distribution chart to show candidate demographics.
The sheer variety of visualizations means no single charting library can cover every need. Most BI tools lock you into one or two libraries, offering only limited customization.
This creates a tough choice:
- Settle for generic charts, leading to a subpar user experience.
- Build everything in-house, investing time and resources in analytics infrastructure instead of your core product.
As your analytics needs evolve, rigid BI tools make it increasingly difficult to adapt. That’s why flexibility is crucial when choosing an embedded analytics solution.
At Semaphor, we’ve designed a pluggable architecture that lets you bring your own charting library—or even fully custom components. This gives you complete control over the user experience, enabling you to integrate interactive, custom visuals with just a few lines of code.
Let's take a look at how it works.
Component Props Interface
Before diving into creating custom components, it's important to understand the props that Semaphor passes to your custom visuals. Every custom component receives a standardized set of props through the SingleInputVisualProps
interface:
Data Prop
The data
prop contains the actual data returned by your query. It's an array of records where each record is a key-value object:
- Type:
Record<string, string | number | boolean>[]
- Description: An array of objects representing rows of data from your query
- Example:
This structure allows your component to work with any query result, regardless of the column names or data types.
Settings Prop
The settings
prop is a key-value object that allows you to control the behavior and appearance of your component:
- Type:
Record<string, string | number | boolean>
- Description: Configuration options defined in your
components.config.ts
file - Example:
These settings are configurable by users in the Semaphor dashboard and provide a way to customize your component without changing the code.
Theme Prop
The theme
prop provides styling information to ensure your component matches the dashboard's appearance:
- Type:
CustomCardTheme
- Properties:
colors
: An array of color values for consistent visual stylingmode
: The current theme mode ('light'
,'dark'
, or'system'
)
Example:
Use these colors for charts, backgrounds, and other visual elements to maintain consistency with the overall dashboard theme.
Step 1: Create your custom component
The repository below includes a basic example of how to create a custom component for Semaphor.
This starts a local server at http://localhost:5173
. You can open up this repository in your favorite code editor and start exploring the code.
In semaphor-components
folder, you'll find the custom component that we'll add to the Semaphor dashboard. It's a simple component showing recent sales — about 50 lines of code. It demonstrates how to use all three props (data
, settings
, and theme
) that Semaphor provides to your custom components. You can make your components as sophisticated as you want.
Once you are satisfied with your custom component, you need to export it as a named export in the index.ts
file. You can export as many components as you want.
And lastly, you will need to specify how this component should appear in Semaphor console with components.config.ts
file.
Step 2: Publish your component to Semaphor
To publish your component to Semaphor, you will first need to install semaphor-cli
. You can do it with the following command:
Now let's initialize a new Semaphor Plugin
for your custom component.
You will be prompted to enter your Semaphor project ID and secret. You can get them from your Semaphor project page.
NOTE: Make sure to initialize the Plugin
and NOT the App.
Once the cli is initalized, you can build and publish your plugin to Semaphor with the following commands.
Step 3: Use your custom component in Semaphor Dashboard
You can now use your custom component in Semaphor dashboard just like any other visual from the visual selector and make it interactive with the rest of the dashboard.
Here is the sample dashboard with the custom component added. If you have any questions feel free to get in touch with us at support@semaphor.cloud