Adding a custom widget
Available since: brezel-spa@2.0.0
Custom widgets are a powerful way to extend the functionality of your Brezel instance. They can be used to display custom data, interact with external services, or provide additional functionality to your users.
Creating a custom widget
There are two ways to create a custom widget, and you need to choose the one that best fits your use case:
- Integrated Vue component: If you want to create a custom widget that is tightly integrated with your SPA. More performant on initial load and more fun to develop (hot module replacement etc.). This is the recommended way.
- Editable Vue widget: If you want to create a custom widget that can be edited in the interface by users such as admins. This has a more restrictive API and is less performant on initial load.
Integrated Vue component
To create an integrated Vue component, you need to create a new Vue component in your SPA repository, in your src/components
directory.
- Create your component in
src/components/MyWidget.vue
:
- Register the component in your
src/main.ts
file:
- Use your widget in your layout:
That’s it! Your custom widget is now available in your layout.
Import Generated Types
You can leverage Brezel’s generated types to get type safety and autocompletion in your custom widget.
To do this, you need to enable the type generation in your systems/<system>/system.json
file:
Then you need to tell TypeScript where to find the types:
Editable Vue widget
To create an editable Vue widget, you need to create a new Vue component in your system directory, preferably under
systems/<system>/widgets
.
- Create your component in
systems/<system>/views/widgets/MyWidget.vue
:
- Register the widget in your
systems/<system>/widgets.bake.json
file:
- Use your widget in your layout:
That’s it! Your custom widget is now available in your layout.
Differences between integrated and editable widgets
- | Integrated widget | Editable widget |
---|---|---|
Importing Module | import { Module } from '@kibro/brezel-spa' | import Module from 'module' |
Importing Api | import { Api } from '@kibro/brezel-spa' | import Api from 'api' |
Using components | components: { MyComponent } | Available components are predefined |
Recommended: Converting editable widgets to integrated widgets
If you want to convert an editable widget to an integrated widget, you can follow these steps:
-
Move the widget to the
src/components
directory. -
Update the naming of the widget to match the Vue component naming conventions:
- Rename the file to PascalCase.
- Rename the component to PascalCase.
- Update the component registration in
src/main.ts
to use PascalCase.
-
Update the imports to use the
@kibro/brezel-spa
package. -
Declare the prop
entity
and any component you want to use in the widget explicitly. -
Update the widget registration in the layout to use the
component
property instead of theview
property. -
Update the widget registration in the
systems/<system>/widgets.bake.json
file (or any other file where the widget is registered) to remove the widget registration.
Updating entity fields
Vue prohibits directly mutating props, so you need to emit an event to update the entity fields.
You must emit the event “event” with an object that contains the _type
property set to “update-entity”, the field you want to update, and the new value.