compelem 0.11.0-b1 → 0.20.0-b2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CompElem.d.ts ADDED
@@ -0,0 +1,148 @@
1
+ import { IComponent } from "./IComponent";
2
+ import { CssTemplate } from "./render/CssTemplate";
3
+ import { Template } from "./render/Template";
4
+ import { DefaultProps, TmplFn, UpdatePoint } from "./types";
5
+ /**
6
+ * CompElem基类,意为组件元素。提供了基本内置属性及生命周期等必备接口
7
+ * 每个组件都需要继承自该类
8
+ *
9
+ * @author holyhigh2
10
+ */
11
+ export declare class CompElem<T = HTMLElement> extends HTMLElement implements IComponent<T> {
12
+ #private;
13
+ static __l_globalRule: HTMLStyleElement;
14
+ static defaults(options: DefaultProps): void;
15
+ __updateTree: Array<UpdatePoint>;
16
+ __cssSheets: Array<CSSStyleSheet>;
17
+ _eventList: Array<[string, Function, Node, Function?]>;
18
+ __docoEventMap: Map<string, Function>;
19
+ __updateCssDeps: Array<string>;
20
+ __updateSubViewDeps: Map<string, Set<UpdatePoint>>;
21
+ __updateViewDeps: string[];
22
+ _watchUpdateMap: Record<string, Set<Function>>;
23
+ _watchDeepUpdateMap: Record<string, Set<Function>>;
24
+ _watchKeys: string[];
25
+ _watchKeysOnceMap: Map<string, boolean>;
26
+ _watchKeysDeep: string[];
27
+ _watchUpdateSetInNextTick: Set<Function>;
28
+ _watchUpdateArgsInNextTick: Map<Function, Record<string, any>>;
29
+ _computedUpdateDeps: Map<string, Set<Function>>;
30
+ _computedUpdateSetInNextTick: Set<Function>;
31
+ _hasChangedPropOrStateMap: Map<string, Function>;
32
+ _hasSyncPropSet: Set<string>;
33
+ _hasShallowStateSet: Set<string>;
34
+ get [Symbol.toStringTag](): string;
35
+ get cid(): number;
36
+ get attrs(): Record<string, string>;
37
+ get props(): Record<string, any>;
38
+ get renderRoot(): T | undefined;
39
+ get renderRoots(): HTMLElement[];
40
+ get parentComponent(): CompElem | undefined;
41
+ get wrapperComponent(): CompElem | undefined;
42
+ get slotHooks(): Record<string, (...args: any[]) => Template>;
43
+ get styleSheets(): CSSStyleSheet[];
44
+ get globalStyleSheet(): CSSStyleSheet;
45
+ get isMounted(): boolean;
46
+ get slots(): Record<string, Array<Node>>;
47
+ /**
48
+ * 组件样式,CSSStyleSheet可动态变更
49
+ */
50
+ static get styles(): Array<string | CSSStyleSheet>;
51
+ static get globalStyle(): string | undefined;
52
+ static get hostStyle(): string | CSSStyleSheet | undefined;
53
+ get styles(): Array<CssTemplate>;
54
+ constructor(...args: any[]);
55
+ insertStyleSheet(sheet: string | CSSStyleSheet): CSSStyleSheet | null;
56
+ /**
57
+ * Returns the root component in the parent chain, or itself if it's the top-level component.
58
+ */
59
+ get rootComponent(): CompElem;
60
+ connectedCallback(): void;
61
+ disconnectedCallback(): void;
62
+ __bindEvents(): void;
63
+ __unbindEvents(): void;
64
+ beforeDestroyed(): void;
65
+ destroyed(): void;
66
+ get isDestroyed(): boolean;
67
+ destroy(): void;
68
+ __init(): void;
69
+ propsReady(props: Record<string, any>): void;
70
+ render(): Template | null;
71
+ beforeMount(): void;
72
+ mounted(): void;
73
+ __onSlotChangeHook(e: Event): void;
74
+ slotChange(slot: HTMLSlotElement, name: string): void;
75
+ attributeChangedCallback(attributeName: string, oldValue: string | null, newValue: string | null): void;
76
+ /**
77
+ * 是否需要更新,可获取变更属性
78
+ * 返回true时更新
79
+ */
80
+ shouldUpdate(changed: Record<string, any>): boolean;
81
+ /**
82
+ * 1. 调用render
83
+ * 2. 更新@query/all
84
+ * 3. 更新ref
85
+ * 4. 更新prop到attr的映射
86
+ * @param changed
87
+ */
88
+ updated(changed: Record<string, any>): void;
89
+ /**
90
+ * 由监控变量调用
91
+ * @param stateKey
92
+ * @param ov
93
+ * @param rootStateKey 如果是对象内部属性变更,会返回根属性名
94
+ * @returns
95
+ */
96
+ _notify(ov: any, chain: string[], subNewValue?: any, subOldValue?: any): void;
97
+ _requestWatchUpdate(newValue: any, oldValue: any, fullPath: string, rootObjNew?: any, rootObjOld?: any): void;
98
+ _requestComputedUpdate(fullPath: string): void;
99
+ _updateProps(props: Record<string, any>): void;
100
+ _wrapperProp: Record<string, string>;
101
+ _initProps(props: Record<string, any>, attrs?: Record<string, any>): void;
102
+ /**
103
+ * 绑定slot标签,render时调用
104
+ */
105
+ _bindSlot(slot: HTMLSlotElement, name: string, props: Record<string, any>): void;
106
+ _bindSlotHook(name: string, hook: (...args: any[]) => Template): void;
107
+ _updateSlot(name: string, propName?: string, value?: any): void;
108
+ _asyncDirectives: WeakMap<TmplFn, any>;
109
+ renderAsync(cbk: TmplFn, ...args: any[]): void;
110
+ __attrChanged(name: string, oldValue: string | null, newValue: string | null): void;
111
+ _regWrapper(wrapperComponent: CompElem): void;
112
+ _regSubViewDeps(props: string[], up: UpdatePoint): void;
113
+ _getPrivateData(): Record<string, any>;
114
+ /**
115
+ * 抛出自定义事件
116
+ * @param evName 事件名称
117
+ * @param args 自定义参数
118
+ */
119
+ emit(evName: string, arg?: Record<string, any>, options?: {
120
+ event?: Event;
121
+ bubbles?: boolean;
122
+ composed?: boolean;
123
+ }): void;
124
+ /**
125
+ * 在root上绑定事件
126
+ * @param evName
127
+ * @param hook
128
+ * @returns 函数钩子,用于卸载
129
+ */
130
+ on(evName: string, hook: (e: Event) => void): any;
131
+ /**
132
+ * 从root上移除事件
133
+ * @param evName
134
+ * @param hook on函数返回的钩子,可选。为空时移除所有evName事件
135
+ */
136
+ off(evName: string, hook?: (e: Event) => void): void;
137
+ addEvent(node: Node, evName: string, hook: (e: Event) => void): any;
138
+ removeEvent(node: Node, evName: string, hook?: (e: Event) => void): void;
139
+ /**
140
+ * 下一帧执行
141
+ * @param cbk
142
+ */
143
+ nextTick(cbk: () => void): void;
144
+ /**
145
+ * 强制更新一次视图
146
+ */
147
+ forceUpdate(): void;
148
+ }
@@ -0,0 +1,65 @@
1
+ import { CssTemplate } from "./render/CssTemplate";
2
+ import { Template } from "./render/Template";
3
+ /**
4
+ * 组件接口
5
+ * 定义组件属性、生命周期及方法
6
+ * @author holyhigh2
7
+ */
8
+ export interface IComponent<T = HTMLElement> {
9
+ get cid(): number;
10
+ get attrs(): Record<string, string>;
11
+ get props(): Record<string, any>;
12
+ get renderRoot(): T | undefined;
13
+ get renderRoots(): HTMLElement[];
14
+ get rootComponent(): HTMLElement | undefined;
15
+ get parentComponent(): HTMLElement | undefined;
16
+ get wrapperComponent(): HTMLElement | undefined;
17
+ get slots(): Record<string, Array<Node>>;
18
+ get slotHooks(): Record<string, (...args: any[]) => Template>;
19
+ get styleSheets(): CSSStyleSheet[];
20
+ get globalStyleSheet(): CSSStyleSheet;
21
+ get isMounted(): boolean;
22
+ get styles(): Array<CssTemplate>;
23
+ propsReady(props: Record<string, any>): void;
24
+ render(): Template | null;
25
+ beforeMount(): void;
26
+ mounted(): void;
27
+ connectedCallback(): void;
28
+ disconnectedCallback(): void;
29
+ beforeDestroyed(): void;
30
+ destroyed(): void;
31
+ shouldUpdate(changed: Record<string, any>): boolean;
32
+ updated(changed: Record<string, any>): void;
33
+ slotChange(slot: HTMLSlotElement, slotName: string): void;
34
+ attributeChangedCallback(attributeName: string, oldValue: string | null, newValue: string | null): void;
35
+ /**
36
+ * 抛出自定义事件
37
+ * @param evName 事件名称
38
+ * @param args 自定义参数
39
+ */
40
+ emit(evName: string, arg: Record<string, any>, options?: {
41
+ event?: Event;
42
+ bubbles?: boolean;
43
+ composed?: boolean;
44
+ }): void;
45
+ /**
46
+ * 在root上绑定事件
47
+ * @param evName
48
+ * @param hook
49
+ */
50
+ on(evName: string, hook: (e: Event) => void): void;
51
+ /**
52
+ * 下一帧执行
53
+ * @param cbk
54
+ */
55
+ nextTick(cbk: () => void): void;
56
+ /**
57
+ * 强制更新一次视图
58
+ */
59
+ forceUpdate(): void;
60
+ /**
61
+ * 向组件插入样式表,没有shadowDOM的组件调用无效
62
+ * @param sheet
63
+ */
64
+ insertStyleSheet(sheet: string | CSSStyleSheet): CSSStyleSheet | null;
65
+ }
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  一个现代化、响应式、快速、轻量的WebComponent开发库。为开发者提供丰富、灵活、可扩展的声明式接口
3
3
 
4
4
  ## 概览
5
- CompElem 基于 Class 进行构建,该模型允许开发者使用装饰器进行声明式编码,核心特性包括:
5
+ CompElem 基于 Class 进行构建,该模型允许开发者使用装饰器进行声明式编码并可使用继承、扩展等高级特性,其核心特性包括:
6
6
  - 类JSX的原生模板系统
7
7
  - 丰富的装饰器及指令
8
8
  - 可选的生命周期
@@ -45,10 +45,10 @@ export class PageTest extends CompElem {
45
45
  //动态样式
46
46
  get styles() {
47
47
  return [
48
- () => `h2,p,i,h3{
48
+ css`h2,p,i,h3{
49
49
  background-image:${this.color};
50
50
  }`,
51
- () => `h2,p,i,h3{
51
+ css`h2,p,i,h3{
52
52
  filter:hue-rotate(${this.rotation}deg);
53
53
  }`,
54
54
  ]
@@ -106,6 +106,10 @@ export class PageTest extends CompElem {
106
106
  render(): Template{
107
107
  return html`<div>Hello CompElem</div>`
108
108
  }
109
+ //对于无需内部样式的组件(如容器类)可返回null,此时不会创建Shadow DOM
110
+ render(){
111
+ return null
112
+ }
109
113
  ```
110
114
  - ### 视图模板-属性表达式
111
115
 
@@ -132,20 +136,38 @@ export class PageTest extends CompElem {
132
136
  - kebab 短横线
133
137
  - snake 下划线
134
138
 
139
+ - ### 无视图
140
+ 对于无体组件无需定义渲染函数,常用于layout、grid等结构控制相关组件。无视图组件仅支持global及host样式,如
141
+ ```ts
142
+ static get hostStyle(): string {
143
+ return `
144
+ l-main{
145
+ display: block;
146
+ flex: 1;
147
+ overflow: hidden;
148
+ }
149
+ `;
150
+ }
151
+ ```
152
+ 同样,无视图组件的renderRoot/renderRoots/...等属性都为空
153
+
135
154
  - ### 样式
136
- 使用静态函数定义组件样式或全局样式(如弹框)
155
+ 使用静态函数定义组件样式或全局样式(如弹框),对于包裹在上级组件内的动态样式(如:hover)时,可通过hostStyles进行指定
137
156
  ```ts
138
157
  static get styles(): Array<string | CSSStyleSheet> {
139
158
  return [];
140
159
  }
141
- static get globalStyles(): Array<string> {
142
- return [];
160
+ static get globalStyle(): string {
161
+ return '';
162
+ }
163
+ static get hostStyle(): string {
164
+ return '';
143
165
  }
144
166
  ```
145
167
  对于需要动态控制 host 元素样式可以使用组件实例 getter
146
168
  ```ts
147
169
  get styles(){
148
- return [()=>`:host{
170
+ return [css`:host{
149
171
  ${this.border?'border: 1px solid rgb(var(--l-color-border-secondary)); ':''}
150
172
  }`]
151
173
  }
@@ -171,7 +193,7 @@ export class PageTest extends CompElem {
171
193
  }
172
194
  ```
173
195
 
174
- 属性可以在组件内修改但默认不会同步父组件,除非显式指定`sync`或自行 emit update 事件
196
+ 属性可以在组件内修改但默认不会同步父组件,除非显式指定`sync`或自行 emit `update:xxx` 事件
175
197
  全部注解参数见 `PropOption`
176
198
 
177
199
  - ### 状态
@@ -237,19 +259,26 @@ export class PageTest extends CompElem {
237
259
  - `readonly` rootComponent 根组件引用
238
260
  - `readonly` parentComponent 父组件引用,可能为空
239
261
  - `readonly` wrapperComponent 包装(组件所在视图归属)组件引用,可能为空
240
- - `readonly` renderRoot/renderRoots 渲染根元素/渲染根元素列表
241
- - `readonly` shadowRoot 阴影DOM
262
+ - `readonly` renderRoot/renderRoots 渲染根元素/渲染根元素列表,可能为空
263
+ - `readonly` shadowRoot 阴影DOM,可能为空
242
264
  - `readonly` slots 插槽元素映射
243
265
  - `readonly` slotHooks 动态插槽钩子映射
244
- - `readonly` css 组件样式对象列表
266
+ - `readonly` styleSheets 组件样式对象列表
267
+ - `readonly` globalStyleSheet 通过静态getter创建的全局样式表对象,所有实例共享
245
268
  - `readonly` attrs 组件特性
246
269
  - `readonly` props 组件属性
270
+ - `readonly` isMounted 组件是否已挂载
271
+ - `readonly` isDestroyed 组件是否已销毁
247
272
  - slotComponent 所在插槽组件
248
273
  - on(evName: string, hook: (e: Event) => void) 在root元素上绑定事件
274
+ - off(evName: string, hook?: (e: Event) => void) 在root元素上卸载事件
275
+ - addEvent(node: Node, evName: string, hook: (e: Event) => void) 在指定元素上绑定事件
276
+ - removeEvent(node: Node, evName: string, hook?: (e: Event) => void) 在指定元素上卸载事件
249
277
  - emit(evName: string, arg: Record<string, any>, options?: {event?: Event;bubbles?: boolean;composed?: boolean;}) 抛出自定义事件
250
278
  - nextTick(cbk: () => void) 下一帧执行函数
251
279
  - forceUpdate() 强制更新一次视图
252
- - insertStyleSheet(sheet: string | CSSStyleSheet): CSSStyleSheet 向组件插入样式表
280
+ - insertStyleSheet(sheet: string | CSSStyleSheet): CSSStyleSheet 向组件ShadowDOM插入样式表
281
+ - destroy() 销毁组件
253
282
 
254
283
  ## 组件渲染流程
255
284
 
@@ -331,7 +360,7 @@ return html` <l-tooltip>
331
360
  "content"
332
361
  )}
333
362
  </l-tooltip>`;
334
- ```
363
+ ```
335
364
  2. 通过元素注入
336
365
  可在非CompElem环境中使用,比如将组件嵌入到React/Vue环境中使用
337
366
  ```html
@@ -379,7 +408,8 @@ return html` <l-tooltip>
379
408
  | ------- | --------- | ----------------------------------------------------------- | ------------------------------------------------------ |
380
409
  | bind | TAG | 绑定属性/特性到标签上,根据标签类型及组件 prop 定义自动判断 | `<div a="b" ${bind(obj)}>` |
381
410
  | show | TAG | 隐藏/显示标签(基于 display) | `<div a="b" ${show(visible)}>` |
382
- | model | TAG | 双向绑定 | `<div a="b" ${model(xx,modelPath?)}>` |
411
+ | model | TAG | 双向绑定 | `<div a="b" ${model(xx,modelPath?)}>`
412
+ | sync | PROP | 类似Model,实现属性的同步跟踪 | `<l-dialog .visible="${sync(this.visible)}">` |
383
413
  | classes | CLASS | 绑定样式类属性,支持对象/数组/字符串。可以和静态字符混用 | `<div class="otherClass ${classes(obj)}>"` |
384
414
  | styles | STYLE | 绑定样式规则属性,支持对象/字符串。可以和静态字符混用 | `<div style="a:b;${styles(obj)}>"` |
385
415
  | forEach | TEXT/SLOT | 输出循环结构 | `...>${forEach(ary,(item)=>html`...`)}<...` |
@@ -388,6 +418,7 @@ return html` <l-tooltip>
388
418
  | when | TEXT/SLOT | 多条件分支,支持 switch/ifelse 两种模式 | ` ...>${when(condition,{c1:()=>html``,c2:...})}<... ` |
389
419
  | slot | SLOT | 动态插槽 | ` ...>${slot((args) => html``)}<... ` |
390
420
  | htmlD | TAG | 向指定元素插入HTML内容 | `<div a="b" ${htmlD('<b>1</b>')}>` |
421
+ | htmlC | TEXT/SLOT | 向指定文本位置插入指定HTML内容 | `...>${htmlC('<b>1</b>')}><...` |
391
422
 
392
423
  ## 装饰器 Decorator
393
424
 
@@ -396,11 +427,12 @@ return html` <l-tooltip>
396
427
  > 设置 getter/setter 后,该属性的`@watch` 将会失效
397
428
  - @query/queryAll 定义 CssSelector 查询结果
398
429
  - @tag 自定义组件的标签名
399
- - @event 定义全局事件
400
430
  - @watch 监控 state/prop 变更
401
431
  - @computed 计算属性,仅在响应变量变更时更新缓存值
402
432
  - @debounced 定义函数防抖
403
- - @bindThis 为组件方法绑定this
433
+ - @event 定义事件,支持修饰符
434
+ - @onced 定义一次性事件
435
+ - @throttled 定义节流函数
404
436
 
405
437
  #### 继承
406
438
  部分指令可由子类继承不会覆盖,包括@state/@prop/@watch/@computed