Simple component model for small to medium sites. Usable from JS & TS.
npm install @mangoweb/scripts-base@mangoweb/scripts-baseThe manGoweb template for scripts employed on small to medium sized projects.
``bash`
npm install @mangoweb/scripts-base
⚠️ You might want to also fix the package version so that any potential future backwards incompatibilities don't break your build.
This package is generally intended for live development with the occasional BC break.
Should that affect your application because you failed to fix the version, that's on you.
In your index.ts file (or equivalent), use:
`typescript
import { initializeComponents } from '@mangoweb/scripts-base'
import { MyComponent } from './components/MyComponent'
initializeComponents([MyComponent], 'initComponents')
`
In your template:
`html`
You must:
- Inherit from Componentstatic componentName: string
- Define
`typescript
import { Component, DelegateEvent, EventListeners } from '@mangoweb/scripts-base'
interface MyComponentData {
foo: number
}
export class MyComponent extends Component
public static componentName = 'MyComponent'
protected getListeners = (): EventListeners => [
['click', this.handleClick],
['click', '.delegateSelector', this.handleDelegateClick],
// …
]
// The type of the argument depends on the actual event.
// It could also be, for instance, a KeyboardEvent
private handleClick(event: MouseEvent) {
console.log('clicked', this.data.foo)
}
// Careful: this only works for events that bubble.
private handleDelegateClick(event: DelegateEvent<'click'>) {
console.log('delegate target', event.delegateTarget)
}
}
`
The Component superclass accepts up to three generic parameters, all of which are optional. The first one, thedata
structure of , defaults to {}. The second one is the type of el. It defaults to HTMLElement but is usefulWindow
to change when we want to assert that it is something else (e.g. , HTMLBodyElement, SVGElement, etc.).
The last parameter should only be necessary to change in very extreme situations. It is the map from supported event
types to their respective Event objects. It should be inferred from the type of el, although it may be necessary
to change, should the standard library prove to be incomplete.
The EventListeners return type of getListeners optionally accepts the same two parameters as the last two ofComponents. It may be necessary to specify one or both if inference fails.
#### Life-cycle methods
The following happens to your component during initialization (in that order):
1. The constructor is called (if it exists) as your component is instantialized.init
2. Event listeners are attached
3. The method is called (if it exists)
You typically don't need to implement a constructor but it can be useful to avoid TS2564.
#### Failed initialization
If for whatever reason you decide that the component is unable to run, just throw a ComponentInitializationError from either the constructor or the init method.DEBUG
You don't need to worry about any impact on production environment ‒ the error is always caught and its message only displayed when is true.
Valid reasons for yielding and error include:
- Invalid data supplied
- A crucial element in the DOM is not found
- A crucial API is not present
#### More specific el
Optionally, you can also make an assertion that the element the component is attached to is an instance of a more specific interface than HTMLElement.
To that end, you may supply the second generic parameter.
For example, to attach your component on a