Skip to content

Commit afa89cd

Browse files
authored
Add method for setting CSS style on the current attribute (#15)
2 parents 53513ee + e153376 commit afa89cd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/Component.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,42 @@ export class Component<T extends HTMLElement = HTMLElement> extends BaseComponen
7373
return [...this.element.querySelectorAll<T>(selectors)].map(e => new Component<T>(e));
7474
}
7575

76+
/**
77+
* Set style property
78+
* @param name Property name
79+
* @param value Property value
80+
*/
81+
public css(name: string, value: string): typeof this;
82+
83+
/**
84+
* Set style property
85+
* @param name Property name
86+
* @param value Property value
87+
* @param priority Whether to make this rule `!important`
88+
*/
89+
public css(name: string, value: string, priority: boolean): typeof this;
90+
91+
/**
92+
* Set style properties
93+
* @param properties Object of style property name and value pairs
94+
*/
95+
public css(properties: Record<string, string>): typeof this;
96+
97+
public css(...args: [string, string] | [string, string, boolean] | [Record<string, string>]): typeof this {
98+
if (args.length === 2 || args.length === 3) {
99+
const name: string = args[0];
100+
const value: string = args[1];
101+
const priority: boolean = args[2] ?? false;
102+
this.element.style.setProperty(name, value, priority ? "important" : void 0);
103+
}
104+
else {
105+
const properties: Record<string, string> = args[0];
106+
for (const [name, value] of Object.entries(properties))
107+
this.css(name, value);
108+
}
109+
return this;
110+
}
111+
76112
public override on<K extends keyof HTMLElementEventMap>(type: K, listener: (ev: HTMLElementEventMap[K], component: this) => any, options?: boolean | AddEventListenerOptions) {
77113
return super.on(type as any, listener, options);
78114
}

0 commit comments

Comments
 (0)