2.2.3 • Published 9 months ago

@cmmn/ui v2.2.3

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

UI library

Simple but reach components based on CustomElements, uhtml and @cmmn/cell2.

component can be generated by comman cmmn gen my-comp ./components

it will generate 3 files: my-comp.component.ts, my-comp.template.ts and my-comp.style.less

Component example:

@Injectable(true) // enabled DI (1)
@component({
    name: 'my-comp', // name of custom element, so use it with <my-comp /> in html 
    template: (html, state, events) => html` 
    <div onclick=${events.onClick()}>${state.Version}</div>
  `, // template is just a template literal (2)
    style, // will be injected in head, no scoping (3)  
})
export class MyComponent extends HtmlComponent<IState, IEvents> {
    // injecting some services  
    constructor(private api: ApiService) {
        super();

        this.useAction(this.someAction, () => [
            this.Version,
        ], ActionSubscribeType.OnFirstRender);
    }

    // properties are mappend from attributes (2)
    @property()
    protected version!: number;

    someAction([version]) {
        // it will be called every time the version changes after first render
    }

    @action(() => version)
    otherAction(version) {
        // it will be called every time the version changes after component will be connected
    }

    @effect()
    someEffect() {
        // it will be called after first render    
    }


    @effect(() => this.version)
    otherEffect() {
        // it will be called after renders, if version have changed    
    }

    connectedCallback() {
        super.connectedCallback(); // important, should be called
        // it will be unsubscribed in disconnectedCallback
        this.onDispose = this.on('render', console.log);
    }

    // render will be after it changed in animationFrame callback
    // frequent renders will be combined in one animationFrame
    // it will not render if:
    // * error have been thrown
    // * State returns undefined
    // * State returns smth comparable to previous result (compare(newValue, oldValue) == true)
    // you can try-catch errors here and return other error state
    get State() {
        try {
            return {
                Version: this.version
            }
        } catch (e) {
            return {Version: 'error'}
        }
    }

    // handle errors in source = effects|aciton|state|template
    onError(error: Error, source) {
        if (source == 'effect' || source == 'action')
            return;
        // by default it will warn in console
        super.onError(error, source);
    }
}
  1. About DI you can read at @cmmn/core
  2. About templates you can read at uhtml, but
    1. html... renders inside current component
    2. html()... will creates new elements every time
    3. html(key) will creates new elements every time key will change
    4. html(obj, key) will creates new elements every time key or obj will change
    5. html has cache which you can see in html.cache
  3. Style should be just a string, your bundler should hanlde it, as @cmmn/tools for example
  4. Properties are mapped from html attributes of your custom element.
    1. Naming will change from camelCase to snake-case in dom. (myProp in js <-> my-prop in dom)
    2. It supports most of js values like objects, functions, numbers, strings...
    3. It compares value equality by Fn.compare from @cmmn/core, so it can call .equals() of value, f.e.
  5. Effects are functions that will be called after render have been, like in React.
    1. You may provide a filtering function: only if result have changed, effect will be called
  6. Actions are functions that will be called if their dependencies will change
    1. It can be subscribed at onConnected: when element attached to dom
    2. Or atFirstRender: when it was rendered first
  7. You can use this.element for direct access to dom-element
  8. You can use this.element.parentElement.component for access to parent component, if parentElement is root of it.
  9. You can access injected content via this.Children. It is stored in connectedCallback.
  10. HtmlComponent is EventEmitter of render | connected | dispose
2.2.3

9 months ago

2.2.2

9 months ago

2.2.1

10 months ago

2.2.0

12 months ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.9.13

1 year ago

1.9.12

1 year ago

1.9.11

1 year ago

1.9.10

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.8.2

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.9.9

1 year ago

1.9.7

1 year ago

1.9.6

1 year ago

1.9.4

1 year ago

1.9.3

1 year ago

1.9.2

1 year ago

1.8.6

1 year ago

1.8.4

1 year ago

1.8.3

1 year ago

1.7.19

2 years ago

1.7.11

2 years ago

1.7.12

2 years ago

1.7.13

2 years ago

1.7.14

2 years ago

1.7.15

2 years ago

1.7.16

2 years ago

1.7.17

2 years ago

1.7.18

2 years ago

1.7.10

2 years ago

1.7.9

2 years ago

1.7.8

2 years ago

1.7.7

2 years ago

1.7.6

2 years ago

1.7.5

2 years ago

1.7.4

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.0

2 years ago

1.6.27

2 years ago

1.6.29

2 years ago

1.6.26

2 years ago

1.6.4

3 years ago

1.6.3

3 years ago

1.6.11

3 years ago

1.6.10

3 years ago

1.6.13

3 years ago

1.6.12

3 years ago

1.6.15

3 years ago

1.6.14

3 years ago

1.6.17

3 years ago

1.6.19

3 years ago

1.6.18

3 years ago

1.6.20

2 years ago

1.6.21

2 years ago

1.6.24

2 years ago

1.6.25

2 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.10

3 years ago

1.5.12

3 years ago

1.5.11

3 years ago

1.5.13

3 years ago

1.5.9

3 years ago

1.5.8

3 years ago

1.5.7

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.4.6

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.20

3 years ago

1.4.21

3 years ago

4.0.6-cmmn

3 years ago

1.4.9

3 years ago

1.4.11

3 years ago

1.4.8

3 years ago

1.4.10

3 years ago

1.4.7

3 years ago

1.4.13

3 years ago

1.4.12

3 years ago

1.4.17

3 years ago

1.4.16

3 years ago

1.4.19

3 years ago

1.4.18

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago