API Reference
The following functions are a public API of the hybrids library available as named exports:
define
define(tagName: string, descriptorsOrConstructor: Object | Function): Wrapperarguments:
tagName- a custom element tag name,descriptorsOrConstructor- an object with a map of hybrid property descriptors or constructor
returns:
Wrapper- custom element constructor (extendsHTMLElement)
define({ tagName: descriptorsOrConstructor, ... }): { tagName: Wrapper, ... }arguments:
tagName- a custom element tag name in pascal case or camel case,descriptorsOrConstructor- an object with a map of hybrid property descriptors or constructor
returns:
{ tagName: Wrapper, ...}- a map of custom element constructors (extendsHTMLElement)
property
property(defaultValue: any, [connect: Function]): Objectarguments:
defaultValue- any valueconnect- a connect callback function of the property descriptor
returns:
a property descriptor, which resolves to value
parent
parent(hybridsOrFn: Object | Function: (hybrids) => {...}: Boolean): Objectarguments:
hybridsOrFn- reference to an object containing property descriptors or a function, which returnstruewhen currenthybridsmeets the condition
returns:
a property descriptor, which resolves to
nullorElementinstance
children
children(hybridsOrFn: Object | Function: (hybrids) => {...}: Boolean, [options: Object]): Objectarguments:
hybridsOrFn- reference to an object containing property descriptors or a function, which returnstruewhen currenthybridsmeets the conditionoptions- object with available keys:deep- boolean, defaults tofalsenested- boolean, defaults tofalse
returns:
a property descriptor, which resolves to
arrayofElementinstances
render
render(fn: Function, options: Object = { shadowRoot: true }): Objectarguments:
fn(host: Element): Function- callback function withhostargument; returned function hashostandtargetargumentsoptions: Object- an object, which has a following structure:{ shadowRoot: true }(default value) - initializes Shadow DOM and settargetasshadowRoot{ shadowRoot: false }- setstargetargument ashost,{ shadowRoot: { extraOption: true, ... } }- initializes Shadow DOM with passed options forattachShadow()method
returns:
hybrid property descriptor, which resolves to a function
store
direct
store.get(Model: object, id?: string | object) : object;arguments:
Model: object- a model definitionid: string | object- a string or an object representing identifier of the model instance
returns:
Model instance or model instance placeholder
store.set(Model: object, values: object) : Promise;arguments:
Model: object- a model definitionvalues: object- an object with partial values of the model instance
returns:
A promise, which resolves with the model instance
store.set(modelInstance: object, values: object | null): Promise;arguments:
modelInstance: object- a model instancevalues: object | null- an object with partial values of the model instance ornullfor deleting the model
returns:
A promise, which resolves to the model instance or placeholder (for model deletion)
factory
store(Model: object, options?: id | { id?: string | (host) => any, draft?: boolean }): objectarguments:
Model: object- a model definitionoptions- an object with the following properties or the shorter syntax with the belowidfield valueid- ahostproperty name, or a function returning the identifier using thehostdraft- a boolean switch for the draft mode, where the property returns a copy of the model instance for the form manipulation
returns:
hybrid property descriptor, which resolves to a store model instance
guards
store.ready(model: object): booleanarguments:
model: object- a model instance
returns:
truefor a valid model instance,falseotherwise
store.pending(model: object): boolean | Promisearguments:
model: object- a model instance
returns:
In pending state a promise instance resolving with the next model value,
falseotherwise
store.error(model: object, propertyName?: string): boolean | Error | anyarguments:
model- a model instancepropertyName- a property name of the failed validation defined withstore.value()method
returns:
An error instance or whatever has been thrown or
false. WhenpropertyNameis set, it returnserr.errors[propertyName]orfalse
value
store.value(defaultValue: string | number, validate?: fn | RegExp, errorMessage?: string): String | Numberarguments:
defaultValue-stringornumbervaluevalidate- a validation function -validate(val, key, model), which should returnfalse, error message or throws when validation fails, or a RegExp instance. If omitted, the default validation is used, which fails for empty string and0.errorMessage- optional error message used when validation fails
returns:
a
StringorNumberinstance
html
html`<div property="${value}">${value}</div>` : Functionarguments:
HTML content as a template content
value- dynamic values as a property values or element content
returns:
an update function, which takes
hostandtargetarguments
html`...`.define(map: Object): Functionarguments:
map- object with hybrids definitions or custom element's constructors
returns:
update function compatible with content expression
html`...`.style(...styles: Array<string | CSSStyleSheet>): Functionarguments:
styles- a list of text contents of CSS stylesheets, or instances ofCSSStyleSheet(only for constructable stylesheets)
returns:
an update function compatible with content expression
html.resolve(promise: Promise, placeholder: Function, delay = 200): Functionarguments:
promise- promise, which should resolve/reject update functionplaceholder- update function for render content until promise is resolved or rejecteddelay- delay in milliseconds, after which placeholder is rendered
returns:
update function compatible with content expression
svg
svg`<circle property="${value}">${value}</circle>` : Functionarguments:
SVG content as a template content
value- dynamic values as a property values or element content
returns:
an update function, which takes
hostandtargetarguments
svg`...`.define(map: Object): Functionarguments:
map- object with hybrids definitions or custom element's constructors
returns:
update function compatible with content expression
svg`...`.style(styles: string, [styles: string]...): Functionarguments:
styles- text content of CSS stylesheet
returns:
update function compatible with content expression
svg.resolve(promise: Promise, placeholder: Function, delay = 200): Functionarguments:
promise- promise, which should resolve/reject update functionplaceholder- update function for render content until promise is resolved or rejecteddelay- delay in milliseconds, after which placeholder is rendered
returns:
update function compatible with content expression
dispatch
dispatch(host: Element, eventType: string, [options]): Booleanarguments:
host- element instanceeventType- type of the event to be dispatchedoptions- a dictionary, having the following optional fields:bubbles- a boolean indicating whether the event bubbles. The default is falsecancelable- a boolean indicating whether the event can be cancelled. The default is falsecomposed- a boolean indicating whether the event will trigger listeners outside of a shadow root The default is falsedetail- a custom data, which will be passed to an event listener
returns:
falseif event is cancelable and at least one of the event handlers which handled this event calledpreventDefault(), otherwise it returnstrue
Last updated
Was this helpful?