@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
38 lines (37 loc) • 2.02 kB
TypeScript
import type { PropertyProvider } from '../misc/functions';
import type { ESLAttributeDecorator } from '../dom/attr';
export type AttrParser<T> = (attr: string | null) => T;
export type AttrSerializer<T> = (val: T) => null | boolean | string;
/** HTML attribute mapping configuration */
type AttrDescriptor<T = string> = {
/** HTML attribute name. Uses kebab-cased variable name by default */
name?: string;
/** Create getter only */
readonly?: boolean;
/**
* Specifies the attribute inheritance behavior.
* If `inherit` is set to `true`, the attribute will inherit the value from the same-named attribute of the closest parent element in the DOM tree.
* For instance, `@attr({inherit: true}) ignore;` will look for an `ignore` attribute in the parent elements if it's not defined in the current element.
* If `dataAttr` is also true, it will search for `data-ignore` instead.
*
* If `inherit` is set to a string, it will use this string as the attribute name to search for in the parent elements.
* For example, `@attr({inherit: 'alt-ignore'}) ignore;` will first look for its own `ignore` attribute (or 'data-ignore' if `dataAttr` is true),
* and if not found, it will look for an `alt-ignore` attribute in the parent elements.
*/
inherit?: boolean | string;
/** Use data-* attribute */
dataAttr?: boolean;
/** Default property value. Used if no attribute is present on the element. Empty string by default. Supports provider function. */
defaultValue?: T | PropertyProvider<T>;
/** Parser from attribute value */
parser?: AttrParser<T>;
/** Serializer to transform passed value to attribute value */
serializer?: AttrSerializer<T>;
};
/**
* Decorator to map current property to element attribute value.
* Maps string type property.
* @param config - mapping configuration. See {@link AttrDescriptor}
*/
export declare const attr: <T = string>(config?: AttrDescriptor<T>) => ESLAttributeDecorator;
export {};