|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +import { LitElement, css, html } from 'lit'; |
| 9 | +import { customElement, state, query } from 'lit/decorators.js'; |
| 10 | +import { classMap } from 'lit/directives/class-map.js'; |
| 11 | +import { styleMap } from 'lit/directives/style-map.js'; |
| 12 | +import '@material/web/icon/icon.js'; |
| 13 | + |
| 14 | +/** |
| 15 | + * A playground preview + editor with a draggable handle. |
| 16 | + */ |
| 17 | +@customElement('drag-playground') |
| 18 | +export class DragPlayground extends LitElement { |
| 19 | + static styles = css` |
| 20 | + :host { |
| 21 | + display: block; |
| 22 | + --_drag-bar-height: 24px; |
| 23 | + --_drag-bar-border-width: 1px; |
| 24 | + --_half-drag-bar-height: calc( |
| 25 | + (var(--_drag-bar-height) / 2) + var(--_drag-bar-border-width) |
| 26 | + ); |
| 27 | + } |
| 28 | +
|
| 29 | + #wrapper { |
| 30 | + display: flex; |
| 31 | + flex-direction: column; |
| 32 | + } |
| 33 | +
|
| 34 | + :host, |
| 35 | + #wrapper, |
| 36 | + ::slotted(*) { |
| 37 | + height: 100%; |
| 38 | + } |
| 39 | +
|
| 40 | + slot { |
| 41 | + display: block; |
| 42 | + overflow: hidden; |
| 43 | + } |
| 44 | +
|
| 45 | + [name='preview'] { |
| 46 | + height: max( |
| 47 | + calc( |
| 48 | + 100% - var(--editor-percentage, 0%) - var(--_half-drag-bar-height) |
| 49 | + ), |
| 50 | + 0px |
| 51 | + ); |
| 52 | + } |
| 53 | +
|
| 54 | + [name='editor'] { |
| 55 | + height: max( |
| 56 | + calc(var(--editor-percentage, 0px) - var(--_half-drag-bar-height)), |
| 57 | + 0px |
| 58 | + ); |
| 59 | + } |
| 60 | +
|
| 61 | + #drag-bar { |
| 62 | + touch-action: none; |
| 63 | + background-color: var(--md-sys-color-surface-container); |
| 64 | + color: var(--md-sys-color-on-surface); |
| 65 | + border: var(--_drag-bar-border-width) solid var(--md-sys-color-outline); |
| 66 | + border-radius: 12px; |
| 67 | + height: var(--_drag-bar-height); |
| 68 | + display: flex; |
| 69 | + justify-content: center; |
| 70 | + align-items: center; |
| 71 | + -webkit-user-select: none; |
| 72 | + user-select: none; |
| 73 | + } |
| 74 | +
|
| 75 | + #drag-bar:hover { |
| 76 | + background-color: var(--md-sys-color-surface-container-high); |
| 77 | + cursor: grab; |
| 78 | + } |
| 79 | +
|
| 80 | + #drag-bar.isDragging { |
| 81 | + background-color: var(--md-sys-color-inverse-surface); |
| 82 | + color: var(--md-sys-color-inverse-on-surface); |
| 83 | + cursor: grabbing; |
| 84 | + } |
| 85 | + `; |
| 86 | + |
| 87 | + /** |
| 88 | + * Whether or not we are in the "dragging" state. |
| 89 | + */ |
| 90 | + @state() private isDragging = false; |
| 91 | + |
| 92 | + /** |
| 93 | + * The percentage of the editor height. |
| 94 | + */ |
| 95 | + @state() private editorHeightPercent = 0; |
| 96 | + |
| 97 | + @query('#wrapper') private wrapperEl!: HTMLElement; |
| 98 | + |
| 99 | + /** |
| 100 | + * A set of pointer IDs in the case that the user is dragging with multiple |
| 101 | + * pointers. |
| 102 | + */ |
| 103 | + private pointerIds = new Set<number>(); |
| 104 | + |
| 105 | + render() { |
| 106 | + return html`<div |
| 107 | + id="wrapper" |
| 108 | + style=${styleMap({ |
| 109 | + '--editor-percentage': `${this.editorHeightPercent}%`, |
| 110 | + })} |
| 111 | + > |
| 112 | + <slot name="preview"></slot> |
| 113 | + <div |
| 114 | + id="drag-bar" |
| 115 | + tabindex="0" |
| 116 | + role="slider" |
| 117 | + aria-orientation="vertical" |
| 118 | + aria-valuemax="100" |
| 119 | + aria-valuemin="0" |
| 120 | + aria-valuenow="${this.editorHeightPercent}" |
| 121 | + aria-valuetext="${this.editorHeightPercent} percent" |
| 122 | + aria-label="Editor height" |
| 123 | + @focus=${this.onFocus} |
| 124 | + @blur=${this.onBlur} |
| 125 | + @keydown=${this.onKeydown} |
| 126 | + @pointerdown=${this.onPointerdown} |
| 127 | + @pointerup=${this.onPointerup} |
| 128 | + @pointermove=${this.onPointermove} |
| 129 | + class=${classMap({ |
| 130 | + isDragging: this.isDragging, |
| 131 | + })} |
| 132 | + > |
| 133 | + <md-icon>drag_handle</md-icon> |
| 134 | + </div> |
| 135 | + <slot name="editor"></slot> |
| 136 | + </div>`; |
| 137 | + } |
| 138 | + |
| 139 | + private onFocus() { |
| 140 | + this.isDragging = true; |
| 141 | + } |
| 142 | + |
| 143 | + private onBlur() { |
| 144 | + this.isDragging = false; |
| 145 | + } |
| 146 | + |
| 147 | + private onKeydown(event: KeyboardEvent) { |
| 148 | + const { key } = event; |
| 149 | + switch (key) { |
| 150 | + case 'ArrowRight': |
| 151 | + case 'ArrowUp': |
| 152 | + this.editorHeightPercent = Math.min(this.editorHeightPercent + 1, 100); |
| 153 | + break; |
| 154 | + case 'ArrowLeft': |
| 155 | + case 'ArrowDown': |
| 156 | + this.editorHeightPercent = Math.max(this.editorHeightPercent - 1, 0); |
| 157 | + break; |
| 158 | + case 'PageUp': |
| 159 | + this.editorHeightPercent = Math.min(this.editorHeightPercent + 10, 100); |
| 160 | + break; |
| 161 | + case 'PageDown': |
| 162 | + this.editorHeightPercent = Math.max(this.editorHeightPercent - 10, 0); |
| 163 | + break; |
| 164 | + case 'Home': |
| 165 | + this.editorHeightPercent = 0; |
| 166 | + break; |
| 167 | + case 'End': |
| 168 | + this.editorHeightPercent = 100; |
| 169 | + break; |
| 170 | + default: |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private onPointerdown(event: PointerEvent) { |
| 176 | + this.isDragging = true; |
| 177 | + |
| 178 | + if (this.pointerIds.has(event.pointerId)) return; |
| 179 | + |
| 180 | + this.pointerIds.add(event.pointerId); |
| 181 | + (event.target as HTMLElement).setPointerCapture(event.pointerId); |
| 182 | + } |
| 183 | + |
| 184 | + private onPointerup(event: PointerEvent) { |
| 185 | + this.pointerIds.delete(event.pointerId); |
| 186 | + (event.target as HTMLElement).releasePointerCapture(event.pointerId); |
| 187 | + |
| 188 | + if (this.pointerIds.size === 0) { |
| 189 | + this.isDragging = false; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private onPointermove(event: PointerEvent) { |
| 194 | + if (!this.isDragging) return; |
| 195 | + |
| 196 | + const { clientY: mouseY } = event; |
| 197 | + const { top: wrapperTop, bottom: wrapperBottom } = |
| 198 | + this.wrapperEl.getBoundingClientRect(); |
| 199 | + |
| 200 | + // The height of the wrapper |
| 201 | + const height = wrapperBottom - wrapperTop; |
| 202 | + |
| 203 | + // Calculate the percentage of the editor height in which the pointer is |
| 204 | + // located |
| 205 | + const editorHeightPercent = 100 - ((mouseY - wrapperTop) / height) * 100; |
| 206 | + |
| 207 | + // Clamp the percentage between 0 and 100 |
| 208 | + this.editorHeightPercent = Math.min(Math.max(editorHeightPercent, 0), 100); |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +declare global { |
| 213 | + interface HTMLElementTagNameMap { |
| 214 | + 'drag-playground': DragPlayground; |
| 215 | + } |
| 216 | +} |
0 commit comments