1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import classNames from "classnames";
|
18 | import * as React from "react";
|
19 | import { polyfill } from "react-lifecycles-compat";
|
20 |
|
21 | import { AbstractPureComponent2, Classes, Keys } from "../../common";
|
22 | import { DISPLAYNAME_PREFIX } from "../../common/props";
|
23 | import { clamp } from "../../common/utils";
|
24 | import { HandleProps } from "./handleProps";
|
25 | import { formatPercentage } from "./sliderUtils";
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export interface IInternalHandleProps extends HandleProps {
|
31 | disabled?: boolean;
|
32 | label: JSX.Element | string | undefined;
|
33 | max: number;
|
34 | min: number;
|
35 | stepSize: number;
|
36 | tickSize: number;
|
37 | tickSizeRatio: number;
|
38 | vertical: boolean;
|
39 | }
|
40 |
|
41 | export interface IHandleState {
|
42 |
|
43 | isMoving?: boolean;
|
44 | }
|
45 |
|
46 |
|
47 | const NUMBER_PROPS = ["max", "min", "stepSize", "tickSize", "value"];
|
48 |
|
49 |
|
50 | @polyfill
|
51 | export class Handle extends AbstractPureComponent2<IInternalHandleProps, IHandleState> {
|
52 | public static displayName = `${DISPLAYNAME_PREFIX}.SliderHandle`;
|
53 |
|
54 | public state = {
|
55 | isMoving: false,
|
56 | };
|
57 |
|
58 | private handleElement: HTMLElement | null = null;
|
59 |
|
60 | private refHandlers = {
|
61 | handle: (el: HTMLSpanElement) => (this.handleElement = el),
|
62 | };
|
63 |
|
64 | public componentDidMount() {
|
65 |
|
66 |
|
67 | this.forceUpdate();
|
68 | }
|
69 |
|
70 | public render() {
|
71 | const { className, disabled, label } = this.props;
|
72 | const { isMoving } = this.state;
|
73 |
|
74 | return (
|
75 | <span
|
76 | className={classNames(Classes.SLIDER_HANDLE, { [Classes.ACTIVE]: isMoving }, className)}
|
77 | onKeyDown={disabled ? undefined : this.handleKeyDown}
|
78 | onKeyUp={disabled ? undefined : this.handleKeyUp}
|
79 | onMouseDown={disabled ? undefined : this.beginHandleMovement}
|
80 | onTouchStart={disabled ? undefined : this.beginHandleTouchMovement}
|
81 | ref={this.refHandlers.handle}
|
82 | style={this.getStyleProperties()}
|
83 | tabIndex={0}
|
84 | >
|
85 | {label == null ? null : <span className={Classes.SLIDER_LABEL}>{label}</span>}
|
86 | </span>
|
87 | );
|
88 | }
|
89 |
|
90 | public componentWillUnmount() {
|
91 | this.removeDocumentEventListeners();
|
92 | }
|
93 |
|
94 |
|
95 | public clientToValue(clientPixel: number) {
|
96 | const { stepSize, tickSize, value, vertical } = this.props;
|
97 | if (this.handleElement == null) {
|
98 | return value;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 | const clientPixelNormalized = vertical ? window.innerHeight - clientPixel : clientPixel;
|
104 | const handleCenterPixel = this.getHandleElementCenterPixel(this.handleElement);
|
105 | const pixelDelta = clientPixelNormalized - handleCenterPixel;
|
106 |
|
107 | if (isNaN(pixelDelta)) {
|
108 | return value;
|
109 | }
|
110 |
|
111 | return value + Math.round(pixelDelta / (tickSize * stepSize)) * stepSize;
|
112 | }
|
113 |
|
114 | public mouseEventClientOffset(event: MouseEvent | React.MouseEvent<HTMLElement>) {
|
115 | return this.props.vertical ? event.clientY : event.clientX;
|
116 | }
|
117 |
|
118 | public touchEventClientOffset(event: TouchEvent | React.TouchEvent<HTMLElement>) {
|
119 | const touch = event.changedTouches[0];
|
120 | return this.props.vertical ? touch.clientY : touch.clientX;
|
121 | }
|
122 |
|
123 | public beginHandleMovement = (event: MouseEvent | React.MouseEvent<HTMLElement>) => {
|
124 | document.addEventListener("mousemove", this.handleHandleMovement);
|
125 | document.addEventListener("mouseup", this.endHandleMovement);
|
126 | this.setState({ isMoving: true });
|
127 | this.changeValue(this.clientToValue(this.mouseEventClientOffset(event)));
|
128 | };
|
129 |
|
130 | public beginHandleTouchMovement = (event: TouchEvent | React.TouchEvent<HTMLElement>) => {
|
131 | document.addEventListener("touchmove", this.handleHandleTouchMovement);
|
132 | document.addEventListener("touchend", this.endHandleTouchMovement);
|
133 | document.addEventListener("touchcancel", this.endHandleTouchMovement);
|
134 | this.setState({ isMoving: true });
|
135 | this.changeValue(this.clientToValue(this.touchEventClientOffset(event)));
|
136 | };
|
137 |
|
138 | protected validateProps(props: IInternalHandleProps) {
|
139 | for (const prop of NUMBER_PROPS) {
|
140 | if (typeof (props as any)[prop] !== "number") {
|
141 | throw new Error(`[Blueprint] <Handle> requires number value for ${prop} prop`);
|
142 | }
|
143 | }
|
144 | }
|
145 |
|
146 | private getStyleProperties = (): React.CSSProperties => {
|
147 | if (this.handleElement == null) {
|
148 | return {};
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | const { min = 0, tickSizeRatio, value, vertical } = this.props;
|
158 | const { handleMidpoint } = this.getHandleMidpointAndOffset(this.handleElement, true);
|
159 | const offsetRatio = (value - min) * tickSizeRatio;
|
160 | const offsetCalc = `calc(${formatPercentage(offsetRatio)} - ${handleMidpoint}px)`;
|
161 | return vertical ? { bottom: offsetCalc } : { left: offsetCalc };
|
162 | };
|
163 |
|
164 | private endHandleMovement = (event: MouseEvent) => {
|
165 | this.handleMoveEndedAt(this.mouseEventClientOffset(event));
|
166 | };
|
167 |
|
168 | private endHandleTouchMovement = (event: TouchEvent) => {
|
169 | this.handleMoveEndedAt(this.touchEventClientOffset(event));
|
170 | };
|
171 |
|
172 | private handleMoveEndedAt = (clientPixel: number) => {
|
173 | this.removeDocumentEventListeners();
|
174 | this.setState({ isMoving: false });
|
175 |
|
176 | const finalValue = this.changeValue(this.clientToValue(clientPixel));
|
177 | this.props.onRelease?.(finalValue);
|
178 | };
|
179 |
|
180 | private handleHandleMovement = (event: MouseEvent) => {
|
181 | this.handleMovedTo(this.mouseEventClientOffset(event));
|
182 | };
|
183 |
|
184 | private handleHandleTouchMovement = (event: TouchEvent) => {
|
185 | this.handleMovedTo(this.touchEventClientOffset(event));
|
186 | };
|
187 |
|
188 | private handleMovedTo = (clientPixel: number) => {
|
189 | if (this.state.isMoving && !this.props.disabled) {
|
190 | this.changeValue(this.clientToValue(clientPixel));
|
191 | }
|
192 | };
|
193 |
|
194 | private handleKeyDown = (event: React.KeyboardEvent<HTMLSpanElement>) => {
|
195 | const { stepSize, value } = this.props;
|
196 |
|
197 |
|
198 | const { which } = event;
|
199 | if (which === Keys.ARROW_DOWN || which === Keys.ARROW_LEFT) {
|
200 | this.changeValue(value - stepSize);
|
201 |
|
202 | event.preventDefault();
|
203 | } else if (which === Keys.ARROW_UP || which === Keys.ARROW_RIGHT) {
|
204 | this.changeValue(value + stepSize);
|
205 | event.preventDefault();
|
206 | }
|
207 | };
|
208 |
|
209 | private handleKeyUp = (event: React.KeyboardEvent<HTMLSpanElement>) => {
|
210 |
|
211 |
|
212 | if ([Keys.ARROW_UP, Keys.ARROW_DOWN, Keys.ARROW_LEFT, Keys.ARROW_RIGHT].indexOf(event.which) >= 0) {
|
213 | this.props.onRelease?.(this.props.value);
|
214 | }
|
215 | };
|
216 |
|
217 |
|
218 | private changeValue(newValue: number, callback = this.props.onChange) {
|
219 | newValue = this.clamp(newValue);
|
220 | if (!isNaN(newValue) && this.props.value !== newValue) {
|
221 | callback?.(newValue);
|
222 | }
|
223 | return newValue;
|
224 | }
|
225 |
|
226 |
|
227 | private clamp(value: number) {
|
228 | return clamp(value, this.props.min, this.props.max);
|
229 | }
|
230 |
|
231 | private getHandleElementCenterPixel(handleElement: HTMLElement) {
|
232 | const { handleMidpoint, handleOffset } = this.getHandleMidpointAndOffset(handleElement);
|
233 | return handleOffset + handleMidpoint;
|
234 | }
|
235 |
|
236 | private getHandleMidpointAndOffset(handleElement: HTMLElement, useOppositeDimension = false) {
|
237 | if (handleElement == null) {
|
238 | return { handleMidpoint: 0, handleOffset: 0 };
|
239 | }
|
240 |
|
241 | const { vertical } = this.props;
|
242 |
|
243 |
|
244 | const handleRect = handleElement.getBoundingClientRect();
|
245 |
|
246 | const sizeKey = vertical
|
247 | ? useOppositeDimension
|
248 | ? "width"
|
249 | : "height"
|
250 | : useOppositeDimension
|
251 | ? "height"
|
252 | : "width";
|
253 |
|
254 |
|
255 |
|
256 | const handleOffset = vertical ? window.innerHeight - (handleRect.top + handleRect[sizeKey]) : handleRect.left;
|
257 |
|
258 | return { handleMidpoint: handleRect[sizeKey] / 2, handleOffset };
|
259 | }
|
260 |
|
261 | private removeDocumentEventListeners() {
|
262 | document.removeEventListener("mousemove", this.handleHandleMovement);
|
263 | document.removeEventListener("mouseup", this.endHandleMovement);
|
264 | document.removeEventListener("touchmove", this.handleHandleTouchMovement);
|
265 | document.removeEventListener("touchend", this.endHandleTouchMovement);
|
266 | document.removeEventListener("touchcancel", this.endHandleTouchMovement);
|
267 | }
|
268 | }
|