View Composing
Last updated
Last updated
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"
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:
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.
With the MedicalProvider and PetList example we have two views, each with its default composition:
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:
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:
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:
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.
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.
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.
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.
Layout compositions for HTML partials - Blog October 2016
Shadow DOM v1: Self-Contained Web Components - Google Developers