@deck.gl/widgets 9.0.0-beta.9 → 9.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+ /* global document */
2
+ import {FlyToInterpolator} from '@deck.gl/core';
3
+ import type {Deck, Viewport, Widget, WidgetPlacement} from '@deck.gl/core';
4
+ import {render} from 'preact';
5
+ import {ButtonGroup, GroupedIconButton} from './components';
6
+
7
+ interface ZoomWidgetProps {
8
+ id: string;
9
+ placement?: WidgetPlacement;
10
+ /**
11
+ * View to attach to and interact with. Required when using multiple views.
12
+ */
13
+ viewId?: string | null;
14
+ /**
15
+ * Button orientation.
16
+ */
17
+ orientation?: 'vertical' | 'horizontal';
18
+ /**
19
+ * Tooltip message on zoom in button.
20
+ */
21
+ zoomInLabel?: string;
22
+ /**
23
+ * Tooltip message on zoom out button.
24
+ */
25
+ zoomOutLabel?: string;
26
+ /**
27
+ * Zoom transition duration in ms.
28
+ */
29
+ transitionDuration?: number;
30
+ /**
31
+ * CSS inline style overrides.
32
+ */
33
+ style?: Partial<CSSStyleDeclaration>;
34
+ /**
35
+ * Additional CSS class.
36
+ */
37
+ className?: string;
38
+ }
39
+
40
+ export class ZoomWidget implements Widget<ZoomWidgetProps> {
41
+ id = 'zoom';
42
+ props: ZoomWidgetProps;
43
+ placement: WidgetPlacement = 'top-left';
44
+ orientation: 'vertical' | 'horizontal' = 'vertical';
45
+ viewId?: string | null = null;
46
+ viewport?: Viewport;
47
+ deck?: Deck;
48
+ element?: HTMLDivElement;
49
+
50
+ constructor(props: ZoomWidgetProps) {
51
+ this.id = props.id || 'zoom';
52
+ this.viewId = props.viewId || null;
53
+ this.placement = props.placement || 'top-left';
54
+ this.orientation = props.orientation || 'vertical';
55
+ props.transitionDuration = props.transitionDuration || 200;
56
+ props.zoomInLabel = props.zoomInLabel || 'Zoom In';
57
+ props.zoomOutLabel = props.zoomOutLabel || 'Zoom Out';
58
+ props.style = props.style || {};
59
+ this.props = props;
60
+ }
61
+
62
+ onAdd({deck}: {deck: Deck}): HTMLDivElement {
63
+ const {style, className} = this.props;
64
+ const element = document.createElement('div');
65
+ element.classList.add('deck-widget', 'deck-widget-zoom');
66
+ if (className) element.classList.add(className);
67
+ if (style) {
68
+ Object.entries(style).map(([key, value]) => element.style.setProperty(key, value as string));
69
+ }
70
+ const ui = (
71
+ <ButtonGroup orientation={this.orientation}>
72
+ <GroupedIconButton
73
+ onClick={() => this.handleZoomIn()}
74
+ label={this.props.zoomInLabel}
75
+ className="deck-widget-zoom-in"
76
+ />
77
+ <GroupedIconButton
78
+ onClick={() => this.handleZoomOut()}
79
+ label={this.props.zoomOutLabel}
80
+ className="deck-widget-zoom-out"
81
+ />
82
+ </ButtonGroup>
83
+ );
84
+ render(ui, element);
85
+
86
+ this.deck = deck;
87
+ this.element = element;
88
+
89
+ return element;
90
+ }
91
+
92
+ onRemove() {
93
+ this.deck = undefined;
94
+ this.element = undefined;
95
+ }
96
+
97
+ setProps(props: Partial<ZoomWidgetProps>) {
98
+ Object.assign(this.props, props);
99
+ }
100
+
101
+ onViewportChange(viewport) {
102
+ this.viewport = viewport;
103
+ }
104
+
105
+ handleZoom(nextZoom: number) {
106
+ const viewId = this.viewId || 'default-view';
107
+ const nextViewState = {
108
+ ...this.viewport,
109
+ zoom: nextZoom,
110
+ transitionDuration: this.props.transitionDuration,
111
+ transitionInterpolator: new FlyToInterpolator()
112
+ };
113
+ // @ts-ignore Using private method temporary until there's a public one
114
+ this.deck._onViewStateChange({viewId, viewState: nextViewState, interactionState: {}});
115
+ }
116
+
117
+ handleZoomIn() {
118
+ if (this.viewport) {
119
+ this.handleZoom(this.viewport.zoom + 1);
120
+ }
121
+ }
122
+
123
+ handleZoomOut() {
124
+ if (this.viewport) {
125
+ this.handleZoom(this.viewport.zoom - 1);
126
+ }
127
+ }
128
+ }