View Composing

Introduction

When Attaching views from different apps, the views are stacked on top of each other. In most cases, that's not what we want. Instead, we would like to compose elements in the views to make the result look like one page. That's what View Composing does:

In the example above, there are two apps, PetList and MedicalRecord. By default, they are stacked on top of each other. This makes it seem like they are not related, when they actually are. By Composing, we can move the table of examinations into the card from the PetList app and make it look like one coherent concept. In essence, we are changing the composition but not the content to combine apps that were not explicitly built to share the same screen. This is done without touching the source code of the individual apps.

This ability of modifying the composition of views coming from different apps is crucial, especially when working with many apps. Without it, there would just be a stack of views with no meaningful visual context, as shown in this illustration:

Composing was previously called "client-side-blending"

Composition and Content Separation

Composing works by replacing or modifying the default structure of HTML elements. For this to work, content and composition has to be separated. Shadow DOM handles this separation - the content is in light DOM and the composition is in Shadow DOM.

The structure of this separation looks like this:

<template>
<h1 slot="myapp/main-heading">My heading</h1>
<button slot="myapp/left-button">Go left</button>
<button slot="myapp/right-button">Go right</button>
<template is="declarative-shadow-dom">
<style>
.myapp-direction-controls {
display: flex;
justify-content: center;
}
</style>
<slot name="myapp/main-heading"></slot>
<div class="myapp-direction-controls">
<slot name="myapp/left-button"></slot>
<slot name="myapp/right-button"></slot>
</div>
</template>
</template>

Here, the content of the view is defined on the root level and the composition is defined inside the declarative-shadow-dom. The slot elements are insertion points for the content into the composition.

The declarative-shadow-dom is used as the default composition that can be further modified or replaced in runtime using the Starcounter system apps CompositionEditor and CompositionProvider.

Composing the attached views

Default compositions of the attached views

With the MedicalProvider and PetList example we have two views, each with its default composition:

<style>
@import '/PetList/style.css';
</style>
<div class="pet-list-wrapper">
<div class="pet-list-wrapper__row">
<slot name="petlist/details-name"></slot>
<slot name="petlist/details-age-and-animal"></slot>
</div>
<div class="pet-list-wrapper__row">
<slot name="petlist/details-owner-name"></slot>
<slot name="petlist/details-weight"></slot>
</div>
<slot name="petlist/details-list-link"></slot>
</div>
<slot name="medicalrecord/records-list-headline"></slot>
<slot name="medicalrecord/records-list-table"></slot>

When these two views are attached, the default composition from MedicalRecord is appended at the end of the PetList wrapper. The resulting composition looks like this:

<style>
@import '/PetList/style.css';
</style>
<div class="pet-list-wrapper">
<div class="pet-list-wrapper__row">
<slot name="petlist/details-name"></slot>
<slot name="petlist/details-age-and-animal"></slot>
</div>
<div class="pet-list-wrapper__row">
<slot name="petlist/details-owner-name"></slot>
<slot name="petlist/details-weight"></slot>
</div>
<slot name="petlist/details-list-link"></slot>
</div>
<slot name="medicalrecord/records-list-headline"></slot>
<slot name="medicalrecord/records-list-table"></slot>

Custom compositions of the attached views

To create a custom composition, we will move the MedicalRecord table and headline into the div class="pet-list-wrapper" and expand the width of the wrapper to fit the table:

<style>
@import '/PetList/style.css';
.pet-list-wrapper {
max-width: 750px;
}
</style>
<div class="pet-list-wrapper">
<div class="pet-list-wrapper__row">
<slot name="petlist/details-name"></slot>
<slot name="petlist/details-age-and-animal"></slot>
</div>
<div class="pet-list-wrapper__row">
<slot name="petlist/details-owner-name"></slot>
<slot name="petlist/details-weight"></slot>
</div>
<slot name="medicalrecord/records-list-headline"></slot>
<slot name="medicalrecord/records-list-table"></slot>
<slot name="petlist/details-list-link"></slot>
</div>

We have now produced the result shown in the image above; the view from the MedicalRecord app has been neatly integrated with the view from PetList.

The tool for creating these custom compositions is the CompositionEditor app. When this app is running, you can open it at any page with Ctrl + E. When the app opens, it gives you an HTML editor for the composition of your attached views.

There are two main concepts in the CompositionEditor - the identifier and composition ("layout" on the below picture).

The identifier represents the unique set of the attached views. For example, the identifier for the PetList and MedicalProvider example looks like this:

[partial-id="/sc/htmlmerger?PetList=/PetList/views/PetDetails.html&MedicalRecordProvider=/MedicalRecordProvider/views/RecordsList.html"]

The /sc/htmlmerger is a prefix that is added every time a view contains views that come from more than one response. In the identifier above, it's the merged views of PetDetails.html and RecordsList.html.

The composition changes made in the editor are displayed in real time.

Compositions are saved in the database and can be queried for with SELECT * FROM Starcounter.HTMLComposition. Since the CompositionEditor interacts with the database, it should not run in production.

Compositions can also be accessed with the static methods GetUsingkey(string key) and GetUsingKeyAndVersion(string key, string version)on the HTMLComposition class. These methods return an HTMLComposition with the specified key, or key and version.

Providing custom compositions

Custom compositions are provided by the CompositionProvider app. When views are merged, this app checks if there's a composition in the database with the same identifier and serves it, otherwise, the default composition is used.

Because of this, the CompositionProvider has to be running if you want to render custom compositions of your views.

Result

Composing in action

This screenshot is a result of several of our sample apps running together:

As you can see, they look like one app because of Attaching and Composing.

Summary

Composing allows us to make views coming from multiple apps to look like one by rearranging the HTML elements the Shadow DOM. Thus, the actual content and functionality defined in the light DOM will stay the same. The revised composition is stored in the database and retrieved when the views are attached. All of this is done without touching the source code of the apps.

Read More