UNPKG

6.06 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.Spinner = exports.SpinnerSize = void 0;
19var tslib_1 = require("tslib");
20var classnames_1 = tslib_1.__importDefault(require("classnames"));
21var React = tslib_1.__importStar(require("react"));
22var common_1 = require("../../common");
23var errors_1 = require("../../common/errors");
24var props_1 = require("../../common/props");
25var utils_1 = require("../../common/utils");
26var SpinnerSize;
27(function (SpinnerSize) {
28 SpinnerSize[SpinnerSize["SMALL"] = 20] = "SMALL";
29 SpinnerSize[SpinnerSize["STANDARD"] = 50] = "STANDARD";
30 SpinnerSize[SpinnerSize["LARGE"] = 100] = "LARGE";
31})(SpinnerSize = exports.SpinnerSize || (exports.SpinnerSize = {}));
32// see http://stackoverflow.com/a/18473154/3124288 for calculating arc path
33var R = 45;
34var 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);
35// unitless total length of SVG path, to which stroke-dash* properties are relative.
36// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/pathLength
37// this value is the result of `<path d={SPINNER_TRACK} />.getTotalLength()` and works in all browsers:
38var PATH_LENGTH = 280;
39var MIN_SIZE = 10;
40var STROKE_WIDTH = 4;
41var MIN_STROKE_WIDTH = 16;
42/**
43 * Spinner component.
44 *
45 * @see https://blueprintjs.com/docs/#core/components/spinner
46 */
47var Spinner = /** @class */ (function (_super) {
48 tslib_1.__extends(Spinner, _super);
49 function Spinner() {
50 return _super !== null && _super.apply(this, arguments) || this;
51 }
52 Spinner.prototype.componentDidUpdate = function (prevProps) {
53 if (prevProps.value !== this.props.value) {
54 // IE/Edge: re-render after changing value to force SVG update
55 this.forceUpdate();
56 }
57 };
58 Spinner.prototype.render = function () {
59 var _a;
60 var _b = this.props, className = _b.className, intent = _b.intent, value = _b.value, _c = _b.tagName, tagName = _c === void 0 ? "div" : _c, htmlProps = tslib_1.__rest(_b, ["className", "intent", "value", "tagName"]);
61 var size = this.getSize();
62 var classes = (0, classnames_1.default)(common_1.Classes.SPINNER, common_1.Classes.intentClass(intent), (_a = {}, _a[common_1.Classes.SPINNER_NO_SPIN] = value != null, _a), className);
63 // keep spinner track width consistent at all sizes (down to about 10px).
64 var strokeWidth = Math.min(MIN_STROKE_WIDTH, (STROKE_WIDTH * SpinnerSize.LARGE) / size);
65 var strokeOffset = PATH_LENGTH - PATH_LENGTH * (value == null ? 0.25 : (0, utils_1.clamp)(value, 0, 1));
66 // multiple DOM elements around SVG are necessary to properly isolate animation:
67 // - SVG elements in IE do not support anim/trans so they must be set on a parent HTML element.
68 // - SPINNER_ANIMATION isolates svg from parent display and is always centered inside root element.
69 return React.createElement(tagName, tslib_1.__assign({ "aria-valuemax": 100, "aria-valuemin": 0, "aria-valuenow": value === undefined ? undefined : value * 100, className: classes, role: "progressbar" }, htmlProps), React.createElement(tagName, { className: common_1.Classes.SPINNER_ANIMATION }, React.createElement("svg", { width: size, height: size, strokeWidth: strokeWidth.toFixed(2), viewBox: this.getViewBox(strokeWidth) },
70 React.createElement("path", { className: common_1.Classes.SPINNER_TRACK, d: SPINNER_TRACK }),
71 React.createElement("path", { className: common_1.Classes.SPINNER_HEAD, d: SPINNER_TRACK, pathLength: PATH_LENGTH, strokeDasharray: "".concat(PATH_LENGTH, " ").concat(PATH_LENGTH), strokeDashoffset: strokeOffset }))));
72 };
73 Spinner.prototype.validateProps = function (_a) {
74 var _b = _a.className, className = _b === void 0 ? "" : _b, size = _a.size;
75 if (size != null && (className.indexOf(common_1.Classes.SMALL) >= 0 || className.indexOf(common_1.Classes.LARGE) >= 0)) {
76 console.warn(errors_1.SPINNER_WARN_CLASSES_SIZE);
77 }
78 };
79 /**
80 * Resolve size to a pixel value.
81 * Size can be set by className, props, default, or minimum constant.
82 */
83 Spinner.prototype.getSize = function () {
84 var _a = this.props, _b = _a.className, className = _b === void 0 ? "" : _b, size = _a.size;
85 if (size == null) {
86 // allow Classes constants to determine default size.
87 if (className.indexOf(common_1.Classes.SMALL) >= 0) {
88 return SpinnerSize.SMALL;
89 }
90 else if (className.indexOf(common_1.Classes.LARGE) >= 0) {
91 return SpinnerSize.LARGE;
92 }
93 return SpinnerSize.STANDARD;
94 }
95 return Math.max(MIN_SIZE, size);
96 };
97 /** Compute viewbox such that stroked track sits exactly at edge of image frame. */
98 Spinner.prototype.getViewBox = function (strokeWidth) {
99 var radius = R + strokeWidth / 2;
100 var viewBoxX = (50 - radius).toFixed(2);
101 var viewBoxWidth = (radius * 2).toFixed(2);
102 return "".concat(viewBoxX, " ").concat(viewBoxX, " ").concat(viewBoxWidth, " ").concat(viewBoxWidth);
103 };
104 Spinner.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".Spinner");
105 return Spinner;
106}(common_1.AbstractPureComponent2));
107exports.Spinner = Spinner;
108//# sourceMappingURL=spinner.js.map
\No newline at end of file