Skip to main content

Viewports

A Node can display other Nodes, usually by providing a layout for them. This is done by using a property called a viewport, which accepts child Nodes to display.

A viewport property connects to other Nodes' view properties, which contain render functions. For browser-based viewports, the property should use the datatype web-view[], defined as follows:

properties.yaml
viewport:
datatype: web-view[]
Note

web-view[] is an array of web-view. This means the property can accept a list of web-view datatype values from view properties.

A viewport property is typically used to display other Nodes' view properties. In order to display other Nodes, we first need to define a view property (see Views).

properties.yaml
view:
datatype: web-view
viewport:
datatype: web-view[]

Once the view and viewport properties are set up, we need to define a render function for view that uses the child Nodes connected to viewport.

To get the data from Nodes connected to viewport, we use context.get() just like with any other property.

src/main.js
// viewport will contain an array of render functions
const viewport = context.get('viewport');

Please see the Demos below for different implementations of parent render functions that loop through the viewport data and render the children.

Note

A Viewport Property does not have to use the property key viewport, other keys can be used. A Node can also have multiple viewports. However, if your node only has a single viewport, using viewport as the key will follow standard naming convention.

properties.yaml
gallery_viewport:
datatype: web-view[]
alt_gallery_viewport:
datatype: web-view[]

Demo

This is a very basic example to illustrate the concept of a viewport. Consider using a templating engine like pug, hyperscript, lit, or a frontend framework.

 

src/main.js
// This render function will render all the children connected to viewport
function render(element) {

// Since ((viewport)) has the datatype \`web-view[]\`, an array of render functions will be returned
const viewport = context.get('viewport') || [];

// Loop through each item and call the function provided
viewport.forEach((viewRender) => {

// Create an element for the child to mount to
const viewElement = document.createElement('div');
element.appendChild(viewElement);

// Pass in the element to the child render function
viewRender?.(viewElement);
});
}

context.set('view', render);