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