🇧🇷 Leia em Português | 🇺🇸 Read in English
The stop
filter is a function that allows you to stop the propagation of an event in the DOM, preventing it from being transmitted to other elements that may be listening for the same event.
- Event Interruption: Ideal in situations where you want to prevent events from being processed in ancestor or sibling elements, such as avoiding unwanted side effects.
- Complex Events: Useful in event handlers that need to isolate the action of a specific event, ensuring that the logic does not affect other components.
The stop
function uses the stopPropagation
method of the Event
object to prevent the event from continuing its propagation to other elements in the DOM. After stopping the propagation, the function returns the event itself, allowing you to perform other operations with it.
/**
* @param {Event} event - The event to be filtered.
* @returns {Event} The event itself, after stopping propagation.
*/
function stop(event) {
event.stopPropagation();
return event;
}
- event (required):
- Type:
Event
- Description: The event to be filtered, usually a user interaction event, such as a click or mouse movement.
- Type:
- Type:
Event
- Description: The event itself, after stopping propagation, allowing you to perform additional operations with the modified event.
-
Import the
stop
filter:import { stop } from '@bake-js/-o-id/event';
-
Use the filter in an event handler:
- Step 1: Capture the event you want to manipulate.
- Step 2: Call the
stop
function, passing the event as an argument.
This example demonstrates how to use the stop
filter in conjunction with the on
decorator to stop the propagation of a click event on a button within a custom component.
import { define } from '@bake-js/-o-id';
import on, { stop } from '@bake-js/-o-id/event';
@define('my-component')
class MyComponent extends HTMLElement {
@on.click('button', stop)
handleClick(event) {
console.log('Button clicked!'); // Message displayed in the console
// The event propagation is stopped here
}
connectedCallback() {
this.innerHTML = `
<div>
<button>Click Me!</button>
</div>
`;
}
}
-
Module Imports:
- The component imports the
define
decorator to register the Custom Element. - Imports
on
andstop
from the event module.
- The component imports the
-
Component Definition:
- The
my-component
component is defined using the@define
decorator.
- The
-
Handling Button Click:
- The
handleClick
method is decorated with@on.click
, which listens for the click event on the button. - The
stop
filter is applied to stop the event propagation, ensuring it does not reach ancestor elements.
- The
-
Button Rendering:
- In the
connectedCallback
method, the HTML for the button is inserted into the component.
- In the
- When the "Click Me!" button is clicked, the message "Button clicked!" is displayed in the console.
- The propagation of the click event is stopped, preventing the event from being processed by other event handlers that may be associated with ancestor elements.
When clicking the button, the message "Button clicked!" will be displayed in the console, and the propagation of the event will be stopped, preventing further actions on parent elements.
- Total Control: Allows you to control event propagation, avoiding unwanted side effects in your application.
- Integration with Decorators: Using the
stop
filter in combination with decorators provides a declarative approach to event handling, resulting in cleaner and more organized code.
This example demonstrates the utility of the stop
filter in web component development, providing an effective way to manage event propagation and ensuring that your application logic remains isolated and controlled.