@polymer/lit-element
Version:
Polymer based lit-html custom element
320 lines • 13.6 kB
TypeScript
/**
* @license
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
/**
* Converts property values to and from attribute values.
*/
export interface ComplexAttributeConverter<Type = any, TypeHint = any> {
/**
* Function called to convert an attribute value to a property
* value.
*/
fromAttribute?(value: string, type?: TypeHint): Type;
/**
* Function called to convert a property value to an attribute
* value.
*/
toAttribute?(value: Type, type?: TypeHint): string | null;
}
declare type AttributeConverter<Type = any, TypeHint = any> = ComplexAttributeConverter<Type> | ((value: string, type?: TypeHint) => Type);
/**
* Defines options for a property accessor.
*/
export interface PropertyDeclaration<Type = any, TypeHint = any> {
/**
* Indicates how and whether the property becomes an observed attribute.
* If the value is `false`, the property is not added to `observedAttributes`.
* If true or absent, the lowercased property name is observed (e.g. `fooBar`
* becomes `foobar`). If a string, the string value is observed (e.g
* `attribute: 'foo-bar'`).
*/
attribute?: boolean | string;
/**
* Indicates the type of the property. This is used only as a hint for the
* `converter` to determine how to convert the attribute
* to/from a property.
*/
type?: TypeHint;
/**
* Indicates how to convert the attribute to/from a property. If this value
* is a function, it is used to convert the attribute value a the property
* value. If it's an object, it can have keys for `fromAttribute` and
* `toAttribute`. If no `toAttribute` function is provided and
* `reflect` is set to `true`, the property value is set directly to the
* attribute. A default `converter` is used if none is provided; it supports
* `Boolean`, `String`, `Number`, `Object`, and `Array`. Note,
* when a property changes and the converter is used to update the attribute,
* the property is never updated again as a result of the attribute changing,
* and vice versa.
*/
converter?: AttributeConverter<Type, TypeHint>;
/**
* Indicates if the property should reflect to an attribute.
* If `true`, when the property is set, the attribute is set using the
* attribute name determined according to the rules for the `attribute`
* property option and the value of the property converted using the rules
* from the `converter` property option.
*/
reflect?: boolean;
/**
* A function that indicates if a property should be considered changed when
* it is set. The function should take the `newValue` and `oldValue` and
* return `true` if an update should be requested.
*/
hasChanged?(value: Type, oldValue: Type): boolean;
/**
* Indicates whether an accessor will be created for this property. By
* default, an accessor will be generated for this property that requests an
* update when set. If this flag is `true`, no accessor will be created, and
* it will be the user's responsibility to call
* `this.requestUpdate(propertyName, oldValue)` to request an update when
* the property changes.
*/
noAccessor?: boolean;
}
/**
* Map of properties to PropertyDeclaration options. For each property an
* accessor is made, and the property is processed according to the
* PropertyDeclaration options.
*/
export interface PropertyDeclarations {
[key: string]: PropertyDeclaration;
}
export declare type PropertyValues = Map<PropertyKey, unknown>;
export declare const defaultConverter: ComplexAttributeConverter;
export interface HasChanged {
(value: unknown, old: unknown): boolean;
}
/**
* Change function that returns true if `value` is different from `oldValue`.
* This method is used as the default for a property's `hasChanged` function.
*/
export declare const notEqual: HasChanged;
/**
* Base element class which manages element properties and attributes. When
* properties change, the `update` method is asynchronously called. This method
* should be supplied by subclassers to render updates as desired.
*/
export declare abstract class UpdatingElement extends HTMLElement {
/**
* Maps attribute names to properties; for example `foobar` attribute to
* `fooBar` property. Created lazily on user subclasses when finalizing the
* class.
*/
private static _attributeToPropertyMap;
/**
* Marks class as having finished creating properties.
*/
protected static finalized: boolean;
/**
* Memoized list of all class properties, including any superclass properties.
* Created lazily on user subclasses when finalizing the class.
*/
private static _classProperties?;
/**
* User-supplied object that maps property names to `PropertyDeclaration`
* objects containing options for configuring the property.
*/
static properties: PropertyDeclarations;
/**
* Returns a list of attributes corresponding to the registered properties.
* @nocollapse
*/
static readonly observedAttributes: string[];
/**
* Ensures the private `_classProperties` property metadata is created.
* In addition to `_finalize` this is also called in `createProperty` to
* ensure the `@property` decorator can add property metadata.
*/
/** @nocollapse */
private static _ensureClassProperties;
/**
* Creates a property accessor on the element prototype if one does not exist.
* The property setter calls the property's `hasChanged` property option
* or uses a strict identity check to determine whether or not to request
* an update.
* @nocollapse
*/
static createProperty(name: PropertyKey, options?: PropertyDeclaration): void;
/**
* Creates property accessors for registered properties and ensures
* any superclasses are also finalized.
* @nocollapse
*/
private static _finalize;
/**
* Returns the property name for the given attribute `name`.
* @nocollapse
*/
private static _attributeNameForProperty;
/**
* Returns true if a property should request an update.
* Called when a property value is set and uses the `hasChanged`
* option for the property if present or a strict identity check.
* @nocollapse
*/
private static _valueHasChanged;
/**
* Returns the property value for the given attribute value.
* Called via the `attributeChangedCallback` and uses the property's
* `converter` or `converter.fromAttribute` property option.
* @nocollapse
*/
private static _propertyValueFromAttribute;
/**
* Returns the attribute value for the given property value. If this
* returns undefined, the property will *not* be reflected to an attribute.
* If this returns null, the attribute will be removed, otherwise the
* attribute will be set to the value.
* This uses the property's `reflect` and `type.toAttribute` property options.
* @nocollapse
*/
private static _propertyValueToAttribute;
private _updateState;
private _instanceProperties;
private _updatePromise;
private _hasConnectedResolver;
/**
* Map with keys for any properties that have changed since the last
* update cycle with previous values.
*/
private _changedProperties;
/**
* Map with keys of properties that should be reflected when updated.
*/
private _reflectingProperties;
constructor();
/**
* Performs element initialization. By default captures any pre-set values for
* registered properties.
*/
protected initialize(): void;
/**
* Fixes any properties set on the instance before upgrade time.
* Otherwise these would shadow the accessor and break these properties.
* The properties are stored in a Map which is played back after the
* constructor runs. Note, on very old versions of Safari (<=9) or Chrome
* (<=41), properties created for native platform properties like (`id` or
* `name`) may not have default values set in the element constructor. On
* these browsers native properties appear on instances and therefore their
* default value will overwrite any element default (e.g. if the element sets
* this.id = 'id' in the constructor, the 'id' will become '' since this is
* the native platform default).
*/
private _saveInstanceProperties;
/**
* Applies previously saved instance properties.
*/
private _applyInstanceProperties;
connectedCallback(): void;
/**
* Allows for `super.disconnectedCallback()` in extensions while
* reserving the possibility of making non-breaking feature additions
* when disconnecting at some point in the future.
*/
disconnectedCallback(): void;
/**
* Synchronizes property values when attributes change.
*/
attributeChangedCallback(name: string, old: string, value: string): void;
private _propertyToAttribute;
private _attributeToProperty;
/**
* Requests an update which is processed asynchronously. This should
* be called when an element should update based on some state not triggered
* by setting a property. In this case, pass no arguments. It should also be
* called when manually implementing a property setter. In this case, pass the
* property `name` and `oldValue` to ensure that any configured property
* options are honored. Returns the `updateComplete` Promise which is resolved
* when the update completes.
*
* @param name {PropertyKey} (optional) name of requesting property
* @param oldValue {any} (optional) old value of requesting property
* @returns {Promise} A Promise that is resolved when the update completes.
*/
requestUpdate(name?: PropertyKey, oldValue?: any): Promise<unknown>;
/**
* Sets up the element to asynchronously update.
*/
private _enqueueUpdate;
private readonly _hasConnected;
private readonly _hasRequestedUpdate;
protected readonly hasUpdated: number;
/**
* Performs an element update.
*
* You can override this method to change the timing of updates. For instance,
* to schedule updates to occur just before the next frame:
*
* ```
* protected async performUpdate(): Promise<unknown> {
* await new Promise((resolve) => requestAnimationFrame(() => resolve()));
* super.performUpdate();
* }
* ```
*/
protected performUpdate(): void | Promise<unknown>;
private _markUpdated;
/**
* Returns a Promise that resolves when the element has completed updating.
* The Promise value is a boolean that is `true` if the element completed the
* update without triggering another update. The Promise result is `false` if
* a property was set inside `updated()`. This getter can be implemented to
* await additional state. For example, it is sometimes useful to await a
* rendered element before fulfilling this Promise. To do this, first await
* `super.updateComplete` then any subsequent state.
*
* @returns {Promise} The Promise returns a boolean that indicates if the
* update resolved without triggering another update.
*/
readonly updateComplete: Promise<unknown>;
/**
* Controls whether or not `update` should be called when the element requests
* an update. By default, this method always returns `true`, but this can be
* customized to control when to update.
*
* * @param _changedProperties Map of changed properties with old values
*/
protected shouldUpdate(_changedProperties: PropertyValues): boolean;
/**
* Updates the element. This method reflects property values to attributes.
* It can be overridden to render and keep updated element DOM.
* Setting properties inside this method will *not* trigger
* another update.
*
* * @param _changedProperties Map of changed properties with old values
*/
protected update(_changedProperties: PropertyValues): void;
/**
* Invoked whenever the element is updated. Implement to perform
* post-updating tasks via DOM APIs, for example, focusing an element.
*
* Setting properties inside this method will trigger the element to update
* again after this update cycle completes.
*
* * @param _changedProperties Map of changed properties with old values
*/
protected updated(_changedProperties: PropertyValues): void;
/**
* Invoked when the element is first updated. Implement to perform one time
* work on the element after update.
*
* Setting properties inside this method will trigger the element to update
* again after this update cycle completes.
*
* * @param _changedProperties Map of changed properties with old values
*/
protected firstUpdated(_changedProperties: PropertyValues): void;
}
export {};
//# sourceMappingURL=updating-element.d.ts.map