🧬
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.

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:
1
import { c } from "atomico";
2
3
function component() {
4
return <host />;
5
}
6
7
component.props = {
8
// Simple statement
9
value1: String,
10
// Structured statement
11
value2: {
12
type: String,
13
reflect: true,
14
attr: "advaceprop",
15
value: "default string",
16
event: {
17
type: "UpdateAdvanceProp",
18
bubbles: true,
19
},
20
},
21
};
22
23
customElement.define("web-component", c(component));
Copied!
Consider that:
  1. 1.
    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.
  2. 2.
    Structured declarations require the "type" property minimally.
  3. 3.
    Not all types can use the "reflect" properties.
  4. 4.
    The declaration of the "value" property can vary depending on the type.

Simple statements

Las declaraciones simples permiten asociar solo la validación de tipo.
1
component.props = {
2
propString: String,
3
propNumber: Number,
4
propObject: Object,
5
propArray: Array,
6
propBool: Boolean,
7
propCallback: Function,
8
};
Copied!

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

1
// valid declaration
2
component.props = { myName: String };
3
// valid declaration
4
component.props = { myName: { type: String } };
Copied!
Type
Supports reflect
String
✔️
Number
✔️
Boolean
✔️
Object
✔️
Array
✔️
Promise
Symbol
Function

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:
1
component.props = {
2
checked: {
3
type: Boolean,
4
reflect: true,
5
},
6
};
Copied!

Prop.event

It allows dispatching an automatic event before the prop value change, example:
1
component.props = {
2
value: {
3
type: String,
4
event: {
5
type: "change",
6
bubbles: true,
7
composed: true,
8
detail: "any value",
9
cancelable: true,
10
},
11
},
12
};
13
// listener
14
nodeComponent.addEventListener("change", handler);
Copied!
Donde :
  • 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.

Prop.value

Atomico allows the definition of default values of the props.
1
WebComponents.props = {
2
valueNormal: {
3
type: Number,
4
value: 100,
5
},
6
valueObject: {
7
type: Object,
8
value: () => ({}),
9
},
10
};
Copied!
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:
1
function component() {
2
const [message, setMessage] = useProp("message");
3
return (
4
<host>
5
Hello, {message}
6
<input oninput={({ target }) => setMessage(target.value)} />
7
</host>
8
);
9
}
10
11
component.props = { message: String };
Copied!
Last modified 8mo ago