Composable mixins that provide specific functionality to elements, following a modular architecture pattern. Mixins enable code reuse and feature composition.
Core element functionality including template stamping, data-binding, and lifecycle management.
/**
* Element class mixin that provides the core API for Polymer's meta-programming
* features including template stamping, data-binding, attribute deserialization,
* and property change observation.
*/
function ElementMixin<T extends Constructor<HTMLElement>>(base: T): T & ElementMixinConstructor;
interface ElementMixinConstructor {
new(): ElementMixin;
/** Polymer version */
polymerElementVersion: string;
/** Template for the element */
template: HTMLTemplateElement;
/** Import path for resolving URLs */
importPath: string;
/** Create property effects from property config */
createProperties(props: PropertyDeclarations): void;
/** Create observers from observer array */
createObservers(observers: string[], dynamicFns: object): void;
/** Finalize element class metadata */
finalize(): void;
}
interface ElementMixin {
/** Root element of stamped template */
root: ShadowRoot | HTMLElement;
/** Map of id->element for template elements */
$: {[id: string]: Element};
/** Element's import path */
importPath: string;
/** Application root path */
rootPath: string;
/** Update ShadyCSS styles */
updateStyles(properties?: StyleProperties): void;
/** Resolve URL relative to import path */
resolveUrl(url: string, base?: string): string;
}Usage Examples:
import { ElementMixin } from '@polymer/polymer/lib/mixins/element-mixin.js';
// Creating a custom base class
class MyElementBase extends ElementMixin(HTMLElement) {
constructor() {
super();
console.log('Custom base element created');
}
// Add shared functionality
commonMethod() {
return 'shared behavior';
}
}
// Using the custom base
class MyElement extends MyElementBase {
static get is() { return 'my-element'; }
static get template() {
return html`<div>[[message]]</div>`;
}
ready() {
super.ready();
console.log(this.commonMethod()); // "shared behavior"
}
}Advanced property observation, computed properties, and data binding capabilities.
/**
* Mixin that provides property effects including computed properties,
* observers, and template binding
*/
function PropertyEffects<T extends Constructor<HTMLElement>>(base: T): T & PropertyEffectsConstructor;
interface PropertyEffectsConstructor {
/** Create computed property */
createComputedProperty(property: string, expression: string, dynamicFns: object): void;
/** Create property observer */
createPropertyObserver(property: string, method: string, dynamicFn: boolean): void;
/** Create read-only property */
createReadOnlyProperty(property: string, protectedSetter: boolean): void;
/** Create reflected property */
createReflectedProperty(property: string): void;
/** Create notifying property */
createNotifyingProperty(property: string): void;
}
interface PropertyEffects {
/** Set property value and notify */
_setProperty(property: string, value: any): void;
/** Get property value */
_getProperty(property: string): any;
/** Notify property change */
_notifyPropertyChange(property: string, value: any, oldValue: any): void;
/** Create method observer */
_createMethodObserver(expression: string, dynamicFns: object): void;
}Property configuration and metadata handling.
/**
* Mixin that provides property configuration and flattening
*/
function PropertiesMixin<T extends Constructor<HTMLElement>>(base: T): T & PropertiesMixinConstructor;
interface PropertiesMixinConstructor {
/** Flattened properties from class hierarchy */
_properties: PropertyDeclarations;
/** Finalize property configuration */
finalize(): void;
/** Create properties from configuration */
createProperties(props: PropertyDeclarations): void;
}
interface PropertiesMixin {
/** Initialize properties with defaults */
_initializeProperties(): void;
/** Check if property can have default applied */
_canApplyPropertyDefault(property: string): boolean;
}Template instantiation and DOM stamping functionality.
/**
* Mixin that provides template stamping capabilities
*/
function TemplateStamp<T extends Constructor<HTMLElement>>(base: T): T & TemplateStampConstructor;
interface TemplateStampConstructor {
/** Parse template for binding information */
_parseTemplate(template: HTMLTemplateElement): TemplateInfo;
}
interface TemplateStamp {
/** Stamp template into DocumentFragment */
_stampTemplate(template: HTMLTemplateElement): DocumentFragment;
/** Bind template with data */
_bindTemplate(template: HTMLTemplateElement): void;
}Property getter/setter generation and attribute synchronization.
/**
* Mixin that provides property accessors and attribute sync
*/
function PropertyAccessors<T extends Constructor<HTMLElement>>(base: T): T & PropertyAccessorsConstructor;
interface PropertyAccessorsConstructor {
/** Observed attributes for this element */
observedAttributes: string[];
/** Create property accessor */
createPropertyAccessor(property: string, readOnly?: boolean): void;
}
interface PropertyAccessors {
/** Handle attribute changes */
attributeChangedCallback(name: string, old: string, value: string): void;
/** Deserialize attribute to property */
_deserializeValue(value: string, type: PropertyType): any;
/** Serialize property to attribute */
_serializeValue(value: any): string;
/** Check if element has property accessor */
_hasAccessor(property: string): boolean;
}Gesture-based event handling with cross-platform gesture recognition.
/**
* Mixin that provides gesture event listening capabilities
*/
function GestureEventListeners<T extends Constructor<HTMLElement>>(base: T): T & GestureEventListenersConstructor;
interface GestureEventListeners {
/** Add gesture event listener */
_addEventListenerToNode(node: Node, eventName: string, handler: (e: Event) => void): void;
/** Remove gesture event listener */
_removeEventListenerFromNode(node: Node, eventName: string, handler: (e: Event) => void): void;
}Usage Examples:
import { GestureEventListeners } from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
class GestureElement extends GestureEventListeners(PolymerElement) {
ready() {
super.ready();
// Add gesture listeners
this._addEventListenerToNode(this, 'tap', this._onTap.bind(this));
this._addEventListenerToNode(this, 'track', this._onTrack.bind(this));
}
_onTap(e) {
console.log('Tap gesture detected');
}
_onTrack(e) {
console.log('Track gesture:', e.detail.state, e.detail.dx, e.detail.dy);
}
}Mutable data handling for arrays and objects with change detection.
/**
* Mixin that provides mutable data handling
*/
function MutableData<T extends Constructor<HTMLElement>>(base: T): T & MutableDataConstructor;
/**
* Optional mutable data mixin
*/
function OptionalMutableData<T extends Constructor<HTMLElement>>(base: T): T & OptionalMutableDataConstructor;
interface MutableDataMixin {
/** Force data system to reevaluate paths */
_mutablePropertyChange(path: string, value: any, mutable: boolean): void;
}RTL/LTR direction handling for internationalization.
/**
* Mixin that provides directional layout support
*/
function DirMixin<T extends Constructor<HTMLElement>>(base: T): T & DirMixinConstructor;
interface DirMixin {
/** Current text direction */
dir: string;
}Control element upgrade timing for performance optimization.
/**
* Mixin that allows controlling element upgrade timing
*/
function DisableUpgradeMixin<T extends Constructor<HTMLElement>>(base: T): T & DisableUpgradeMixinConstructor;
interface DisableUpgradeMixin {
/** Disable element upgrade */
_disableUpgrade: boolean;
}
/**
* Find observed attributes getter in constructor chain
*/
function findObservedAttributesGetter(constructor: Constructor): () => string[];Strict template binding parsing for enhanced security.
/**
* Mixin that provides strict binding parsing
*/
function StrictBindingParser<T extends Constructor<HTMLElement>>(base: T): T & StrictBindingParserConstructor;Utility for creating deduplicating mixins.
/**
* Wraps a mixin function to ensure it's only applied once per class hierarchy
*/
function dedupingMixin<T extends Constructor, U>(mixinFunction: (base: T) => U): (base: T) => U;Usage Examples:
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
// Create a deduplicating mixin
const MyMixin = dedupingMixin((base) => {
class MyMixinImpl extends base {
myMixinMethod() {
return 'mixin functionality';
}
}
return MyMixinImpl;
});
// Safe to apply multiple times - only applied once
class ElementA extends MyMixin(PolymerElement) {}
class ElementB extends MyMixin(MyMixin(PolymerElement)) {} // MyMixin only applied oncetype Constructor<T = {}> = new (...args: any[]) => T;
interface PropertyDeclarations {
[property: string]: PropertyType | PropertyDeclaration;
}
interface TemplateInfo {
dynamicFns?: {[property: string]: boolean};
}
interface StyleProperties {
[property: string]: string;
}