-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtoggle-widget.tsx
More file actions
103 lines (93 loc) · 2.71 KB
/
Copy pathtoggle-widget.tsx
File metadata and controls
103 lines (93 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import type {WidgetPlacement, WidgetProps} from '@deck.gl/core';
import {render, type JSX} from 'preact';
import {Widget} from '@deck.gl/core';
import {IconButton} from './lib/components/icon-button';
import {updateWidgetTooltip} from './lib/widget-tooltip';
export type ToggleWidgetProps = WidgetProps & {
/** Widget positioning within the view. Default 'bottom-left'. */
placement?: WidgetPlacement;
/** View to attach to and interact with. Required when using multiple views. */
viewId?: string | null;
/** If the toggle is checked when first added */
initialChecked?: boolean;
/** Data url to display as icon */
icon: string;
/** Data url to display as icon when it is checked */
onIcon?: string;
/** Tooltip label */
label?: string;
/** Tooltip label when it is checked */
onLabel?: string;
/** Icon color, a CSS Color string */
color?: string;
/** Icon color when it is checked, a CSS Color string */
onColor?: string;
/** Callback when the widget is clicked */
onChange?: (checked: boolean) => void;
};
/**
* A generic widget that displays a button with icon or text content.
*/
export class ToggleWidget extends Widget<ToggleWidgetProps> {
static defaultProps: Required<ToggleWidgetProps> = {
...Widget.defaultProps,
id: 'icon',
placement: 'top-left',
viewId: null,
initialChecked: false,
icon: '',
onIcon: undefined!,
label: '',
onLabel: undefined!,
color: '',
onColor: undefined!,
onChange: undefined!
};
className = 'deck-widget-toggle';
placement: WidgetPlacement = 'top-left';
protected override onAfterRenderHTML = updateWidgetTooltip;
checked: boolean;
constructor(props: ToggleWidgetProps) {
super(props);
this.checked = this.props.initialChecked;
this.setProps(this.props);
}
setProps(props: Partial<ToggleWidgetProps>) {
this.placement = props.placement ?? this.placement;
this.viewId = props.viewId;
super.setProps(props);
}
onRenderHTML(rootElement: HTMLElement): void {
const {
className,
style,
icon,
label,
color,
onIcon = icon,
onLabel = label,
onColor = color
} = this.props;
const on = this.checked;
rootElement.dataset.checked = String(on);
render(
<IconButton
className={className}
style={style as JSX.CSSProperties}
icon={on ? onIcon : icon}
label={on ? onLabel : label}
color={on ? onColor : color}
onClick={this._toggle}
/>,
rootElement
);
}
private _toggle = () => {
this.checked = !this.checked;
this.props.onChange?.(this.checked);
this.updateHTML();
};
}