mirror of
https://github.com/material-components/material-web.git
synced 2024-10-27 14:07:17 +03:00
41d41cc278
PiperOrigin-RevId: 455635969
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2021 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Re-dispatches an event from the provided element.
|
|
*
|
|
* This function is useful for forwarding non-composed events, such as `change`
|
|
* events.
|
|
*
|
|
* @example
|
|
* class MyInput extends LitElement {
|
|
* render() {
|
|
* return html`<input @change=${this.redispatchEvent}>`;
|
|
* }
|
|
*
|
|
* protected redispatchEvent(event: Event) {
|
|
* redispatchEvent(this, event);
|
|
* }
|
|
* }
|
|
*
|
|
* @param element The element to dispatch the event from.
|
|
* @param event The event to re-dispatch.
|
|
* @return Whether or not the event was dispatched (if cancelable).
|
|
*/
|
|
export function redispatchEvent(element: Element, event: Event) {
|
|
// For bubbling events in SSR light DOM (or composed), stop their propagation
|
|
// and dispatch the copy.
|
|
if (event.bubbles && (!element.shadowRoot || event.composed)) {
|
|
event.stopPropagation();
|
|
}
|
|
|
|
const copy = Reflect.construct(event.constructor, [event.type, event]);
|
|
const dispatched = element.dispatchEvent(copy);
|
|
if (!dispatched) {
|
|
event.preventDefault();
|
|
}
|
|
|
|
return dispatched;
|
|
}
|