🧬Props(Properties)
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="....">
todocument.querySelector("my-component").myProp
.define strict input types for props.
Syntax
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:
Consider that:
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
Simple statements allow setting just type validations.
Structured declaration
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.
Prop.type
String
✔️
Number
✔️
Boolean
✔️
Object
✔️
Array
✔️
Promise
❌
Symbol
❌
Function
❌
All references to existing types in the browser(HTMLElement, Element, Node, Date, File... more than 300 😎)
❌
Prop.reflect
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:
Prop.event
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 changedevent.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 eventevent.cancelable
: Boolean - optional, indicates that the event can be canceled by any listenerevent.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.
Prop.value
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.
Reactivity in the scope of the webcomponent
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:
Recommended articles
Is it advisable to declare events using the props API?Value cycle as propLast updated