UNPKG

5.78 kBJavaScriptView Raw
1/*
2 * Copyright 2015 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, __rest } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { AbstractPureComponent, Classes } from "../../common";
20import { SPINNER_WARN_CLASSES_SIZE } from "../../common/errors";
21import { DISPLAYNAME_PREFIX } from "../../common/props";
22import { clamp } from "../../common/utils";
23export var SpinnerSize;
24(function (SpinnerSize) {
25 SpinnerSize[SpinnerSize["SMALL"] = 20] = "SMALL";
26 SpinnerSize[SpinnerSize["STANDARD"] = 50] = "STANDARD";
27 SpinnerSize[SpinnerSize["LARGE"] = 100] = "LARGE";
28})(SpinnerSize || (SpinnerSize = {}));
29// see http://stackoverflow.com/a/18473154/3124288 for calculating arc path
30var R = 45;
31var SPINNER_TRACK = "M 50,50 m 0,-".concat(R, " a ").concat(R, ",").concat(R, " 0 1 1 0,").concat(R * 2, " a ").concat(R, ",").concat(R, " 0 1 1 0,-").concat(R * 2);
32// unitless total length of SVG path, to which stroke-dash* properties are relative.
33// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/pathLength
34// this value is the result of `<path d={SPINNER_TRACK} />.getTotalLength()` and works in all browsers:
35var PATH_LENGTH = 280;
36var MIN_SIZE = 10;
37var STROKE_WIDTH = 4;
38var MIN_STROKE_WIDTH = 16;
39/**
40 * Spinner component.
41 *
42 * @see https://blueprintjs.com/docs/#core/components/spinner
43 */
44var Spinner = /** @class */ (function (_super) {
45 __extends(Spinner, _super);
46 function Spinner() {
47 return _super !== null && _super.apply(this, arguments) || this;
48 }
49 Spinner.prototype.componentDidUpdate = function (prevProps) {
50 if (prevProps.value !== this.props.value) {
51 // IE/Edge: re-render after changing value to force SVG update
52 this.forceUpdate();
53 }
54 };
55 Spinner.prototype.render = function () {
56 var _a;
57 var _b = this.props, className = _b.className, intent = _b.intent, value = _b.value, _c = _b.tagName, tagName = _c === void 0 ? "div" : _c, htmlProps = __rest(_b, ["className", "intent", "value", "tagName"]);
58 var size = this.getSize();
59 var classes = classNames(Classes.SPINNER, Classes.intentClass(intent), (_a = {}, _a[Classes.SPINNER_NO_SPIN] = value != null, _a), className);
60 // keep spinner track width consistent at all sizes (down to about 10px).
61 var strokeWidth = Math.min(MIN_STROKE_WIDTH, (STROKE_WIDTH * SpinnerSize.LARGE) / size);
62 var strokeOffset = PATH_LENGTH - PATH_LENGTH * (value == null ? 0.25 : clamp(value, 0, 1));
63 // multiple DOM elements around SVG are necessary to properly isolate animation:
64 // - SVG elements in IE do not support anim/trans so they must be set on a parent HTML element.
65 // - SPINNER_ANIMATION isolates svg from parent display and is always centered inside root element.
66 return React.createElement(tagName, __assign({ "aria-label": "loading", "aria-valuemax": 100, "aria-valuemin": 0, "aria-valuenow": value === undefined ? undefined : value * 100, className: classes, role: "progressbar" }, htmlProps), React.createElement(tagName, { className: Classes.SPINNER_ANIMATION }, React.createElement("svg", { width: size, height: size, strokeWidth: strokeWidth.toFixed(2), viewBox: this.getViewBox(strokeWidth) },
67 React.createElement("path", { className: Classes.SPINNER_TRACK, d: SPINNER_TRACK }),
68 React.createElement("path", { className: Classes.SPINNER_HEAD, d: SPINNER_TRACK, pathLength: PATH_LENGTH, strokeDasharray: "".concat(PATH_LENGTH, " ").concat(PATH_LENGTH), strokeDashoffset: strokeOffset }))));
69 };
70 Spinner.prototype.validateProps = function (_a) {
71 var _b = _a.className, className = _b === void 0 ? "" : _b, size = _a.size;
72 if (size != null && (className.indexOf(Classes.SMALL) >= 0 || className.indexOf(Classes.LARGE) >= 0)) {
73 console.warn(SPINNER_WARN_CLASSES_SIZE);
74 }
75 };
76 /**
77 * Resolve size to a pixel value.
78 * Size can be set by className, props, default, or minimum constant.
79 */
80 Spinner.prototype.getSize = function () {
81 var _a = this.props, _b = _a.className, className = _b === void 0 ? "" : _b, size = _a.size;
82 if (size == null) {
83 // allow Classes constants to determine default size.
84 if (className.indexOf(Classes.SMALL) >= 0) {
85 return SpinnerSize.SMALL;
86 }
87 else if (className.indexOf(Classes.LARGE) >= 0) {
88 return SpinnerSize.LARGE;
89 }
90 return SpinnerSize.STANDARD;
91 }
92 return Math.max(MIN_SIZE, size);
93 };
94 /** Compute viewbox such that stroked track sits exactly at edge of image frame. */
95 Spinner.prototype.getViewBox = function (strokeWidth) {
96 var radius = R + strokeWidth / 2;
97 var viewBoxX = (50 - radius).toFixed(2);
98 var viewBoxWidth = (radius * 2).toFixed(2);
99 return "".concat(viewBoxX, " ").concat(viewBoxX, " ").concat(viewBoxWidth, " ").concat(viewBoxWidth);
100 };
101 Spinner.displayName = "".concat(DISPLAYNAME_PREFIX, ".Spinner");
102 return Spinner;
103}(AbstractPureComponent));
104export { Spinner };
105//# sourceMappingURL=spinner.js.map
\No newline at end of file