MapComplete/src/UI/Base/SvelteUIElement.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-01-17 18:31:51 +01:00
import BaseUIElement from "../BaseUIElement"
import { SvelteComponentTyped } from "svelte"
2023-01-22 20:59:50 +01:00
/**
* The SvelteUIComponent serves as a translating class which which wraps a SvelteElement into the BaseUIElement framework.
2023-03-24 19:21:15 +01:00
* Also see ToSvelte.svelte for the opposite conversion
2023-01-22 20:59:50 +01:00
*/
export default class SvelteUIElement<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any
> extends BaseUIElement {
public readonly _svelteComponent: {
new (args: {
target: HTMLElement
props: Props
events?: Events
slots?: Slots
}): SvelteComponentTyped<Props, Events, Slots>
}
public readonly _props: Props
public readonly _events: Events
public readonly _slots: Slots
2024-04-13 02:40:21 +02:00
private tag: "div" | "span" = "div"
public readonly isSvelte = true
2023-01-17 18:31:51 +01:00
constructor(svelteElement, props?: Props, events?: Events, slots?: Slots) {
2023-01-17 18:31:51 +01:00
super()
2024-07-21 10:52:51 +02:00
this._svelteComponent = <any>svelteElement
this._props = props ?? <Props>{}
this._events = events
this._slots = slots
2023-01-17 18:31:51 +01:00
}
2024-04-13 02:40:21 +02:00
public setSpan() {
this.tag = "span"
return this
}
2023-01-17 18:31:51 +01:00
protected InnerConstructElement(): HTMLElement {
const el = document.createElement(this.tag)
2023-01-17 18:31:51 +01:00
new this._svelteComponent({
target: el,
props: this._props,
events: this._events,
slots: this._slots,
2023-01-17 18:31:51 +01:00
})
return el
}
2024-07-21 10:52:51 +02:00
public getClass() {
if (this.clss.size === 0) {
return undefined
}
return this.clss
}
2024-07-21 10:52:51 +02:00
public getStyle() {
if (this.style === "") {
return undefined
}
return this.style
}
2023-01-17 18:31:51 +01:00
}