This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@smoovy/component
TypeScript icon, indicating that this package has built-in type declarations

0.2.6 • Public • Published

@smoovy/component

Version Size

Very basic component manager

Installation

npm install --save @smoovy/component

Basic usage

every properties are static, so you can't use multiple managers in different scopes. To create a component simply use the @component decorator like this:

import { component } from '@smoovy/component';

@component({
  // this is a simple `querySelectorAll` call
  selector: 'footer'
})
export class Footer {
  public constructor(
    protected element: HTMLElement
  ) {
    // do init stuff with the element
  }
}

Lifecycle

The lifecycle is straight forward: Component gets created -> Component gets destroyed. In order to capture the destroy event and do some cleanup tasks, use the interface OnDestroy:

@component({
  selector: 'footer'
})
export class Footer implements OnDestroy {
  public constructor(
    protected element: HTMLElement
  ) {
    this.element.style.display = 'block';
  }

  public onDestroy() {
    this.element.style.display = null;
  }
}

Conditional rendering

Sometimes you don't want a component to be initialized. E.g. it's only an animation for desktop devices with a screen size bigger than 1024px. You can add a condition to the component initialization like this:

@component({
  ...
  condition: () => window.innerWidth > 1024
})
export class Footer {
  ...
}

Update components

Sometimes you need to refresh the component state. For instance if the route hase changed. You want to get rid of all the old components and inject the new ones:

import { ComponentManager } from '@smoovy/component';

// since there can only be one manager the update is quit simple
ComponentManager.update();

// only update components inside this scope element
// otherwise the root will be used, which is `document.body`
ComponentManager.update(document.querySelector('footer'));

// do not remove old components. Keep them running, even
// if they're not in the dom. This can be usefule if you
// have animations handled by components that are no longer
// in the dom and still need some time
ComponentManager.update(document.body, false);
setTimeout(() => ComponentManager.update(document.body), 200);

Development commands

// Serve with parcel
npm run serve

// Build with rollup
npm run build

// Run Jest unit tests
npm run test

// Run TSLinter
npm run lint

License

See the LICENSE file for license rights and limitations (MIT).

Package Sidebar

Install

npm i @smoovy/component

Weekly Downloads

0

Version

0.2.6

License

MIT

Unpacked Size

30.4 kB

Total Files

10

Last publish

Collaborators

  • davideperozzi