Skip to content

Commit dc9b7b8

Browse files
committed
base component extends node component
1 parent 3dd8fc8 commit dc9b7b8

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

src/BaseComponent.ts

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* You should have received a copy of the GNU Lesser General Public License along with @cldn/components.
1515
* If not, see <https://www.gnu.org/licenses/>.
1616
*/
17+
import {NodeComponent} from "./NodeComponent.js";
1718

1819
/**
1920
* Non-readonly non-method keys
@@ -39,49 +40,36 @@ type WritableKeys<T> = Extract<
3940
* An {@link !Element} component
4041
* @typeParam T Component element type
4142
*/
42-
export abstract class BaseComponent<T extends Element> {
43-
/**
44-
* This component's element
45-
*/
46-
public readonly element: T;
47-
43+
export abstract class BaseComponent<T extends Element> extends NodeComponent<T> {
4844
/**
4945
* @param element Initial element for this component
5046
* @protected
5147
*/
5248
protected constructor(element: T) {
53-
this.element = element;
54-
}
55-
56-
/**
57-
* Insert component after the last child
58-
*/
59-
public append(...components: BaseComponent<any>[]) {
60-
components.forEach((component) => this.element.appendChild(component.element))
61-
return this;
49+
super(element);
6250
}
6351

6452
/**
6553
* Insert component before the first child
6654
*/
67-
public prepend(...components: BaseComponent<any>[]) {
68-
components.forEach((component) => this.element.prepend(component.element))
55+
public prepend(...components: NodeComponent<any>[]) {
56+
components.forEach((component) => this.node.prepend(component.node))
6957
return this;
7058
}
7159

7260
/**
7361
* Add classes
7462
*/
7563
public class(...classes: string[]) {
76-
this.element.classList.add(...classes.flatMap(c => c.split(" ")));
64+
this.node.classList.add(...classes.flatMap(c => c.split(" ")));
7765
return this;
7866
}
7967

8068
/**
8169
* Remove classes
8270
*/
8371
public removeClass(...classes: string[]) {
84-
this.element.classList.remove(...classes.flatMap(c => c.split(" ")));
72+
this.node.classList.remove(...classes.flatMap(c => c.split(" ")));
8573
return this;
8674
}
8775

@@ -90,7 +78,7 @@ export abstract class BaseComponent<T extends Element> {
9078
*/
9179
public toggleClass(...classes: string[]) {
9280
for (const c of new Set(classes.flatMap(c => c.split(" "))))
93-
this.element.classList.toggle(c);
81+
this.node.classList.toggle(c);
9482
return this;
9583
}
9684

@@ -115,7 +103,7 @@ export abstract class BaseComponent<T extends Element> {
115103
* @returns true if component has all the specified classes
116104
*/
117105
public hasClass(...classes: string[]) {
118-
return classes.every(c => this.element.classList.contains(c));
106+
return classes.every(c => this.node.classList.contains(c));
119107
}
120108

121109
/**
@@ -124,7 +112,7 @@ export abstract class BaseComponent<T extends Element> {
124112
* @param [value] attribute value
125113
*/
126114
public attr(name: string, value?: string) {
127-
this.element.setAttribute(name, value ?? "");
115+
this.node.setAttribute(name, value ?? "");
128116
return this;
129117
}
130118

@@ -133,23 +121,15 @@ export abstract class BaseComponent<T extends Element> {
133121
* @param name attribute name
134122
*/
135123
public removeAttr(name: string) {
136-
this.element.removeAttribute(name);
137-
return this;
138-
}
139-
140-
/**
141-
* Set text content
142-
*/
143-
public text(text: string) {
144-
this.element.textContent = text;
124+
this.node.removeAttribute(name);
145125
return this;
146126
}
147127

148128
/**
149129
* Set inner HTML
150130
*/
151131
public html(html: string) {
152-
this.element.innerHTML = html;
132+
this.node.innerHTML = html;
153133
return this;
154134
}
155135

@@ -159,7 +139,7 @@ export abstract class BaseComponent<T extends Element> {
159139
* @param value property value
160140
*/
161141
public set<K extends WritableKeys<T>>(name: K, value: T[K]) {
162-
this.element[name] = value;
142+
this.node[name] = value;
163143
return this;
164144
}
165145

@@ -168,14 +148,14 @@ export abstract class BaseComponent<T extends Element> {
168148
* @param name property name
169149
*/
170150
public get<K extends WritableKeys<T>>(name: K): T[K] {
171-
return this.element[name];
151+
return this.node[name];
172152
}
173153

174154
/**
175155
* Remove the element
176156
*/
177157
public remove(): this {
178-
this.element.remove();
158+
this.node.remove();
179159
return this;
180160
}
181161

@@ -185,15 +165,15 @@ export abstract class BaseComponent<T extends Element> {
185165
* @param listener
186166
* @param options
187167
*/
188-
public on<K extends keyof ElementEventMap>(type: K, listener: (ev: ElementEventMap[K], component: this) => any, options?: boolean | AddEventListenerOptions) {
189-
this.element.addEventListener(type, e => listener(e, this), options);
168+
public override on<K extends keyof ElementEventMap>(type: K, listener: (ev: ElementEventMap[K], component: this) => any, options?: boolean | AddEventListenerOptions) {
169+
this.node.addEventListener(type, e => listener(e, this), options);
190170
return this;
191171
}
192172

193173
/**
194174
* Get this component's outer HTML
195175
*/
196-
public toString() {
197-
return this.element.outerHTML;
176+
public override toString() {
177+
return this.node.outerHTML;
198178
}
199179
}

0 commit comments

Comments
 (0)