Widget descriptor is a DOM element used for the description of:
The widget descriptor must be a <script>
element with the attribute data-union-widget
. This element can also contain additional attributes.
data-union-widget
: string*Name of the widget. In order to pair a widget descriptor with a route, this attribute must equal to a path
property of a route.
data-union-container
: stringid
attribute of an HTML element in which the widget returned by route will be rendered. container
should be unique within the scope of a widget descriptor.
data-union-namespace
: stringString that represents a unique ID of a widget descriptor. By default, the value of namespace
is set to value within container
.
Value will be available in the widget root component under the namespace
property.
Please note, that at least one of the data-union-container
or data-union-namespace
attributes must be specified.
Additionally, you may pass any valid JSON as the content of the script element, which will be available in the widget root component under the data
property.
Every widget will get it's initial data and namespace to the Root component of widget.
// react-union-boilerplate-basic/src/widgets/Content/Components/Root.js
import React from 'react';
const Root = ({ namespace, data }) => (
<div>
I am widget Content. With namespace: <b>{namespace}</b> and initial data:
<b>{JSON.stringify(data)}</b>
</div>
);
export default Root;
But sometimes can be handy to access these data from nested components.
To avoid passing down these props, we provide a withWidgetContext HOC.
It renders the widget returned by the route with the path my-widget
to element with id my-widget-root
.
<div id="my-widget-root"></div>
<!-- Widget descriptor -->
<script
data-union-widget="name"
data-union-container="my-widget-root"
type="application/json"
>
</script>
namespace
It renders the widget returned by the route with the path my-widget
to the element with id my-widget-root
. The instance is used internally under the key unique-string
(provided by namespace
).
<div id="my-widget-root"></div>
<!-- Widget descriptor -->
<script
data-union-widget="my-widget"
data-union-container="my-widget-root"
data-union-namespace="unique-string"
type="application/json"
>
</script>
If you need to provide custom data to the instance of your widget, pass a valid JSON as the content of the script element.
<div id="my-widget-root"></div>
<!-- Widget descriptor -->
<script
data-union-widget="my-widget"
data-union-container="my-widget-root"
type="application/json"
>
{
"customField": "custom field",
"urls": {
"home": "www.example.com",
"api": "/api"
}
}
</script>
If you only need to provide data to other widgets in your application, there is no need to render anything.
<!-- Widget descriptor -->
<script
data-union-widget="my-widget"
data-union-namespace="my-widget-namespace"
type="application/json"
>
{
"customField": "custom field",
"urls": {
"home": "www.example.com",
"api": "/api"
}
}
</script>