Skip to content

Commit f851601

Browse files
committed
Add properties
1 parent 6494114 commit f851601

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

Writerside/topics/webview/web-components-api.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,54 @@ class MyExampleComponent implements HasLifecycleCallbacksInterface
147147
}
148148
```
149149

150-
## Component Methods
150+
## Properties
151+
152+
Each web component supports the ability to create properties and manage them.
153+
154+
```js
155+
let el = document.createElement('my-element');
156+
157+
el.exampleProperty = 42;
158+
```
159+
160+
By default, this property does not affect the operation of PHP code in any way.
161+
However, if you need to track the state of properties, you can use the
162+
corresponding `Boson\WebView\Api\WebComponents\Component\HasPropertiesInterface`
163+
interface.
164+
165+
```php
166+
class MyExampleComponent implements HasPropertiesInterface
167+
{
168+
public function onPropertyChanged(string $property, mixed $value): void
169+
{
170+
// ...
171+
}
172+
173+
public static function getPropertyNames(): array
174+
{
175+
// ...
176+
}
177+
}
178+
```
179+
180+
In particular, if you need to receive information about changes in property
181+
`exampleProperty`, then you should add it to the `getPropertyNames()` list.
182+
183+
```php
184+
public static function getPropertyNames(): array
185+
{
186+
return [ 'exampleProperty' ];
187+
}
188+
```
189+
190+
<warning>
191+
<b>Properties</b> and <a href="#attributes"><b>attributes</b></a> are different
192+
things. Properties are located directly on the object and can contain arbitrary
193+
data, while an attribute can be specified in HTML tags and can contain
194+
exclusively string values.
195+
</warning>
196+
197+
## Methods
151198

152199
Each web component supports the ability to create methods and process them.
153200

@@ -244,7 +291,7 @@ try {
244291
}
245292
```
246293

247-
## Component Attributes
294+
## Attributes
248295

249296
In addition to methods, each HTML element has attributes. You can subscribe
250297
to change, add or remove an attribute by implementing the
@@ -280,8 +327,7 @@ the attribute value changes.
280327
| `string` | `string` | Attribute value has been changed |
281328
| `null` | `string` | Attribute has been removed |
282329

283-
284-
## Component Events
330+
## Events
285331

286332
Each web component supports the ability to listen several component events
287333
and process them.

0 commit comments

Comments
 (0)