Skip to content

Commit 6494114

Browse files
committed
Actualize web components page
1 parent 0f5e843 commit 6494114

1 file changed

Lines changed: 83 additions & 20 deletions

File tree

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

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ $app->webview->html = '<my-element>Example</my-element>';
4040
```
4141

4242
For more convenient component management, you can use inheritance from the
43-
`Boson\WebView\Api\WebComponentsApi\WebComponent` class.
43+
`Boson\WebView\Api\WebComponents\WebComponent` class.
4444

4545
```php
46-
use Boson\WebView\Api\WebComponentsApi\WebComponent;
47-
4846
class MyExampleComponent extends WebComponent
4947
{
5048
// do something
@@ -56,12 +54,10 @@ class MyExampleComponent extends WebComponent
5654
By default, the component does not contain any HTML content (it uses the
5755
default body one passed to it). If you want to decorate it somehow or define
5856
custom content, you should add the
59-
`Boson\WebView\Api\WebComponentsApi\HasTemplateInterface` interface and
57+
`Boson\WebView\Api\WebComponents\Component\HasTemplateInterface` interface and
6058
implement `render()` method.
6159

6260
```php
63-
use Boson\WebView\Api\WebComponentsApi\HasTemplateInterface;
64-
6561
class MyExampleComponent implements HasTemplateInterface
6662
{
6763
public function render(): string
@@ -76,7 +72,7 @@ class MyExampleComponent implements HasTemplateInterface
7672
## Shadow DOM
7773

7874
In order to switch to shadow house rendering mode, you should implement
79-
the `Boson\WebView\Api\WebComponentsApi\HasShadowDomInterface` interface.
75+
the `Boson\WebView\Api\WebComponents\Component\HasShadowDomInterface` interface.
8076

8177
The shadow DOM is similar to the regular renderer, but isolates its behavior
8278
from global styles and supports slots.
@@ -90,8 +86,6 @@ See more information about templates and slots in
9086
</note>
9187

9288
```php
93-
use Boson\WebView\Api\WebComponentsApi\HasShadowDomInterface;
94-
9589
class MyExampleComponent implements HasShadowDomInterface
9690
{
9791
public function render(): string
@@ -135,12 +129,10 @@ const component = document.createElement('my-element');
135129

136130
In order to accurately determine that an element is connected to any physical
137131
node of the DOM document, the
138-
`Boson\WebView\Api\WebComponentsApi\HasLifecycleCallbacksInterface`
132+
`Boson\WebView\Api\WebComponents\Component\HasLifecycleCallbacksInterface`
139133
interface should be implemented.
140134

141135
```php
142-
use Boson\WebView\Api\WebComponentsApi\HasLifecycleCallbacksInterface;
143-
144136
class MyExampleComponent implements HasLifecycleCallbacksInterface
145137
{
146138
public function onConnect(): void
@@ -167,7 +159,7 @@ If you leave the code as is, then when you click on the element,
167159
a JS error will be thrown: `Uncaught TypeError: this.update is not a function`.
168160

169161
To implement a method (for example, "`update()`"), you should implement the
170-
`Boson\WebView\Api\WebComponentsApi\HasMethodsInterface` interface.
162+
`Boson\WebView\Api\WebComponents\Component\HasMethodsInterface` interface.
171163

172164
```php
173165
class MyExampleComponent implements HasMethodsInterface
@@ -256,12 +248,10 @@ try {
256248

257249
In addition to methods, each HTML element has attributes. You can subscribe
258250
to change, add or remove an attribute by implementing the
259-
`Boson\WebView\Api\WebComponentsApi\HasObservedAttributesInterface` interface.
251+
`Boson\WebView\Api\WebComponents\Component\HasAttributesInterface` interface.
260252

261253
```php
262-
use Boson\WebView\Api\WebComponentsApi\HasObservedAttributesInterface;
263-
264-
class MyExampleComponent implements HasObservedAttributesInterface
254+
class MyExampleComponent implements HasAttributesInterface
265255
{
266256
public function onAttributeChanged(
267257
string $attribute,
@@ -271,14 +261,14 @@ class MyExampleComponent implements HasObservedAttributesInterface
271261
// ...
272262
}
273263

274-
public static function getObservedAttributeNames(): array
264+
public static function getAttributeNames(): array
275265
{
276266
// ...
277267
}
278268
}
279269
```
280270

281-
Method `getObservedAttributeNames()` must return a list of attributes
271+
Method `getAttributeNames()` must return a list of attributes
282272
(strings) to be processed.
283273

284274
Method `onAttributeChanged()` contains a callback that is called when
@@ -290,6 +280,79 @@ the attribute value changes.
290280
| `string` | `string` | Attribute value has been changed |
291281
| `null` | `string` | Attribute has been removed |
292282

283+
284+
## Component Events
285+
286+
Each web component supports the ability to listen several component events
287+
and process them.
288+
289+
To implement an event listener (for example, "[`click`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent)"),
290+
you should implement the `Boson\WebView\Api\WebComponents\Component\HasEventListenersInterface` interface.
291+
292+
```php
293+
class MyExampleComponent implements HasEventListenersInterface
294+
{
295+
public function onEventFired(string $event, array $args = []): void
296+
{
297+
// ...
298+
}
299+
300+
public static function getEventListeners(): array
301+
{
302+
// ...
303+
}
304+
}
305+
```
306+
307+
If you want to listen `click` event, then `getEventListeners()` method
308+
must return the corresponding name.
309+
310+
```php
311+
public static function getEventListeners(): array
312+
{
313+
return [
314+
'click' => [],
315+
];
316+
}
317+
```
318+
319+
If you require any specific information related to an event, then the list
320+
of event fields should be passed as an array value.
321+
322+
For example, `click` event contain readonly properties such as `clientX`
323+
and `clientY`.
324+
325+
```php
326+
public static function getEventListeners(): array
327+
{
328+
return [
329+
'click' => [
330+
// "clientX" property from "click" event
331+
'clientX',
332+
// "clientY" property from "click" event
333+
'clientY',
334+
],
335+
];
336+
}
337+
```
338+
339+
If specific PHP array key names are required for these properties,
340+
they should be specified as keys.
341+
342+
```php
343+
public static function getEventListeners(): array
344+
{
345+
return [
346+
'click' => [
347+
// "clientX" property from "click" event passed as "x"
348+
'x' => 'clientX',
349+
// "clientX" property from "click" event passed as "y"
350+
'y' => 'clientY',
351+
],
352+
];
353+
}
354+
```
355+
293356
## Reactive Context
294357

295358
The reactive context allows you to modify and retrieve values from a
@@ -298,7 +361,7 @@ component programmatically.
298361
By default, it is passed to the constructor as the first argument.
299362

300363
```php
301-
use Boson\WebView\Api\WebComponentsApi\ReactiveContext;
364+
use Boson\WebView\Api\WebComponents\ReactiveContext;
302365
use Boson\WebView\WebView;
303366

304367
class MyExampleComponent

0 commit comments

Comments
 (0)