Skip to content

Commit 17d7dda

Browse files
committed
Add method for setting CSS style on the current attribute
1 parent 53513ee commit 17d7dda

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/Component.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ 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 properties
85+
* @param properties object of style property name and value pairs
86+
*/
87+
public css(properties: Record<string, string>): typeof this;
88+
89+
public css(...args: [string, string] | [Record<string, string>]): typeof this {
90+
if (args.length === 2) {
91+
const [name, value] = args;
92+
this.element.style.setProperty(name, value);
93+
}
94+
else for (const [name, value] of Object.entries(args[0]))
95+
this.css(name, value);
96+
return this;
97+
}
98+
7699
public override on<K extends keyof HTMLElementEventMap>(type: K, listener: (ev: HTMLElementEventMap[K], component: this) => any, options?: boolean | AddEventListenerOptions) {
77100
return super.on(type as any, listener, options);
78101
}

0 commit comments

Comments
 (0)