mapcomplete/UI/Base/SubtleButton.ts

59 lines
2 KiB
TypeScript
Raw Normal View History

import Translations from "../i18n/Translations";
import Combine from "./Combine";
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement";
import Link from "./Link";
import Img from "./Img";
import {UIEventSource} from "../../Logic/UIEventSource";
2021-06-12 02:58:32 +02:00
import {UIElement} from "../UIElement";
2021-06-12 02:58:32 +02:00
export class SubtleButton extends UIElement {
private readonly imageUrl: string | BaseUIElement;
private readonly message: string | BaseUIElement;
private readonly linkTo: { url: string | UIEventSource<string>; newTab?: boolean };
2021-05-30 00:17:31 +02:00
2021-06-12 02:58:32 +02:00
constructor(imageUrl: string | BaseUIElement, message: string | BaseUIElement, linkTo: { url: string | UIEventSource<string>, newTab?: boolean } = undefined) {
super();
this.imageUrl = imageUrl;
this.message = message;
this.linkTo = linkTo;
2021-05-30 00:17:31 +02:00
}
protected InnerRender(): string | BaseUIElement {
2022-01-18 18:52:42 +01:00
const classes = "block flex p-3 my-2 bg-blue-100 rounded-lg hover:shadow-xl hover:bg-blue-200 link-no-underline";
const message = Translations.W(this.message);
let img;
if ((this.imageUrl ?? "") === "") {
2021-06-10 01:36:20 +02:00
img = undefined;
} else if (typeof (this.imageUrl) === "string") {
img = new Img(this.imageUrl)
2020-09-15 02:29:31 +02:00
} else {
img = this.imageUrl;
2020-09-15 02:29:31 +02:00
}
img?.SetClass("block flex items-center justify-center h-11 w-11 flex-shrink0 mr-4")
2021-05-30 00:17:31 +02:00
const image = new Combine([img])
2021-01-25 03:12:09 +01:00
.SetClass("flex-shrink-0");
2021-06-12 02:58:32 +02:00
if (this.linkTo == undefined) {
this.SetClass(classes)
2021-06-12 02:58:32 +02:00
return new Combine([
2021-05-30 00:17:31 +02:00
image,
message?.SetClass("block overflow-ellipsis"),
2021-06-16 21:23:03 +02:00
]).SetClass("flex group w-full");
2020-07-29 19:02:36 +02:00
}
2021-05-30 00:17:31 +02:00
2021-06-12 02:58:32 +02:00
return new Link(
new Combine([
image,
message?.SetClass("block overflow-ellipsis")
2021-06-16 21:23:03 +02:00
]).SetClass("flex group w-full"),
this.linkTo.url,
this.linkTo.newTab ?? false
).SetClass(classes)
2021-06-12 02:58:32 +02:00
}
}