|
| 1 | +// ViewSelectorWidget.tsx |
| 2 | +// deck.gl |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// Copyright (c) vis.gl contributors |
| 5 | + |
| 6 | +import {render, JSX} from 'preact'; |
| 7 | +import {Widget, type WidgetProps, type WidgetPlacement} from '@deck.gl/core'; |
| 8 | +import {Menu} from './lib/components'; |
| 9 | +import {h} from 'preact'; |
| 10 | + |
| 11 | +/** The available view modes */ |
| 12 | +export type ViewMode = 'single' | 'split-horizontal' | 'split-vertical'; |
| 13 | + |
| 14 | +/** Properties for the ViewSelectorWidget */ |
| 15 | +export type ViewSelectorWidgetProps = WidgetProps & { |
| 16 | + /** Widget positioning within the view. Default 'top-left'. */ |
| 17 | + placement?: WidgetPlacement; |
| 18 | + /** Tooltip label */ |
| 19 | + label?: string; |
| 20 | + /** The initial view mode. Defaults to 'single'. */ |
| 21 | + initialViewMode?: ViewMode; |
| 22 | + /** Callback invoked when the view mode changes */ |
| 23 | + onViewModeChange?: (mode: ViewMode) => void; |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * A widget that renders a popup menu for selecting a view mode. |
| 28 | + * It displays a button with the current view mode icon. Clicking the button |
| 29 | + * toggles a popup that shows three icons for: |
| 30 | + * - Single view |
| 31 | + * - Two views, split horizontally |
| 32 | + * - Two views, split vertically |
| 33 | + */ |
| 34 | +export class ViewSelectorWidget extends Widget<ViewSelectorWidgetProps> { |
| 35 | + static defaultProps: Required<ViewSelectorWidgetProps> = { |
| 36 | + ...Widget.defaultProps, |
| 37 | + id: 'view-selector-widget', |
| 38 | + placement: 'top-left', |
| 39 | + label: 'Split View', |
| 40 | + initialViewMode: 'single', |
| 41 | + // eslint-disable-next-line no-console |
| 42 | + onViewModeChange: (viewMode: string) => { |
| 43 | + console.log(viewMode); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + className = 'deck-widget-view-selector'; |
| 48 | + placement: WidgetPlacement = 'top-left'; |
| 49 | + viewMode: ViewMode; |
| 50 | + |
| 51 | + constructor(props: ViewSelectorWidgetProps = {}) { |
| 52 | + super(props, ViewSelectorWidget.defaultProps); |
| 53 | + this.placement = this.props.placement; |
| 54 | + this.viewMode = this.props.initialViewMode; |
| 55 | + } |
| 56 | + |
| 57 | + setProps(props: Partial<ViewSelectorWidgetProps>) { |
| 58 | + super.setProps(props); |
| 59 | + this.placement = props.placement ?? this.placement; |
| 60 | + } |
| 61 | + |
| 62 | + onRenderHTML(rootElement: HTMLElement) { |
| 63 | + render( |
| 64 | + <Menu<ViewMode> |
| 65 | + className="deck-widget-view-selector" |
| 66 | + menuItems={MENU_ITEMS} |
| 67 | + initialItem={this.props.initialViewMode} |
| 68 | + onSelect={this.handleSelectMode} |
| 69 | + />, |
| 70 | + rootElement |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + handleSelectMode = (viewMode: ViewMode) => { |
| 75 | + this.viewMode = viewMode; |
| 76 | + this.updateHTML(); |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +// Define common icon style. |
| 81 | +const ICON_STYLE = {width: '24px', height: '24px'}; |
| 82 | + |
| 83 | +// Define inline SVG icons for each view mode. |
| 84 | +const ICONS: Record<ViewMode, () => JSX.Element> = { |
| 85 | + single: () => ( |
| 86 | + <svg width="24" height="24" style={ICON_STYLE}> |
| 87 | + <rect x="4" y="4" width="16" height="16" stroke="black" fill="none" strokeWidth="2" /> |
| 88 | + </svg> |
| 89 | + ), |
| 90 | + 'split-horizontal': () => ( |
| 91 | + <svg width="24" height="24" style={ICON_STYLE}> |
| 92 | + <rect x="4" y="4" width="16" height="7" stroke="black" fill="none" strokeWidth="2" /> |
| 93 | + <rect x="4" y="13" width="16" height="7" stroke="black" fill="none" strokeWidth="2" /> |
| 94 | + </svg> |
| 95 | + ), |
| 96 | + 'split-vertical': () => ( |
| 97 | + <svg width="24" height="24" style={ICON_STYLE}> |
| 98 | + <rect x="4" y="4" width="7" height="16" stroke="black" fill="none" strokeWidth="2" /> |
| 99 | + <rect x="13" y="4" width="7" height="16" stroke="black" fill="none" strokeWidth="2" /> |
| 100 | + </svg> |
| 101 | + ) |
| 102 | +}; |
| 103 | + |
| 104 | +// Define menu items for the popup menu. |
| 105 | +const MENU_ITEMS: Array<{value: ViewMode; icon: () => JSX.Element; label: string}> = [ |
| 106 | + {value: 'single', icon: ICONS.single, label: 'Single View'}, |
| 107 | + {value: 'split-horizontal', icon: ICONS['split-horizontal'], label: 'Split Horizontal'}, |
| 108 | + {value: 'split-vertical', icon: ICONS['split-vertical'], label: 'Split Vertical'} |
| 109 | +]; |
0 commit comments