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.
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.