auth0-lock
Version:
Auth0 Lock
273 lines (269 loc) • 11.7 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _CSSCore = _interopRequireDefault(require("../../CSSCore"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var Slider = exports.default = /*#__PURE__*/function (_React$Component) {
function Slider(props) {
var _this;
_classCallCheck(this, Slider);
_this = _callSuper(this, Slider, [props]);
_this.state = {
children: {
current: props.children
}
};
_this.childRefs = {};
return _this;
}
// eslint-disable-next-line react/no-deprecated
_inherits(Slider, _React$Component);
return _createClass(Slider, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
// TODO: take a prop to identify what are we rendering instead of
// inferring it from children keys so we can accept more than one
// child (we are already wrapping them).
if (this.state.children.current.key != nextProps.children.key) {
this.setState({
children: {
current: nextProps.children,
prev: this.state.children.current
},
transitionName: this.props.transitionName
});
this.animate = true;
} else if (!this.timeout) {
this.setState({
children: {
current: nextProps.children
},
transitionName: nextProps.transitionName
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
var _this2 = this;
if (this.animate) {
this.animate = false;
var transitionName = this.state.transitionName;
var _this$state$children = this.state.children,
current = _this$state$children.current,
prev = _this$state$children.prev;
var reverse = this.props.reverse;
var currentComponent = this.childRefs[current.key] && this.childRefs[current.key].current;
var prevComponent = this.childRefs[prev.key] && this.childRefs[prev.key].current;
var transition = function transition(component, className, delay) {
// Get the DOM node directly from the component's node property
var node = component === null || component === void 0 ? void 0 : component.node;
if (!node) return;
var activeClassName = "".concat(className, "-active");
_CSSCore.default.addClass(node, className);
setTimeout(function () {
return _CSSCore.default.addClass(node, activeClassName);
}, 17);
if (delay) {
setTimeout(function () {
_CSSCore.default.removeClass(node, className);
_CSSCore.default.removeClass(node, activeClassName);
}, delay);
}
};
var callback = function callback(slide) {
currentComponent.componentWillSlideIn(slide);
var classNamePrefix = reverse ? 'reverse-' : '';
transition(currentComponent, "".concat(classNamePrefix).concat(transitionName, "-enter"), _this2.props.delay);
transition(prevComponent, "".concat(classNamePrefix).concat(transitionName, "-exit"));
_this2.timeout = setTimeout(function () {
var _context;
_this2.setState({
children: {
current: _this2.state.children.current
},
transitionName: _this2.props.transitionName
});
currentComponent.componentDidSlideIn((_context = _this2.props).onDidAppear.bind(_context));
_this2.props.onDidSlide();
_this2.timeout = null;
}, _this2.props.delay);
};
this.props.onWillSlide();
prevComponent.componentWillSlideOut(callback);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.timeout) clearTimeout(this.timeout);
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$state$children2 = this.state.children,
current = _this$state$children2.current,
prev = _this$state$children2.prev;
var children = prev ? [current, prev] : [current];
var childrenToRender = children.map(function (child) {
// Create a ref for this child if it doesn't exist
if (!_this3.childRefs[child.key]) {
_this3.childRefs[child.key] = /*#__PURE__*/_react.default.createRef();
}
return /*#__PURE__*/_react.default.cloneElement(/*#__PURE__*/_react.default.createElement(Child, {}, child), {
ref: _this3.childRefs[child.key],
key: child.key
});
});
return /*#__PURE__*/_react.default.createElement(this.props.component, {}, childrenToRender);
}
}]);
}(_react.default.Component);
Slider.propTypes = {
children: _propTypes.default.node.isRequired,
component: _propTypes.default.string,
delay: _propTypes.default.number.isRequired,
onDidAppear: _propTypes.default.func.isRequired,
onDidSlide: _propTypes.default.func.isRequired,
onWillSlide: _propTypes.default.func.isRequired,
reverse: _propTypes.default.bool.isRequired,
transitionName: _propTypes.default.string.isRequired
};
Slider.defaultProps = {
component: 'span',
onDidAppear: function onDidAppear() {},
onDidSlide: function onDidSlide() {},
onWillSlide: function onWillSlide() {},
reverse: false
};
var Child = /*#__PURE__*/function (_React$Component2) {
function Child(props) {
var _this4;
_classCallCheck(this, Child);
_this4 = _callSuper(this, Child, [props]);
_defineProperty(_this4, "node", void 0);
_this4.state = {
height: '',
originalHeight: '',
show: true
};
return _this4;
}
_inherits(Child, _React$Component2);
return _createClass(Child, [{
key: "componentWillSlideIn",
value: function componentWillSlideIn(slide) {
if (!this.node) return;
this.setState({
height: slide.height,
originalHeight: parseInt(window.getComputedStyle(this.node, null).height, 10),
show: false
});
}
}, {
key: "componentDidSlideIn",
value: function componentDidSlideIn(cb) {
var _this5 = this;
var _this$state = this.state,
height = _this$state.height,
originalHeight = _this$state.originalHeight;
if (height === originalHeight) {
this.setState({
show: true,
height: ''
});
cb();
} else {
this.cb = cb;
var frames = 10;
var count = 0;
var current = height;
var last = originalHeight;
var step = Math.abs(current - last) / frames;
var dir = current < last ? 1 : -1;
var dh = step * dir;
// TODO: rAF
this.t = setInterval(function () {
if (count < frames - 1) {
_this5.setState({
height: current,
animating: true
});
current += dh;
count++;
} else {
clearInterval(_this5.t);
delete _this5.t;
_this5.setState({
height: '',
show: true
});
_this5.cb();
}
}, 17);
}
}
}, {
key: "componentWillSlideOut",
value: function componentWillSlideOut(cb) {
if (!this.node) return;
var size = window.getComputedStyle(this.node, null).height;
cb({
height: parseInt(size, 10),
reverse: this.reverse
});
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.t) {
clearInterval(this.t);
this.cb();
}
}
}, {
key: "render",
value: function render() {
var _this6 = this;
var children = this.props.children;
var _this$state2 = this.state,
height = _this$state2.height,
show = _this$state2.show;
return /*#__PURE__*/_react.default.createElement("div", {
ref: function ref(node) {
return _this6.node = node;
},
style: height ? {
height: height + 'px'
} : {}
}, /*#__PURE__*/_react.default.createElement("div", {
style: {
visibility: show ? 'inherit' : 'hidden'
}
}, children));
}
}]);
}(_react.default.Component);
Child.propTypes = {
children: _propTypes.default.node.isRequired
};