UNPKG

11.6 kBJavaScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __extends } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { AbstractPureComponent, Classes, Utils } from "../../common";
20import { DISPLAYNAME_PREFIX } from "../../common/props";
21import { clamp } from "../../common/utils";
22import { formatPercentage } from "./sliderUtils";
23// props that require number values, for validation
24var NUMBER_PROPS = ["max", "min", "stepSize", "tickSize", "value"];
25/** Internal component for a Handle with click/drag/keyboard logic to determine a new value. */
26var Handle = /** @class */ (function (_super) {
27 __extends(Handle, _super);
28 function Handle() {
29 var _this = _super !== null && _super.apply(this, arguments) || this;
30 _this.state = {
31 isMoving: false,
32 };
33 _this.handleElement = null;
34 _this.refHandlers = {
35 handle: function (el) { return (_this.handleElement = el); },
36 };
37 _this.beginHandleMovement = function (event) {
38 document.addEventListener("mousemove", _this.handleHandleMovement);
39 document.addEventListener("mouseup", _this.endHandleMovement);
40 _this.setState({ isMoving: true });
41 _this.changeValue(_this.clientToValue(_this.mouseEventClientOffset(event)));
42 };
43 _this.beginHandleTouchMovement = function (event) {
44 document.addEventListener("touchmove", _this.handleHandleTouchMovement);
45 document.addEventListener("touchend", _this.endHandleTouchMovement);
46 document.addEventListener("touchcancel", _this.endHandleTouchMovement);
47 _this.setState({ isMoving: true });
48 _this.changeValue(_this.clientToValue(_this.touchEventClientOffset(event)));
49 };
50 _this.getStyleProperties = function () {
51 if (_this.handleElement == null) {
52 return {};
53 }
54 // The handle midpoint of RangeSlider is actually shifted by a margin to
55 // be on the edge of the visible handle element. Because the midpoint
56 // calculation does not take this margin into account, we instead
57 // measure the long side (which is equal to the short side plus the
58 // margin).
59 var _a = _this.props, _b = _a.min, min = _b === void 0 ? 0 : _b, tickSizeRatio = _a.tickSizeRatio, value = _a.value, vertical = _a.vertical;
60 var handleMidpoint = _this.getHandleMidpointAndOffset(_this.handleElement, true).handleMidpoint;
61 var offsetRatio = (value - min) * tickSizeRatio;
62 var offsetCalc = "calc(".concat(formatPercentage(offsetRatio), " - ").concat(handleMidpoint, "px)");
63 return vertical ? { bottom: offsetCalc } : { left: offsetCalc };
64 };
65 _this.endHandleMovement = function (event) {
66 _this.handleMoveEndedAt(_this.mouseEventClientOffset(event));
67 };
68 _this.endHandleTouchMovement = function (event) {
69 _this.handleMoveEndedAt(_this.touchEventClientOffset(event));
70 };
71 _this.handleMoveEndedAt = function (clientPixel) {
72 var _a, _b;
73 _this.removeDocumentEventListeners();
74 _this.setState({ isMoving: false });
75 // always invoke onRelease; changeValue may call onChange if value is different
76 var finalValue = _this.changeValue(_this.clientToValue(clientPixel));
77 (_b = (_a = _this.props).onRelease) === null || _b === void 0 ? void 0 : _b.call(_a, finalValue);
78 };
79 _this.handleHandleMovement = function (event) {
80 _this.handleMovedTo(_this.mouseEventClientOffset(event));
81 };
82 _this.handleHandleTouchMovement = function (event) {
83 _this.handleMovedTo(_this.touchEventClientOffset(event));
84 };
85 _this.handleMovedTo = function (clientPixel) {
86 if (_this.state.isMoving && !_this.props.disabled) {
87 _this.changeValue(_this.clientToValue(clientPixel));
88 }
89 };
90 _this.handleKeyDown = function (event) {
91 var _a = _this.props, stepSize = _a.stepSize, value = _a.value;
92 var key = event.key;
93 if (key === "ArrowDown" || key === "ArrowLeft") {
94 _this.changeValue(value - stepSize);
95 // this key event has been handled! prevent browser scroll on up/down
96 event.preventDefault();
97 }
98 else if (key === "ArrowUp" || key === "ArrowRight") {
99 _this.changeValue(value + stepSize);
100 event.preventDefault();
101 }
102 };
103 _this.handleKeyUp = function (event) {
104 var _a, _b;
105 if (Utils.isArrowKey(event)) {
106 (_b = (_a = _this.props).onRelease) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props.value);
107 }
108 };
109 return _this;
110 }
111 Handle.prototype.componentDidMount = function () {
112 // The first time this component renders, it has no ref to the handle and thus incorrectly centers the handle.
113 // Therefore, on the first mount, force a re-render to center the handle with the ref'd component.
114 this.forceUpdate();
115 };
116 Handle.prototype.render = function () {
117 var _a;
118 var _b = this.props, className = _b.className, disabled = _b.disabled, label = _b.label, min = _b.min, max = _b.max, value = _b.value, vertical = _b.vertical, htmlProps = _b.htmlProps;
119 var isMoving = this.state.isMoving;
120 return (React.createElement("span", __assign({ role: "slider", tabIndex: 0 }, htmlProps, { className: classNames(Classes.SLIDER_HANDLE, (_a = {}, _a[Classes.ACTIVE] = isMoving, _a), className), onKeyDown: disabled ? undefined : this.handleKeyDown, onKeyUp: disabled ? undefined : this.handleKeyUp, onMouseDown: disabled ? undefined : this.beginHandleMovement, onTouchStart: disabled ? undefined : this.beginHandleTouchMovement, ref: this.refHandlers.handle, style: this.getStyleProperties(), "aria-valuemin": min, "aria-valuemax": max, "aria-valuenow": value, "aria-orientation": vertical ? "vertical" : "horizontal" }), label == null ? null : React.createElement("span", { className: Classes.SLIDER_LABEL }, label)));
121 };
122 Handle.prototype.componentWillUnmount = function () {
123 this.removeDocumentEventListeners();
124 };
125 /** Convert client pixel to value between min and max. */
126 Handle.prototype.clientToValue = function (clientPixel) {
127 var _a = this.props, stepSize = _a.stepSize, tickSize = _a.tickSize, value = _a.value, vertical = _a.vertical;
128 if (this.handleElement == null) {
129 return value;
130 }
131 // #1769: this logic doesn't work perfectly when the tick size is
132 // smaller than the handle size; it may be off by a tick or two.
133 var clientPixelNormalized = vertical ? window.innerHeight - clientPixel : clientPixel;
134 var handleCenterPixel = this.getHandleElementCenterPixel(this.handleElement);
135 var pixelDelta = clientPixelNormalized - handleCenterPixel;
136 if (isNaN(pixelDelta)) {
137 return value;
138 }
139 // convert pixels to range value in increments of `stepSize`
140 return value + Math.round(pixelDelta / (tickSize * stepSize)) * stepSize;
141 };
142 Handle.prototype.mouseEventClientOffset = function (event) {
143 return this.props.vertical ? event.clientY : event.clientX;
144 };
145 Handle.prototype.touchEventClientOffset = function (event) {
146 var touch = event.changedTouches[0];
147 return this.props.vertical ? touch.clientY : touch.clientX;
148 };
149 Handle.prototype.validateProps = function (props) {
150 for (var _i = 0, NUMBER_PROPS_1 = NUMBER_PROPS; _i < NUMBER_PROPS_1.length; _i++) {
151 var prop = NUMBER_PROPS_1[_i];
152 if (typeof props[prop] !== "number") {
153 throw new Error("[Blueprint] <Handle> requires number value for ".concat(prop, " prop"));
154 }
155 }
156 };
157 /** Clamp value and invoke callback if it differs from current value */
158 Handle.prototype.changeValue = function (newValue, callback) {
159 if (callback === void 0) { callback = this.props.onChange; }
160 newValue = this.clamp(newValue);
161 if (!isNaN(newValue) && this.props.value !== newValue) {
162 callback === null || callback === void 0 ? void 0 : callback(newValue);
163 }
164 return newValue;
165 };
166 /** Clamp value between min and max props */
167 Handle.prototype.clamp = function (value) {
168 return clamp(value, this.props.min, this.props.max);
169 };
170 Handle.prototype.getHandleElementCenterPixel = function (handleElement) {
171 var _a = this.getHandleMidpointAndOffset(handleElement), handleMidpoint = _a.handleMidpoint, handleOffset = _a.handleOffset;
172 return handleOffset + handleMidpoint;
173 };
174 Handle.prototype.getHandleMidpointAndOffset = function (handleElement, useOppositeDimension) {
175 if (useOppositeDimension === void 0) { useOppositeDimension = false; }
176 if (handleElement == null) {
177 return { handleMidpoint: 0, handleOffset: 0 };
178 }
179 var vertical = this.props.vertical;
180 // N.B. element.clientHeight does not include border size.
181 // Also, element.getBoundingClientRect() is useful to get the top & left position on the page, but
182 // it fails to accurately measure element width & height inside absolutely-positioned and CSS-transformed
183 // containers like Popovers, so we use element.offsetWidth & offsetHeight instead (see https://github.com/palantir/blueprint/issues/4417).
184 var handleRect = handleElement.getBoundingClientRect();
185 handleRect.width = handleElement.offsetWidth;
186 handleRect.height = handleElement.offsetHeight;
187 var sizeKey = vertical
188 ? useOppositeDimension
189 ? "width"
190 : "height"
191 : useOppositeDimension
192 ? "height"
193 : "width";
194 // "bottom" value seems to be consistently incorrect, so explicitly
195 // calculate it using the window offset instead.
196 var handleOffset = vertical ? window.innerHeight - (handleRect.top + handleRect[sizeKey]) : handleRect.left;
197 return { handleMidpoint: handleRect[sizeKey] / 2, handleOffset: handleOffset };
198 };
199 Handle.prototype.removeDocumentEventListeners = function () {
200 document.removeEventListener("mousemove", this.handleHandleMovement);
201 document.removeEventListener("mouseup", this.endHandleMovement);
202 document.removeEventListener("touchmove", this.handleHandleTouchMovement);
203 document.removeEventListener("touchend", this.endHandleTouchMovement);
204 document.removeEventListener("touchcancel", this.endHandleTouchMovement);
205 };
206 Handle.displayName = "".concat(DISPLAYNAME_PREFIX, ".SliderHandle");
207 return Handle;
208}(AbstractPureComponent));
209export { Handle };
210//# sourceMappingURL=handle.js.map
\No newline at end of file