Skip to content

Commit 123b747

Browse files
laurenic0lhyyangosteenBASIS
authored
feat: expanding events section for elementcomposite (#514)
Co-authored-by: Hyyan Abo Fakher <hyyanaf@gmail.com> Co-authored-by: Garrison Osteen <95929849+gosteenBASIS@users.noreply.github.com>
1 parent 993e481 commit 123b747

File tree

1 file changed

+85
-12
lines changed

1 file changed

+85
-12
lines changed

docs/docs/building-ui/element-composite.md

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 1
33
title: Element Composite
4+
sidebar_class_name: has-new-content
45
slug: element_composite
56
---
67

78
<DocChip chip='since' label='23.06' />
89
<JavadocLink type="foundation" location="com/webforj/component/element/ElementComposite" top='true'/>
910

10-
The `ElementComposite` class serves as a versatile foundation for managing composite elements in webforJ applications. Its primary purpose is to facilitate the interaction with HTML elements, represented by the `Element` class, by providing a structured approach to handle properties, attributes, and event listeners. It allows for implementation and reuse of elements in an application. Use the `ElementComposite` class when implementing Web Components for use in webforJ applications.
11+
The `ElementComposite` class serves as a versatile foundation for managing composite elements in webforJ applications. Its primary purpose is to facilitate the interaction with HTML elements, represented by the `Element` class, by providing a structured approach to handle properties, attributes, and event listeners. It allows for implementation and reuse of elements in an app. Use the `ElementComposite` class when implementing Web Components for use in webforJ applications.
1112

1213
While using the `ElementComposite` class, using the `getElement()` method will give you access to the underlying `Element` component. Similarly, the `getNodeName()` method gives you the name of that node in the DOM.
1314

1415
:::tip
15-
It is possible to do everything with the `Element` class itself, without using `ElementComposite` class. However, the provided methods in the `ElementComposite` give users a way to reuse the work that's being done.
16+
it's possible to do everything with the `Element` class itself, without using `ElementComposite` class. However, the provided methods in the `ElementComposite` give users a way to reuse the work that's being done.
1617
:::
1718

18-
Throughout this guide, we'll be implementing the [Shoelace QR code web component](https://shoelace.style/components/qr-code) using the `ElementComposite` class.
19+
This guide demonstrates how to implement the [Shoelace QR code web component](https://shoelace.style/components/qr-code) using the `ElementComposite` class.
1920

2021
<ComponentDemo
2122
path='/webforj/qrdemo?'
@@ -25,7 +26,7 @@ height='175px'
2526

2627
## Property and attribute descriptors {#property-and-attribute-descriptors}
2728

28-
Properties and attributes in web components represent the state of the component. They are often used to manage data or configuration. The `ElementComposite` class provides a convenient way to work with properties and attributes.
29+
Properties and attributes in web components represent the state of the component. they're often used to manage data or configuration. The `ElementComposite` class provides a convenient way to work with properties and attributes.
2930

3031
Properties and attributes can be declared and initialized as `PropertyDescriptor` members of the `ElementComposite` class being written, and then used in the code. To define properties and attributes, use the `set()` method to set the value of a property. For example, `set(PropertyDescriptor<V> property, V value)` sets a property to a specified value.
3132

@@ -43,12 +44,12 @@ set(TITLE, "My Title");
4344
set(VALUE, "My Value");
4445
```
4546

46-
In addition to setting a property, utilize the `get()` method in the `ElementComposite` class to access and read properties. The `get()` method can be passed an optional `boolean` value, which is false by default, to dictate whether the method should make a trip to the client to retrieve the value. This impacts performance, but might be necessary if the property can be modified purely in the client.
47+
In addition to setting a property, use the `get()` method in the `ElementComposite` class to access and read properties. The `get()` method can be passed an optional `boolean` value, which is false by default, to dictate whether the method should make a trip to the client to retrieve the value. This impacts performance, but might be necessary if the property can be modified purely in the client.
4748

4849
A `Type` can also be passed to the method, which dictates what to cast retrieved result to.
4950

5051
:::tip
51-
This `Type` is not overtly necessary, and adds an extra layer of specification as the data is retrieved.
52+
This `Type` isn't overtly necessary, and adds an extra layer of specification as the data is retrieved.
5253
:::
5354

5455
```java
@@ -68,11 +69,13 @@ height='250px'
6869

6970
## Event registration {#event-registration}
7071

71-
Events are a crucial part of web components, allowing communication between different parts of an application. The `ElementComposite` class simplifies event registration and handling. To register an event listener, use the `addEventListener()` method to register event listeners for specific event types. Specify the event class, the listener, and optional event options.
72+
Events enable communication between different parts of your webforJ app. The `ElementComposite` class provides event handling with support for debouncing, throttling, filtering, and custom event data collection.
73+
74+
Register event listeners using the `addEventListener()` method:
7275

7376
```java
7477
// Example: Adding a click event listener
75-
addEventListener(ClickEvent.class, event -> {
78+
addEventListener(ElementClickEvent.class, event -> {
7679
// Handle the click event
7780
});
7881
```
@@ -81,14 +84,84 @@ addEventListener(ClickEvent.class, event -> {
8184
The `ElementComposite` events are different than `Element` events, in that this doesn't allow any class, but only specified `Event` classes.
8285
:::
8386

84-
In the demonstration below, a click event has been created and then added to the QR code component. This event, when fired, will display the "X" coordinate of the mouse at the time of clicking the component, which is provided to the Java event as data. A method is then implemented to allow the user to access this data, which is how it is displayed in the application.
87+
### Built-in event classes {#built-in-event-classes}
88+
89+
webforJ provides pre-built event classes with typed data access:
90+
91+
- **ElementClickEvent**: Mouse click events with coordinates (`getClientX()`, `getClientY()`), button information (`getButton()`), and modifier keys (`isCtrlKey()`, `isShiftKey()`, etc.)
92+
- **ElementDefinedEvent**: Fired when a custom element is defined in the DOM and ready for use
93+
- **ElementEvent**: Base event class providing access to raw event data, event type (`getType()`), and event ID (`getId()`)
94+
95+
### Event payloads {#event-payloads}
96+
97+
Events carry data from the client to your Java code. Access this data through `getData()` for raw event data or use typed methods when available on built-in event classes. For more details on efficiently using event payloads, see the [Events guide](../building-ui/events).
98+
99+
## Custom event classes {#custom-event-classes}
100+
101+
For specialized event handling, create custom event classes with configured payloads using `@EventName` and `@EventOptions` annotations.
102+
103+
In the example below, a click event has been created and then added to the QR code component. This event, when fired, will display the "X" coordinate of the mouse at the time of clicking the component, which is provided to the Java event as data. A method is then implemented to allow the user to access this data, which is how it's displayed in the app.
104+
85105
<ComponentDemo
86106
path='/webforj/qrevent?'
87107
javaE='https://raw.githubusercontent.com/webforj/webforj-documentation/refs/heads/main/src/main/java/com/webforj/samples/views/elementcomposite/QREventView.java'
88108
height='300px'
89109
/>
90110

91-
## Interacting with Slots {#interacting-with-slots}
111+
## `ElementEventOptions` {#elementeventoptions}
112+
113+
`ElementEventOptions` lets you customize event behavior by configuring what data to collect, when events fire, and how they're processed. Here's a comprehensive code snippet showing all the configuration options:
114+
115+
```java
116+
ElementEventOptions options = new ElementEventOptions()
117+
// Collect custom data from the client
118+
.addData("query", "component.value")
119+
.addData("timestamp", "Date.now()")
120+
.addData("isValid", "component.checkValidity()")
121+
122+
// Execute JavaScript before event fires
123+
.setCode("component.classList.add('processing');")
124+
125+
// Only fire if conditions are met
126+
.setFilter("component.value.length >= 2")
127+
128+
// Delay execution until user stops typing (300ms)
129+
.setDebounce(300, DebouncePhase.TRAILING);
130+
131+
addEventListener("input", this::handleSearch, options);
132+
```
133+
134+
### Performance control {#performance-control}
135+
136+
Control when and how often events fire:
137+
138+
**Debouncing** delays execution until activity stops:
139+
140+
```java
141+
options.setDebounce(300, DebouncePhase.TRAILING); // Wait 300ms after last event
142+
```
143+
144+
**Throttling** limits execution frequency:
145+
146+
```java
147+
options.setThrottle(100); // Fire at most once per 100ms
148+
```
149+
150+
Available debounce phases:
151+
152+
- `LEADING`: Fire immediately, then wait
153+
- `TRAILING`: Wait for quiet period, then fire (default)
154+
- `BOTH`: Fire immediately and after quiet period
155+
156+
## Options merging {#options-merging}
157+
158+
Combine event configurations from different sources using `mergeWith()`. Base options provide common data for all events, while specific options add specialized configuration. Later options override conflicting settings.
159+
160+
```java
161+
ElementEventOptions merged = baseOptions.mergeWith(specificOptions);
162+
```
163+
164+
## Interacting with slots {#interacting-with-slots}
92165

93166
Web components often use slots to allow developers to define the structure of a component from the outside. A slot is a placeholder inside a web component that can be filled with content when using the component. In the context of the `ElementComposite` class, slots provide a way to customize the content within a component. The following methods are provided to allow developers to interact with and manipulate slots:
94167

@@ -98,6 +171,6 @@ Web components often use slots to allow developers to define the structure of a
98171

99172
3. **`getFirstComponentInSlot()`**: This method is designed to fetch the first component assigned to the slot. Optionally pass a specific class type to filter the results of this method.
100173

101-
It is also possible to use the `add()` method with a `String` parameter to specify the desired slot in which to add the passed component.
174+
it's also possible to use the `add()` method with a `String` parameter to specify the desired slot in which to add the passed component.
102175

103176
These interactions allow developers to harness the power of web components by providing a clean and straightforward API for manipulating slots, properties, and handling events within the `ElementComposite` class.

0 commit comments

Comments
 (0)