Fundamentls
Work in Progress
Thee is yet a work in progress in this documentation, it means that some addiotnal content, scenarios and example are yet to be brought, nevertheless the current content cover from the basics to more elaborated scenarios.
ViewComponent and BaseService import in CDN/Lone component mode
The import (e.g. import { ViewComponent } from "@still/component/super/ViewComponent.js"
) of ViewComponent and BaseService classes is inexistent CDN mode setup, as this is already provided by CDN itself.
Overview
-
In Still.js, a Component represents any UI output, from a full application to a widget or a microfrontend. Components can be reusable, navigable (for UI/pages), and embedded within other applications.
-
A component must extend
ViewComponent
and define atemplate
for rendering. If initialization logic is needed, a Special methods (lifecycle/Hook) can be implemented, if constructor is in placesuper()
needs to be called as first line. -
For proper localization/routing, both Path and URL must be defined in
route.map.js
. However, this is handled automatically when creating components via still-cli (check bellow). Theapp-setup.js
manages application-level configurations, but for these tutorials we'll be touching it just to set the initial component we want to be loaded.
In Still.js, the template variable contains pure HTML, a defined WebComponent, <st-element> tags, or a combination of these, as it is based on vanilla web technologies (HTML, CSS, and JavaScript).
1. Simple example
Conceptually, all components extends from ViewComponent, and, behide the, therefore, such extending component can serve as a whole page or a simple part of a specific page.
Component Creation
-
The recommended way fo Component creation is by using the still-cli utility (@stilljs/cli on npm), for that we run the command as follow:
Component creation folder context
The component creation command instruction needs to be run inside s Still.js project, or in the
app/
(which is inside the Still.js project) folder or a subfolder of theapp/
folder -
The create command for component options also provides with aliases, in this case we can abbreviate both the comand and the type of object to be created (component in this case):
In the above example, you'll be creating a component with name UserDataTable in the specified path, there is no need to create he folders and sub-folders, as in case they don't exist it'll get created.
1.1 Component required/reserved variables (Reserved for the Framework)
-
isPublic - This states if the component can be accessed without authentication or not.
-
template - Declares the UI itself by using differnt murkups (HTML, Still elements, Web-Component) and stylesheets (CSS). In case the template is defined in an HTML (see here) file then temlate variable should not exist in the component.
In adition to isPublic and template, there are other features which can be used inside the javaScript part of the component such as the special method/Hooks (see hooks section), follow an example:
The template (HTML) content will be display accordingly once the template as rendered. Again, template can also contain Web-component and/or Still component which would be a child component in this case.
2. Component State vs Property
Those are 2 of the existing ways for the component to hold data, therefore, they serve different purpose, as State is reactive and takes affect on the component lifecycle, whereas Property does not. Follow the example:
When it comes to retrieve the value from a property we need to reference the .velue
(line 25), but this is not the case for property (line 33).
To listen to a State change reactively, some ways are provided, but more recurrent are binding it to the template, and subscribing to it as follows in the side code snippets:
// Via property binding in the template it automatically listen to changes reactively
template = `
<div>User Name: @firstName
<button> @saveButtonLabl </button>
`;
// This is a Special method/Hook which can be declared in the component
stAfterInit(){
this.firstName.onChange(newValue => {
console.log(`User first name changed to ${newValue}`);
});
}
3. Nested Component
It's possible to put one component inside another, nevertheless, in order to guarantee the better performance, Still.js only allow 2 levels of nested component as follow:
Parent Component | |
---|---|
Above we have to parent component, which has one level of offsprings, additional level is not allowed using <st-element></st-element>
, anyway it can be achieved by using Web-component or regular HTML. (see best preactices sectio).
Nesting Component considerations
Unlike Still.js component, Web-component and HTML tags are handled directly by the Browser, which means less burden and more performant UI, therefore, the concept in Still.js is that you have to design well the component and use <st-element></st-element> to handle the complex parts of your UI, and 3+ level of nested component for dealing with visual/formating aspects, this definitely will guarantee better performance.
4. Component to Component communication
Covering from the most basic to the most complex scenarios, Still.js provides wide different means of providing communication from component to component (e.g. Parent to child, Sibling to Sibling, any component to any other(s)). (see to the Component Communication for more details)
4.1 Parent to Child communication
Parent Component | |
---|---|
Parent component is passing 2 (hieghestOffer, leadBidderName) properties when referencing the child, those are states variables in the child whichwill be overriden.
Just like it's possible to update child state by passing if as property in <st-element>
tag, we can also pass method as follow:
Unlike state and property variables, method on the <st-element>
component need to be references in paranthesis (), also, normally in the child we'll have the method signature, nevertheless we can also have it's own scope.
It's also possible for the child to pass values to parent when executing method signature, follow the example:
```js title="Child Component" hl_lines="8 13" linenums="1" import { ViewComponent } from "../../../@still/component/super/ViewComponent.js";
export class BidOffersComponent extends ViewComponent {
isPublic = true;
template = `
<button (click)="processMyData(30, 'John')">
Call Parent Function
</button>
`;
myMethodSignature(age, name){}
}
In addition to using <st-element>
component props, there are other means available for component to component communication such as Pub/Sub
, both @Proxy
and ref
which is done to through <st-element>
again, and the Service
which is a global kind. (see to the Component Communication).
Embieding component in your regular HTML or within other Frameworks
When it comes to components, Still.js provides the Lone Component which only require to reference the CDN for both CSS and JavaScript files thereby not needing to create a Still.js project, therefore, this approach can be followed either for small use case as well as for complex ones such as Microfrontend. (follow do documentation here)