一键导入
attribute
@attribute decorator for defining HTML attribute-mapped properties. Invoke when creating or updating component attributes that map to HTML attributes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
@attribute decorator for defining HTML attribute-mapped properties. Invoke when creating or updating component attributes that map to HTML attributes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
@property decorator for defining pure JavaScript properties that do NOT map to HTML attributes. Invoke when creating internal component state properties.
BEM SCSS mixins for component styling. Invoke when writing or updating component SCSS styles using block/element/modifier/state mixins.
Component CSS styling standards including variables, host styles, states, and responsive patterns. Invoke when creating or updating component SCSS files.
@CustomElement decorator for registering Web Components. Invoke when creating new components or modifying component registration options.
Component documentation generation. Invoke when writing or updating component documentation including API tables, examples, and usage guides.
Event system for Web Components including emit() and custom event classes. Invoke when creating or updating component events.
| name | attribute |
| description | @attribute decorator for defining HTML attribute-mapped properties. Invoke when creating or updating component attributes that map to HTML attributes. |
定义映射到 HTML attribute 的响应式属性。
import { CustomElement, attribute } from "@decorator";
@attribute(options: AttributeOptions)
propertyName: type = defaultValue;
| 属性 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | StringConstructor | NumberConstructor | BooleanConstructor | DateConstructor | EnumConstructor | object | 是 | 属性类型 |
default | any | 否 | 默认值 |
observer | (this: Component, newVal: any, oldVal: any) => void | 否 | 属性变化回调 |
a11y | A11yOption | 否 | 无障碍属性同步配置 |
| type 值 | 说明 | HTML 行为 |
|---|---|---|
String | 字符串 | 直接映射 |
Number | 数字 | 自动解析为数字 |
Boolean | 布尔 | 属性存在即为 true |
Array | JSON 数组 | JSON.parse 解析 |
Object | JSON 对象 | JSON.parse 解析 |
Enum([...]) | 枚举 | 限制可选值 |
Date | 日期 | 自动解析为 Date |
@attribute({ type: String, default: "" })
heading: string = "";
@attribute({ type: Number, default: 0 })
count: number = 0;
@attribute({ type: Boolean, default: false })
disabled: boolean = false;
@attribute({
type: String,
default: "",
observer(this: EaAlert, newVal: string) {
this._container.innerHTML = html(newVal);
},
})
content: string = "";
import { Enum } from "@utils/Enum";
@attribute({
type: Enum(["small", "medium", "large"]),
default: "medium",
observer(this: EaComponent) {
this.updateContainerClasslist();
},
})
size: string = "medium";
import { Enum } from "@utils/Enum";
import { VARIANT_TYPES, VARIANT_DEFAULT, type VariantType } from "@constants/variant";
@attribute({
type: Enum(VARIANT_TYPES),
default: VARIANT_DEFAULT,
observer(this: EaAlert, newVal: VariantType) {
this.updateContainerClasslist();
},
})
variant: VariantType = VARIANT_DEFAULT;
当属性变化时需要同步更新 ARIA 属性(如 aria-disabled、aria-expanded、aria-checked)或 HTML inert 属性时,必须使用 a11y 选项,禁止在 observer 中手动 setAttribute/removeAttribute。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
ariaAttr | string | 是 | 同步到的 ARIA 属性名(如 "aria-disabled"、"aria-expanded"、"inert") |
target | string | 否 | 目标元素 CSS 选择器,默认 ":host" 表示组件宿主元素 |
map | (val: any) => string | null | 否 | 值映射函数,返回 null 时移除属性;省略时用 String(value) |
@attribute({
type: Boolean,
default: false,
a11y: {
ariaAttr: "aria-disabled",
map: v => String(v),
},
})
disabled: boolean = false;
@attribute({
type: Boolean,
default: false,
a11y: {
ariaAttr: "aria-expanded",
target: ".ea-sub-menu__title",
},
})
open: boolean = false;
// placeholder 为空时移除 aria-label
@attribute({
type: String,
default: "",
a11y: {
ariaAttr: "aria-label",
map: v => v || null,
},
})
placeholder: string = "";
// 关闭时设置 inert,打开时移除
@attribute({
type: Boolean,
default: false,
a11y: {
ariaAttr: "inert",
target: ".ea-collapse-item__content",
map: v => v ? null : "",
},
})
active: boolean = false;
| 场景 | ariaAttr | map | 说明 |
|---|---|---|---|
disabled → aria-disabled | "aria-disabled" | v => String(v) | 布尔属性映射 |
checked → aria-checked | "aria-checked" | v => String(!!v) | 布尔属性映射 |
open → aria-expanded | "aria-expanded" | 无需 map | 默认 String(value) |
open → inert(关闭时阻止焦点) | "inert" | v => v ? null : "" | 打开时移除 inert |
placeholder → aria-label | "aria-label" | v => v || null | 空值时移除属性 |
filterable → aria-autocomplete | "aria-autocomplete" | v => v ? "both" : null | 条件映射 |
value → aria-valuenow | "aria-valuenow" | v => String(v) | 数值映射 |
min → aria-valuemin | "aria-valuemin" | v => String(v) | 数值映射 |
max → aria-valuemax | "aria-valuemax" | v => String(v) | 数值映射 |
a11y 在属性变化时自动调用 syncA11yAttribute,无需在 observer 中手动 setAttributeconnectedCallback 中 initA11yAttributes 自动初始化,不依赖 $mount 执行顺序target 为 ":host" 时直接操作宿主元素,其他值通过 shadowRoot.querySelector 查找map 返回 null 时调用 removeAttribute,返回字符串时调用 setAttribute以下场景仍需手动管理,不适合使用 a11y 选项:
aria-activedescendant、aria-labelledby、aria-describedby 的值是子元素动态 IDtarget 只支持 Shadow DOM 内部元素,不支持 Light DOMdisabled || limitDisabled)_setupAria() 中的 id、aria-controls、aria-haspopup 等不随属性变化的设置closeText, showIcon)close-text, show-icon)name 选项,装饰器会自动处理命名转换@attribute({ type: String, default: "" })
closeText: string = ""; // HTML: close-text=""
@attribute({ type: Boolean, default: false })
showIcon: boolean = false; // HTML: show-icon
ElementAttributesMap@CustomElement 自动收集 @attribute 注册的属性,生成 observedAttributesObject.defineProperty 为每个属性创建 getter/setterhasAttribute 判断toggleAttribute,其他类型用 setAttributeattributeChangedCallback 中自动调用 observer 回调title, lang, dir, draggable, tabIndex, style, className, id, hidden, accessKey, contentEditable 等是 HTMLElement 保留属性,使用 @attribute 声明同名属性会导致 jsdom 升级失败。
title → headingtype → variant