Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/js/toolbar_item.ts
2313 views
1
import type { RenderProps } from "@anywidget/types";
2
import { html, css } from "lit";
3
import { property } from "lit/decorators.js";
4
import { classMap } from 'lit/directives/class-map.js';
5
6
import { legacyStyles } from './ipywidgets_styles';
7
import { LitWidget } from "./lit_widget";
8
import { materialStyles } from "./styles";
9
import { loadFonts } from "./utils";
10
11
export interface ToolbarItemModel {
12
active: boolean;
13
primary: boolean;
14
icon: string;
15
// Note: "tooltip" is already used by ipywidgets.
16
tooltip_text: string;
17
}
18
19
export class ToolbarItem extends LitWidget<ToolbarItemModel, ToolbarItem> {
20
static get componentName() {
21
return `tool-button`;
22
}
23
24
static override styles = [
25
legacyStyles,
26
materialStyles,
27
css`
28
button {
29
font-size: 16px !important;
30
height: 32px;
31
padding: 0px 0px 0px 4px;
32
width: 32px;
33
}
34
`,
35
];
36
37
modelNameToViewName(): Map<keyof ToolbarItemModel, keyof ToolbarItem> {
38
return new Map([
39
["active", "active"],
40
["primary", "primary"],
41
["icon", "icon"],
42
["tooltip_text", "tooltip_text"],
43
]);
44
}
45
46
@property({ type: Boolean })
47
active: boolean = false;
48
49
@property({ type: Boolean })
50
primary: boolean = true;
51
52
@property({ type: String })
53
icon: string = '';
54
55
@property({ type: String })
56
tooltip_text: string = '';
57
58
override render() {
59
return html`
60
<button
61
class=${classMap({
62
'legacy-button': true,
63
'primary': this.primary,
64
'active': this.active,
65
})}
66
title="${this.tooltip_text}"
67
@click="${this.onClick}">
68
<span class="material-symbols-outlined">${this.icon}</span>
69
</button>`;
70
}
71
72
private onClick(_: Event) {
73
this.active = !this.active;
74
}
75
}
76
77
// Without this check, there's a component registry issue when developing locally.
78
if (!customElements.get(ToolbarItem.componentName)) {
79
customElements.define(ToolbarItem.componentName, ToolbarItem);
80
}
81
82
async function render({ model, el }: RenderProps<ToolbarItemModel>) {
83
loadFonts();
84
const manager = <ToolbarItem>document.createElement(ToolbarItem.componentName);
85
manager.model = model;
86
el.appendChild(manager);
87
}
88
89
export default { render };
90
91