The props in Atomico are the way to associate the webcomponent properties and reactive attributes that trigger the logic or interface of the webcomponent.
Props is the Atomico recommended way to declare visible and accessible states at the instance level of your webcomponents, with props you can:
Access state via instance, example: document.querySelector("my-component").myStateProp
.
Dispatch events on prop value change, example: document.querySelector("my-component").addEventListener("myPropChange",console.log)
.
Reflect attributes as prop, example: <my-component my-prop="....">
to document.querySelector("my-component").myProp
.
define strict input types for props.
Any function that represents the webcomponent will be able to associate the static object props for the declaration of reactive properties and attributes, for example:
The prop names in Camel Case format will be translated to for use as an attribute to the Kebab Case format, this behavior can be modified through the "attr" property when using a structured declaration.
Structured declarations require the "type" property minimally.
Not all types can use the "reflect" properties.
The declaration of the "value" property can vary depending on the type.
Simple statements allow setting just type validations.
Improve the definition by adding utility declarations, allowing for example to reflect the property's value as attributes, automatically emit events or associate default values. Remember these types of declarations minimally require the use of the type property.
If the "reflect" property is set to true, its value is reflected as an attribute of the webcomponent, this is useful for the declaration of CSS states, example:
It allows dispatching an automatic event before the prop value change, example:
Where:
event.type
: String - optional, name of the event to be emitted when the prop is changed
event.bubbles
: Boolean - optional, indicates that the event can be listened to by containers.
event.detail
: Any - optional, allows to attach a custom detail for the event
event.cancelable
: Boolean - optional, indicates that the event can be canceled by any listener
event.composed
: Boolean - optional, allows the event to exceed the shadow-root limit
The special properties of the event are the well-known Event Init
, you can know more details in the attached documentation.
Atomico allows the definition of default values of the props.
The association of callback as value allows generating unique values for each instance of the webcomponent, this is useful with the Object and Array types since it eliminates the references between instances.
Atomico removes the use of "this" given its functional approach, but adds the hook [useProp] (hooks / useprop.md) which allows to reference a prop for use with a functional syntax, eg:
Atomico has a really efficient and simple type validation method, the type validation works in the following way:
the given value is transformed to the corresponding type, be it String, Number, Boolean, Array or Object, once transformed it is sent to the .
evaluates if the value is of the declared type:
If it corresponds to the type:
It is saved in props.
An event is emitted (if this has been configured in the prop).
It is reflected as an attribute (if this has been configured in the prop).
It is sent to the update queue and subsequent rendering.
It does not correspond to the type: an error is created by console with the following data:
target: Instance of the webcomponent.
value: Input value.
type: expected type.
Type | Supports reflect |
---|---|
String
✔️
Number
✔️
Boolean
✔️
Object
✔️
Array
✔️
Promise
❌
Symbol
❌
Function
❌
All references to existing types in the browser(HTMLElement, Element, Node, Date, File... more than 300 😎)
❌