@workday/canvas-kit-react-skeleton
Version:
A component that renders a skeleton.
126 lines (125 loc) • 5.34 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var styled_1 = __importDefault(require("@emotion/styled"));
var core_1 = require("@emotion/core");
var canvas_kit_react_core_1 = __importDefault(require("@workday/canvas-kit-react-core"));
var canvas_kit_react_common_1 = require("@workday/canvas-kit-react-common");
var TRANSPARENCY_POSITION = 45;
var WHITE_SHEEN_WIDTH = 10;
var DURATION = 5;
var AccessibleHide = styled_1.default('div')(canvas_kit_react_common_1.accessibleHide);
var SkeletonAnimator = styled_1.default('div')(function (_a) {
var diagonal = _a.diagonal, topPosition = _a.topPosition, width = _a.width;
var backgroundPositionAnimation = core_1.keyframes({
from: {
left: "-" + diagonal + "px",
},
to: {
left: diagonal + width + "px",
},
});
return {
animation: backgroundPositionAnimation + " " + DURATION + "s ease-in-out infinite",
background: "linear-gradient(130deg, rgba(255,255,255,0) " + TRANSPARENCY_POSITION + "%, " + canvas_kit_react_core_1.default.colors.frenchVanilla100 + ", rgba(255,255,255,0) " + (TRANSPARENCY_POSITION + WHITE_SHEEN_WIDTH) + "%)",
backgroundRepeat: 'no-repeat',
backgroundSize: diagonal + "px " + diagonal + "px",
width: diagonal,
height: diagonal,
top: topPosition,
position: 'absolute',
};
});
var SkeletonContainer = styled_1.default('div')({
overflow: 'hidden',
width: '100%',
height: '100%',
position: 'relative',
});
var Skeleton = (function (_super) {
__extends(Skeleton, _super);
function Skeleton() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.ref = React.createRef();
_this.state = {
width: 0,
height: 0,
};
return _this;
}
Skeleton.prototype.render = function () {
var _a = this.props, _b = _a["aria-label"], loadingAriaLabel = _b === void 0 ? 'Loading' : _b, children = _a.children, elemProps = __rest(_a, ['aria-label', "children"]);
var _c = this.state, width = _c.width, height = _c.height;
var diagonal = Math.sqrt(width * width + height * height) + WHITE_SHEEN_WIDTH;
var topPosition = (-1 * (diagonal - height)) / 2;
return (React.createElement(SkeletonContainer, __assign({ ref: this.ref }, elemProps),
React.createElement(AccessibleHide, null, loadingAriaLabel),
React.createElement(SkeletonAnimator, { diagonal: diagonal, topPosition: topPosition, width: width }),
React.createElement("div", { "aria-hidden": true }, children)));
};
Skeleton.prototype.componentDidMount = function () {
if (this.ref.current) {
this.setState({
height: this.ref.current.clientHeight,
width: this.ref.current.clientWidth,
});
}
};
Skeleton.prototype.componentDidUpdate = function () {
var _a = this.state, width = _a.width, height = _a.height;
var current = this.ref.current;
if (current && (width !== current.clientWidth || height !== current.clientHeight)) {
this.setState({
height: current.clientHeight,
width: current.clientWidth,
});
}
};
return Skeleton;
}(React.Component));
exports.default = Skeleton;