Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 00dfe6a

Browse files
committed
feat: add more documentation
1 parent 3fc35af commit 00dfe6a

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

README.md

+84-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,88 @@ This example contains documentation and example code for creating widgets using
2121

2222
**IMPORTANT:** Make sure you check the [widget development documentation](/widget-development.md) when developing your own.
2323

24+
## `widget.json`
25+
26+
There is a special file called `widget.json` in the root of the widget. Widget registries, [like this
27+
example](https://github.com/js-widgets/widget-registry-boilerplate), need this file to learn about
28+
this widget. It contains the following keys:
29+
30+
| Name | Required | Example | Description |
31+
| ------------------------ | -------- | -------------------------------------- | ---------------------------------------------------------------------------------------------- |
32+
| shortcode | yes | product-catalog | The machine name identifier for the widget. |
33+
| description | no | A catalog of products for our company. | A longer description for the widget. This is shown in the widget catalog. |
34+
| availableTranslations | yes | `['en', 'es']` | Language codes this widget is available in for internationalization purposes. |
35+
| settingsSchema | no | | A JSON Schema object decribing the input parameters for the widgets at embed time. |
36+
| externalPeerDependencies | no | | List of runtime dependencies for this widget that embedders need to add along with the widget. |
37+
| status | yes | stable | One of `stable`, `beta`, `wip`, or `deprecated`. |
38+
39+
### Configurable widgets
40+
Configuration parameters are specified during the embed process. Drupal will create a form element
41+
automatically to gather those parameters in the editorial screens. For the CMS to know what form
42+
element to use, the widget definition needs to include a [JSON Schema](https://json-schema.org)
43+
definition for every parameter.
44+
45+
This repository contains an example of a configurable parameter. In this case it's the text of the
46+
button. This is [described in `widget.json`](https://github.com/js-widgets/example-widget/blob/master/widget.json#L20-L36) as:
47+
48+
```json
49+
"settingsSchema": {
50+
"type": "object",
51+
"additionalProperties": false,
52+
"properties": {
53+
"fields": {
54+
"type": "object",
55+
"properties": {
56+
"button-text": {
57+
"type": "string",
58+
"title": "Button text",
59+
"description": "Some random string to be displayed when the widget is rendered.",
60+
"examples": ["I am a button", "Please, click me", "CLICK"]
61+
}
62+
}
63+
}
64+
}
65+
},
66+
```
67+
68+
And then accessed in the [widget code `Widget.jsx`](https://github.com/js-widgets/example-widget/blob/master/src/components/Widget.jsx#L25):
69+
70+
```jsx
71+
<p className="is-size-6 pb-4">
72+
<button className="button is-primary">{element.getAttribute('data-button-text')}</button>
73+
</p>
74+
```
75+
76+
Note that since the field name is `button-text`, the value is accessed as `'data-button-text'`.
77+
78+
### External dependencies
79+
80+
`externalPeerDependencies` is a tool designed to share JS dependencies across different widgets.
81+
This module provides an example how to avoid bundling `react`, `react-dom`, and `react-intl` with
82+
the widget's JS, while making Drupal (or any other available integrations) load the dependencies
83+
automatically.
84+
85+
For each dependency you will need to:
86+
87+
1. [Tell Webpack to not include the library](https://github.com/js-widgets/example-widget/blob/master/craco.config.js#L32-L35) in the resulting JS file(s) for this widget.
88+
```js
89+
// webpack.config.js or craco.config.js
90+
externals: {
91+
react: 'React',
92+
'react-dom': 'ReactDOM',
93+
'react-intl': 'ReactIntl',
94+
},
95+
// ...
96+
```
97+
1. [Tell the widget registry (in `widget.json`)](https://github.com/js-widgets/example-widget/blob/master/widget.json#L37-L46), and ultimately the CMS integrations where to find these libraries that were excluded.
98+
```json
99+
"externalPeerDependencies": {
100+
"react": {"src": "https://unpkg.com/react@^17/umd/react.production.min.js"},
101+
"react-dom": {"src": "https://unpkg.com/react-dom@^17/umd/react-dom.production.min.js"},
102+
"react-intl": {"src": "https://unpkg.com/react-intl-bundle@^1/dist/react-intl.production.min.js"}
103+
},
104+
```
105+
24106
### Continuous Integration
25107

26108
Testing and deployment scripts available inside this example repository using [GitHub Actions](https://github.com/features/actions).
@@ -97,7 +179,7 @@ document.loadWidgets({
97179
| ------------------ | -------- | ------------------------ | ----------------------- | ------------------------------------------------------------------------- |
98180
| renderFunctionName | yes | | `renderExampleWidget` | The render function callback. |
99181
| instanceId | yes | | `example-widget-1` | The already present HTML element ID where the react app will be rendered. |
100-
| language | no | en-us | de | The language code for internationalization purposes. |
182+
| language | no | en | de | The language code for internationalization purposes. |
101183
| origin | no | `window.location.origin` | https://www.example.org | Protocol and hostname where a JSONAPI endpoint is available. |
102184
| onRenderFinish | no | | | A callback that executes after the widget has been rendered. |
103185

@@ -157,7 +239,7 @@ Create all translation messages in `src/messages.js`, following the provided exa
157239
| German | de |
158240
| English | en |
159241
| Spanish | es |
160-
| Latin American Spanish | es-la |
242+
| Latin American Spanish | esla |
161243
| French | fr |
162244
| Italian | it |
163245
| Japanese | ja |

widget.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
"src": "https://unpkg.com/react-intl-bundle@^1/dist/react-intl.production.min.js"
4646
}
4747
},
48-
"title": "Example Widget"
48+
"title": "Example Widget",
49+
"status": "stable"
4950
}

0 commit comments

Comments
 (0)