Add a new site widget to your CLI project with the following command:
The CLI generates this directory structure in your project repo:
In the your site widget folder, three files are created:
element.json
file that defines the site configuration settings of your site widget.element.tsx
file that contains the main code for the custom element that the site widget is built on.panel.tsx
file that contains the code for your widget's settings panel.It is possible to create each of these files yourself, but we don't recommend it for a couple of reasons:
The element.json
file configures the settings for how your site widget appears a user's site. This file is required, so don't delete it after the site widget is generated. If you add your own files, you must include element.json
.
When you generate a new site widget in your project, you'll see the following code in element.json
:
You can edit the JSON and add keys as follows:
Key | Type | Description |
---|---|---|
id | string | A unique identifier for your site widget. The CLI generates this GUID for you. If you add the JSON file yourself, you must generate your own GUID. |
name | string | The name of the site widget as it appears in the app dashboard. The CLI prompts you for the name when you create the site widget. |
height.defaultHeight | number | The widget's height in pixels when it is first installed on a site. |
width.defaultWidth | number | The widget's width in pixels when it is first installed on a site. |
width.stretchByDefault | boolean | Whether to stretch the widget to full width on installation. |
width.allowStretch | boolean | Whether to allow users to toggle the widget between full-width and default width. |
installation.autoAddToSite | boolean | Whether to automatically add the widget to the site home page upon installation. Users can later move these widgets around on their site. |
installation.essential | boolean | Whether to mark the widget as a core part of your app's functionality. You can set this key to true only if the widget is added as part of a site page extension. If set to true , deleting this widget (or the section or page that contain it) from a site will also delete the app. |
The element.tsx
file is where you write the code for the custom element that defines the site widget. You can write code in other files and include it here, but you must return your main component in this file. This is where the CLI will look for the site widget definition.
This file is required for the site widget to work, so don't delete it. If you add the files on your own, you must include element.tsx
.
When the element.tsx
file is generated, it looks like this:
The file sets up a React component called CustomElement
, where you write the custom element code. It also calls reactToWebComponent
to convert your React component to a custom element, and exports the custom element so Wix can work with it as a site widget.
We recommend writing your code in React, since the rest of the CLI also works with React. However, you can also write code directly in element.tsx
with Javascript. If you do so, make sure to export the custom element, like in the example below:
Note that you don't need to call define()
even here; Wix still takes care of that for you even if you haven't defined the custom element in React.
The panel.tsx
file contains the code defining your site widget's settings panel. The settings panel lets site users customize the widget after they install your app. You're not required to include the panel.tsx
file. However, without this file, your site widget won't have a settings panel.
In the panel’s code, use Wix's JavaScript SDK to access widget properties and retrieve environmental data from the editor, as well as access and manage Wix business solutions.
To apply changes made in the settings panel to the widget, use the Widget API’s setProp()
function. Widget properties are bound to your custom element’s attributes, so any change in the properties automatically updates the corresponding attribute. To handle attribute updates so they are reflected in your widget in the editor, use the attributeChangedCallback()
in your custom element's code.
After the site widget is generated, you'll see code like this in panel.tsx
file:
The file contains two important pieces of code:
SettingsPanel
where you write your code defining the site widget's settings panel.withProviders()
that wraps the component with the necessary React providers. This includes the <WixDesignSystemProvider>
and <WixProvider>
.As with element.tsx
, you can write code in other files and include it here, but you must return your main component in this file. The panel code must be written in React to work with the rest of the CLI.