The LS framework allows you to work with custom HTML elements as components.
Clone repository
git clone https://github.com/LubosSremanak/lsjs.git
Install dependencies
npm install
Development server
webpack serve
Build for production
webpack
Generator
Each component must be declared in the module.
import Module from "../../ls/model/module";
import {RootComponent} from "./root/root.component";
const components = [
RootComponent,
]
new Module(components);
RootComponent
<app-router></app-router>
RouterModule
import RouterModule from "../../ls/model/router.module";
import {HomeComponent} from "./root/home/home.component";
const routes = [
{path: '', component: HomeComponent},
]
new RouterModule(routes);
All elements with the navigate attribute are automatically bound to the onclick navigator
<button navigate="generator">Generator</button>
Each component have injected Router
this.router.navigate('home');
All components are merged into the app-root component, located in index.html.
- selector
- selector.component.html
- selector.component.css
- selector.component.js
Each component must extend the Component class and pass a constant __dirname, in the super constructor.
import {Component} from "../../../ls/model/component";
export class RootComponent extends Component {
constructor() {
super(__dirname);
}
async onInit() {
}
async onChange(change) {
}
async onDestroy() {
}
}
Method onInit is executed after shadow DOM initialization.
async onInit() {
console.log(this.dom) // in this moment, we can access elements in component
}
Method onDestroy is executed after prop change.
change:{ name, oldValue, newValue, isFirst }
All props must be defined in static array 'props' to detect the change.
static props=['name','color'];
Method onDestroy is executed after component is removed from DOM.
Component is represented by a shadow DOM that provides encapsulation.
The DOM object of component is stored in this.dom
.
We have injected Renderer service for work with component DOM.
Renderer service makes it easier to work with the DOM and reduces code.
createComponent
const myComponent=this.renderer.createComponent(MyComponent);
//alternative
this.create('app-myComponent');
getSelector
const selector = this.getSelector(MyComponent);
// return: 'app-myComponent'
get
const element=this.renderer.get('elementId');
//alternative
this.get('elementId');
append
// append <app-myComponent></app-myComponent> to div with id="containerId"
this.renderer.append('containerId',MyComponent);
prepend
// prepend <app-myComponent></app-myComponent> to div with id="containerId"
this.renderer.prepend('containerId',MyComponent);
change
// remove the inside of the container
// append <app-myComponent></app-myComponent> to div with id="containerId"
this.renderer.change('containerId',MyComponent);
remove
// remove HTML element"
this.renderer.remove(element);
removeById
// remove HTML element with id="elementId"
this.renderer.removeById('elementId');
removeAllByClass
// remove all HTML elements with class="image"
this.renderer.removeAllByClass('image');
Communication with child.
getProp
// Get prop passed to component
const name=getProp('name');
setProp
//Set prop of component
const component=this.get("id");
this.renderer.setProp(component, 'name', 'yourName');
bindProp
//prop name and set name must be same
//parent
<app-element name="Lubos"></app-element>
//app-element
<span set="name"></span>
// app-element
const name=this.renderer.bindProp('name');// return: 'Lubos'
bindValue
//change inner text of span with set="variableName"
//template
<span set="variableName"></span>
// component
this.renderer.bindValue('variableName','value');
Communication with parent.
createEmitter
//Create event to pass data to parent
const emmiter=this.renderer.createEmitter('removedSection',dataForParent);
emit
//Fire event in component
this.renderer.emit(emmiter);
createEmitterAndEmit
//Create and fire now event in component
this.renderer.createEmitterAndEmit('removedSection',dataForParent);
subscribe
//Listen on event
this.renderer.subscribe(eventArea,'removedSection',yourEventHandler);
subscribeById
//Listen on event by id
this.renderer.subscribe('eventAreaId','removedSection',yourEventHandler);