@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
134 lines (96 loc) • 4.12 kB
Markdown
# [ESL](../../../) Base Element
Version: *1.2.0*
Authors: *Alexey Stsefanovich (ala'n)*
<a name="intro"></a>
Provides the core for creating custom elements and TS (TypeScript) decorators to simplify component creation.
**ESLBaseElement** - base class for custom (tag) element declaration
### Base Element static API
- `MyElement.is` - property that defines tag name
- `MyElement.observedAttributes` - an array of attributes to observe
- `MyElement.register` - register component inside `customElements` registry
- `MyElement.registered` - returns a promise that will be resolved as soon as the component is registered
### Base Element API
Properties:
- `connected` - readonly marker, true if element connected and base `connectedCallback` executed
Attributes:
- `connectedCallback` - called when the element is appended to the DOM
- `disconnectedCallback` - called when the element is disconnected from the DOM
- `attributeChangedCallback` - called when the observable attribute is changed
(happens when the attribute is accessed for writing, independently of the actual value change)
- `$$cls` - checks or changes element CSS classes (uses CSSClassUtils)
- `$$attr` - checks or changes element attributes
- `$$fire` - dispatches event
- `$$on` - subscribes to the event manually or subscribes decorated method
- `$$off` - unsubscribes from the event manually or unsubscribes decorated method
### Element decorators
- `` - to map string type property to HTML attribute
- `` - to map boolean property to HTML boolean (marker) attribute state
- `` - to map object property to HTML attribute using JSON format to serialize / deserialize value
- `` - decorate a method with `ESLListenerDescriptor` props
- `` - a decorator to create prototype-level value definition.
It also gives an ability to override a property created via ``, `` or `` at the parent level
with non-attribute accessor value.
### Base Example
```ts
import {ESLBaseElement, attr, boolAttr, jsonAttr, listen} from '@exadel/esl';
class MyCustomComponent extends ESLBaseElement {
static is = 'my-element';
/** Reflects 'my-string-prop' attribute */
() public myStringProp: string;
/** Reflects to 'my-marker' attribute-marker */
() public myMarker: boolean;
/** Reflects to JSON value in 'my-config' attribute */
() public myConfig: Recorg<string, string>;
connectedCallback() {
super.connectedCallback();
// Init my component
}
disconnectedCallback() {
// Unsubscribe listeners, revert side effects
super.disconnectedCallback();
}
}
// Register custom tag with name provided in the static `is` property
MyCustomComponent.register();
// Or register custom tag with passed tag name
MyCustomComponent.register('my-tag');
```
### Event Listener example
The following listeners will be subscribed and unsubscribed automatically
```ts
import {ESLBaseElement, listen} from '@exadel/esl';
class MyCustomComponent {
('click')
onClick(e: MouseEvent) { /* Handle click event */}
({event: 'click', selector: '.btn'})
onBtnClick(e: MouseEvent) { /* Handle btn click event */}
}
```
### Event Listener manual example
Manual event listeners management
```ts
import {ESLBaseElement, listen} from '@exadel/esl';
class MyCustomComponent {
bindEvents() {
// Meta information fetched from `@listen` decorator
this.$$on(this.onClick);
// Subscribe event
this.$$on(this.onEvent, 'event');
// Subscribe event with descriptor
this.$$on(this.onEvent, {event: 'some-event'});
}
unbindEvents() {
// Unsubscribe listener related to `onClick` method
this.$$off(this.onClick);
// Unsubscribe `event`
this.$$off('event'); // or this.$$off(this.onEvent);
// Unsubscribe host event listeners that hadled by `window`
this.$$off({target: window});
// Unsubscribe all events
this.$$off();
}
({event: 'click', auto: false})
onClick(e: MouseEvent) { /* Handle btn click event */ }
onEvent(e: Event) { /* ... */ }
}
```