2018-02-28 17:45:11 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2018-02-28 17:45:11 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2018-02-28 17:45:11 +00:00
|
|
|
*/
|
|
|
|
|
|
2018-03-06 22:02:25 +00:00
|
|
|
import {Injector, Type} from '@angular/core';
|
2018-02-27 22:06:06 +00:00
|
|
|
import {Subscription} from 'rxjs';
|
2018-02-28 17:45:11 +00:00
|
|
|
|
2018-03-06 22:02:25 +00:00
|
|
|
import {ComponentNgElementStrategyFactory} from './component-factory-strategy';
|
2018-02-28 17:45:11 +00:00
|
|
|
import {NgElementStrategy, NgElementStrategyFactory} from './element-strategy';
|
2022-01-12 21:37:37 +00:00
|
|
|
import {getComponentInputs, getDefaultAttributeToPropertyInputs} from './utils';
|
2018-02-28 17:45:11 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-02-27 21:33:41 +00:00
|
|
|
* Prototype for a class constructor based on an Angular component
|
|
|
|
|
* that can be used for custom element registration. Implemented and returned
|
|
|
|
|
* by the {@link createCustomElement createCustomElement() function}.
|
2018-02-28 17:45:11 +00:00
|
|
|
*
|
2020-07-27 19:46:57 +00:00
|
|
|
* @see [Angular Elements Overview](guide/elements "Turning Angular components into custom elements")
|
|
|
|
|
*
|
2018-10-19 11:12:20 +00:00
|
|
|
* @publicApi
|
2018-02-28 17:45:11 +00:00
|
|
|
*/
|
|
|
|
|
export interface NgElementConstructor<P> {
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* An array of observed attribute names for the custom element,
|
|
|
|
|
* derived by transforming input property names from the source component.
|
|
|
|
|
*/
|
2018-02-28 17:45:11 +00:00
|
|
|
readonly observedAttributes: string[];
|
|
|
|
|
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Initializes a constructor instance.
|
2020-03-05 01:27:27 +00:00
|
|
|
* @param injector If provided, overrides the configured injector.
|
2018-02-27 21:33:41 +00:00
|
|
|
*/
|
2020-04-13 23:40:21 +00:00
|
|
|
new (injector?: Injector): NgElement & WithProperties<P>;
|
2018-02-28 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-27 21:33:41 +00:00
|
|
|
* Implements the functionality needed for a custom element.
|
2018-02-28 17:45:11 +00:00
|
|
|
*
|
2018-10-19 11:12:20 +00:00
|
|
|
* @publicApi
|
2018-02-28 17:45:11 +00:00
|
|
|
*/
|
|
|
|
|
export abstract class NgElement extends HTMLElement {
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* The strategy that controls how a component is transformed in a custom element.
|
|
|
|
|
*/
|
2020-07-15 10:21:04 +00:00
|
|
|
protected abstract ngElementStrategy: NgElementStrategy;
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* A subscription to change, connect, and disconnect events in the custom element.
|
|
|
|
|
*/
|
2018-02-28 17:45:11 +00:00
|
|
|
protected ngElementEventsSubscription: Subscription | null = null;
|
|
|
|
|
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
2020-04-13 23:40:21 +00:00
|
|
|
* Prototype for a handler that responds to a change in an observed attribute.
|
|
|
|
|
* @param attrName The name of the attribute that has changed.
|
|
|
|
|
* @param oldValue The previous value of the attribute.
|
|
|
|
|
* @param newValue The new value of the attribute.
|
|
|
|
|
* @param namespace The namespace in which the attribute is defined.
|
|
|
|
|
* @returns Nothing.
|
|
|
|
|
*/
|
2018-02-28 17:45:11 +00:00
|
|
|
abstract attributeChangedCallback(
|
|
|
|
|
attrName: string,
|
|
|
|
|
oldValue: string | null,
|
|
|
|
|
newValue: string,
|
|
|
|
|
namespace?: string,
|
|
|
|
|
): void;
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Prototype for a handler that responds to the insertion of the custom element in the DOM.
|
|
|
|
|
* @returns Nothing.
|
|
|
|
|
*/
|
2018-02-28 17:45:11 +00:00
|
|
|
abstract connectedCallback(): void;
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Prototype for a handler that responds to the deletion of the custom element from the DOM.
|
|
|
|
|
* @returns Nothing.
|
|
|
|
|
*/
|
2018-02-28 17:45:11 +00:00
|
|
|
abstract disconnectedCallback(): void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-27 21:33:41 +00:00
|
|
|
* Additional type information that can be added to the NgElement class,
|
|
|
|
|
* for properties that are added based
|
2018-02-28 17:45:11 +00:00
|
|
|
* on the inputs and methods of the underlying component.
|
2018-03-02 18:08:16 +00:00
|
|
|
*
|
2018-10-19 11:12:20 +00:00
|
|
|
* @publicApi
|
2018-02-28 17:45:11 +00:00
|
|
|
*/
|
|
|
|
|
export type WithProperties<P> = {
|
|
|
|
|
[property in keyof P]: P[property];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-27 21:33:41 +00:00
|
|
|
* A configuration that initializes an NgElementConstructor with the
|
|
|
|
|
* dependencies and strategy it needs to transform a component into
|
|
|
|
|
* a custom element class.
|
2018-02-28 17:45:11 +00:00
|
|
|
*
|
2018-10-19 11:12:20 +00:00
|
|
|
* @publicApi
|
2018-02-28 17:45:11 +00:00
|
|
|
*/
|
|
|
|
|
export interface NgElementConfig {
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* The injector to use for retrieving the component's factory.
|
|
|
|
|
*/
|
2018-03-02 06:34:21 +00:00
|
|
|
injector: Injector;
|
2018-02-27 21:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* An optional custom strategy factory to use instead of the default.
|
2018-08-21 18:30:10 +00:00
|
|
|
* The strategy controls how the transformation is performed.
|
2018-02-27 21:33:41 +00:00
|
|
|
*/
|
2018-03-02 06:34:21 +00:00
|
|
|
strategyFactory?: NgElementStrategyFactory;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 17:45:11 +00:00
|
|
|
/**
|
2018-02-27 21:33:41 +00:00
|
|
|
* @description Creates a custom element class based on an Angular component.
|
2018-02-28 17:45:11 +00:00
|
|
|
*
|
2018-02-27 21:33:41 +00:00
|
|
|
* Builds a class that encapsulates the functionality of the provided component and
|
|
|
|
|
* uses the configuration information to provide more context to the class.
|
|
|
|
|
* Takes the component factory's inputs and outputs to convert them to the proper
|
|
|
|
|
* custom element API and add hooks to input changes.
|
|
|
|
|
*
|
|
|
|
|
* The configuration's injector is the initial injector set on the class,
|
|
|
|
|
* and used by default for each created instance.This behavior can be overridden with the
|
2018-02-28 17:45:11 +00:00
|
|
|
* static property to affect all newly created instances, or as a constructor argument for
|
2018-02-27 21:33:41 +00:00
|
|
|
* one-off creations.
|
|
|
|
|
*
|
2020-07-27 19:46:57 +00:00
|
|
|
* @see [Angular Elements Overview](guide/elements "Turning Angular components into custom elements")
|
|
|
|
|
*
|
2018-02-27 21:33:41 +00:00
|
|
|
* @param component The component to transform.
|
|
|
|
|
* @param config A configuration that provides initialization information to the created class.
|
|
|
|
|
* @returns The custom-element construction class, which can be registered with
|
|
|
|
|
* a browser's `CustomElementRegistry`.
|
2018-02-28 17:45:11 +00:00
|
|
|
*
|
2018-10-19 11:12:20 +00:00
|
|
|
* @publicApi
|
2018-02-28 17:45:11 +00:00
|
|
|
*/
|
2018-03-14 20:24:27 +00:00
|
|
|
export function createCustomElement<P>(
|
2018-03-02 06:34:21 +00:00
|
|
|
component: Type<any>,
|
|
|
|
|
config: NgElementConfig,
|
|
|
|
|
): NgElementConstructor<P> {
|
2018-03-06 22:02:25 +00:00
|
|
|
const inputs = getComponentInputs(component, config.injector);
|
2018-03-02 06:34:21 +00:00
|
|
|
|
2018-03-06 22:02:25 +00:00
|
|
|
const strategyFactory =
|
|
|
|
|
config.strategyFactory || new ComponentNgElementStrategyFactory(component, config.injector);
|
2018-03-02 06:34:21 +00:00
|
|
|
|
2018-03-16 00:18:40 +00:00
|
|
|
const attributeToPropertyInputs = getDefaultAttributeToPropertyInputs(inputs);
|
2018-03-02 06:34:21 +00:00
|
|
|
|
2018-02-28 17:45:11 +00:00
|
|
|
class NgElementImpl extends NgElement {
|
2018-05-11 02:35:19 +00:00
|
|
|
// Work around a bug in closure typed optimizations(b/79557487) where it is not honoring static
|
|
|
|
|
// field externs. So using quoted access to explicitly prevent renaming.
|
|
|
|
|
static readonly ['observedAttributes'] = Object.keys(attributeToPropertyInputs);
|
2018-02-28 17:45:11 +00:00
|
|
|
|
2021-07-07 17:58:22 +00:00
|
|
|
protected override get ngElementStrategy(): NgElementStrategy {
|
2018-05-10 20:34:09 +00:00
|
|
|
// TODO(andrewseguin): Add e2e tests that cover cases where the constructor isn't called. For
|
|
|
|
|
// now this is tested using a Google internal test suite.
|
2020-05-20 08:10:27 +00:00
|
|
|
if (!this._ngElementStrategy) {
|
|
|
|
|
const strategy = (this._ngElementStrategy = strategyFactory.create(
|
|
|
|
|
this.injector || config.injector,
|
|
|
|
|
));
|
2020-05-20 08:10:26 +00:00
|
|
|
|
2020-10-14 13:57:00 +00:00
|
|
|
// Re-apply pre-existing input values (set as properties on the element) through the
|
|
|
|
|
// strategy.
|
fix(elements): switch to `ComponentRef.setInput` & remove custom scheduler (#56728)
The custom element implementation previously used a custom code path for
setting inputs, which contained bespoke code for writing input properties,
detecting whether inputs actually change, marking the component dirty,
scheduling and running CD, invoking `ngOnChanges`, etc. This custom logic
had several downsides:
* Its behavior different from how Angular components behave in a normal
template.
For example, inputs setters were invoked in `NgZone.run`, which (when
called from outside the zone) would trigger synchronous CD in the
component, _without_ calling `ngOnChanges`. Only when the custom rAF-
scheduled `detectChanges()` call triggered would `ngOnChanges` be called.
* CD always ran multiple times, because of the above. `NgZone.run` would
trigger CD, and then separately the scheduler would trigger CD.
* Signal inputs were not supported, since inputs were set via direct
property writes.
This change refactors the custom element implementation with two changes:
1. `ComponentRef.setInput` is used instead of a custom code path for
writing inputs.
This allows us to drop all the custom logic related to managing
`ngOnChanges`, since `setInput` does that under the hood. `ngOnChanges`
behavior now matches how the component would behave when _not_ rendered
as a custom element.
2. The custom rAF-based CD scheduler is removed in favor of the main Angular
scheduler, which now handles custom elements as necessary.
Running `NgZone.run` is sufficient to trigger CD when zones are used, and
the hybrid zoneless scheduler now ensures CD is scheduled when `setInput` is
called even with no ZoneJS enabled. As a result, the dedicated elements
scheduler is now only used when Angular's built-in scheduler is disabled.
As a concession to backwards compatibility, the element's view is also
marked for refresh when an input changes. Doing this allows CD to revisit
the element even if it becomes dirty during CD, mimicking how it would be
detected by the former elements scheduler unconditionally refreshing the
view a second time.
As a part of this change, the elements tests have been significantly
refactored. Previously all of Angular was faked/spied, which had a number
of downsides. For example, there were tests which asserted that change
detection only happens once when setting multiple inputs. This wasn't
actually the case (because of `NgZone.run` - see logic above) but the test
didn't catch the issue because it was only spying on `detectChanges()` which
isn't called from `ApplicationRef.tick()`. Even the components were fake.
Now, the tests use real Angular components and factories. They've also been
updated to not use `fakeAsync`.
A number of tests have been disabled, which were previously asserting
behavior that wasn't matching what was actually happening (as above). Other
tests were disabled due to real differences with `ngOnChanges` behavior,
where the current behavior could be seen as a bug.
Fixes #53981
BREAKING CHANGE: as part of switching away from custom CD behavior to the
hybrid scheduler, timing of change detection around custom elements has
changed subtly. These changes make elements more efficient, but can cause
tests which encoded assumptions about how or when elements would be checked
to require updating.
PR Close #56728
2024-06-26 23:09:59 +00:00
|
|
|
// TODO(alxhub): why are we doing this? this makes no sense.
|
2025-01-29 08:49:39 +00:00
|
|
|
inputs.forEach(({propName, transform}) => {
|
|
|
|
|
if (!this.hasOwnProperty(propName)) {
|
|
|
|
|
// No pre-existing value for `propName`.
|
2020-10-14 13:57:00 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 08:49:39 +00:00
|
|
|
// Delete the property from the DOM node and re-apply it through the strategy.
|
2020-10-14 13:57:00 +00:00
|
|
|
const value = (this as any)[propName];
|
|
|
|
|
delete (this as any)[propName];
|
2023-06-14 11:05:27 +00:00
|
|
|
strategy.setInputValue(propName, value, transform);
|
2020-10-14 13:57:00 +00:00
|
|
|
});
|
2020-05-20 08:10:22 +00:00
|
|
|
}
|
2020-05-20 08:10:26 +00:00
|
|
|
|
|
|
|
|
return this._ngElementStrategy!;
|
2020-05-20 08:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-20 08:10:27 +00:00
|
|
|
private _ngElementStrategy?: NgElementStrategy;
|
2020-05-20 08:10:22 +00:00
|
|
|
|
2020-05-20 08:10:27 +00:00
|
|
|
constructor(private readonly injector?: Injector) {
|
2020-05-20 08:10:22 +00:00
|
|
|
super();
|
2018-02-28 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-07 17:58:22 +00:00
|
|
|
override attributeChangedCallback(
|
2018-02-28 17:45:11 +00:00
|
|
|
attrName: string,
|
|
|
|
|
oldValue: string | null,
|
|
|
|
|
newValue: string,
|
|
|
|
|
namespace?: string,
|
|
|
|
|
): void {
|
2023-06-14 11:05:27 +00:00
|
|
|
const [propName, transform] = attributeToPropertyInputs[attrName]!;
|
|
|
|
|
this.ngElementStrategy.setInputValue(propName, newValue, transform);
|
2018-02-28 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-07 17:58:22 +00:00
|
|
|
override connectedCallback(): void {
|
2020-06-13 08:06:35 +00:00
|
|
|
// For historical reasons, some strategies may not have initialized the `events` property
|
|
|
|
|
// until after `connect()` is run. Subscribe to `events` if it is available before running
|
2021-10-18 15:21:58 +00:00
|
|
|
// `connect()` (in order to capture events emitted during initialization), otherwise subscribe
|
|
|
|
|
// afterwards.
|
2020-06-13 08:06:35 +00:00
|
|
|
//
|
|
|
|
|
// TODO: Consider deprecating/removing the post-connect subscription in a future major version
|
|
|
|
|
// (e.g. v11).
|
|
|
|
|
|
|
|
|
|
let subscribedToEvents = false;
|
|
|
|
|
|
|
|
|
|
if (this.ngElementStrategy.events) {
|
|
|
|
|
// `events` are already available: Subscribe to it asap.
|
|
|
|
|
this.subscribeToEvents();
|
|
|
|
|
subscribedToEvents = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 15:31:08 +00:00
|
|
|
this.ngElementStrategy.connect(this);
|
|
|
|
|
|
2020-06-13 08:06:35 +00:00
|
|
|
if (!subscribedToEvents) {
|
|
|
|
|
// `events` were not initialized before running `connect()`: Subscribe to them now.
|
|
|
|
|
// The events emitted during the component initialization have been missed, but at least
|
|
|
|
|
// future events will be captured.
|
|
|
|
|
this.subscribeToEvents();
|
|
|
|
|
}
|
2018-02-28 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-07 17:58:22 +00:00
|
|
|
override disconnectedCallback(): void {
|
2020-05-20 08:10:22 +00:00
|
|
|
// Not using `this.ngElementStrategy` to avoid unnecessarily creating the `NgElementStrategy`.
|
|
|
|
|
if (this._ngElementStrategy) {
|
|
|
|
|
this._ngElementStrategy.disconnect();
|
2018-05-10 20:34:09 +00:00
|
|
|
}
|
2018-02-28 17:45:11 +00:00
|
|
|
|
|
|
|
|
if (this.ngElementEventsSubscription) {
|
|
|
|
|
this.ngElementEventsSubscription.unsubscribe();
|
|
|
|
|
this.ngElementEventsSubscription = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-13 08:06:35 +00:00
|
|
|
|
|
|
|
|
private subscribeToEvents(): void {
|
|
|
|
|
// Listen for events from the strategy and dispatch them as custom events.
|
|
|
|
|
this.ngElementEventsSubscription = this.ngElementStrategy.events.subscribe((e) => {
|
2022-01-12 21:37:37 +00:00
|
|
|
const customEvent = new CustomEvent(e.name, {detail: e.value});
|
2020-06-13 08:06:35 +00:00
|
|
|
this.dispatchEvent(customEvent);
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-02-28 17:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-20 08:10:26 +00:00
|
|
|
// Add getters and setters to the prototype for each property input.
|
2023-06-14 11:05:27 +00:00
|
|
|
inputs.forEach(({propName, transform}) => {
|
2020-10-14 13:57:00 +00:00
|
|
|
Object.defineProperty(NgElementImpl.prototype, propName, {
|
2020-05-20 08:10:26 +00:00
|
|
|
get(): any {
|
|
|
|
|
return this.ngElementStrategy.getInputValue(propName);
|
|
|
|
|
},
|
|
|
|
|
set(newValue: any): void {
|
2023-06-14 11:05:27 +00:00
|
|
|
this.ngElementStrategy.setInputValue(propName, newValue, transform);
|
2020-05-20 08:10:26 +00:00
|
|
|
},
|
2018-02-28 17:45:11 +00:00
|
|
|
configurable: true,
|
|
|
|
|
enumerable: true,
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-10-14 13:57:00 +00:00
|
|
|
|
|
|
|
|
return NgElementImpl as any as NgElementConstructor<P>;
|
2018-08-21 18:30:10 +00:00
|
|
|
}
|