@mopinion/survey
Version:
Collect customer feedback with the Mopinion survey library
81 lines (80 loc) • 4.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = useFormWidth;
var _hooks = require("preact/hooks");
var _utils = require("../../../utils");
var _hooks2 = require("../../../hooks");
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function getWidth(formType) {
var windowWidth = window.innerWidth;
if (windowWidth > 1201) {
return formType === 'slide' ? '425px' : '40%';
} else if (windowWidth > 992 && windowWidth < 1201) {
return formType === 'slide' ? '400px' : '45%';
} else if (windowWidth < 993 && windowWidth > 768) {
return formType === 'slide' ? '360px' : '60%';
} else if (windowWidth < 769 && windowWidth > 544) {
return formType === 'slide' ? '360px' : '70%';
} else if (windowWidth < 545 && windowWidth > 360) {
return formType === 'slide' ? window.innerWidth * 0.8 + 'px' : '90%';
}
return formType === 'slide' ? window.innerWidth - 24 + 'px' : '90%';
}
function getWidthClass(clientWidth) {
var className = '';
if (clientWidth < 360) {
className = 'mop-view-xs';
}
if (clientWidth < 400) {
className += ' mop-view-sm';
}
if (clientWidth < 480) {
className += ' mop-view-md';
}
className += ' mop-view-lg';
return className;
}
function useFormWidth(_ref) {
var _frameRef$current, _frameRef$current3;
var formType = _ref.formType,
frameRef = _ref.frameRef;
var _useState = (0, _hooks.useState)(getWidth(formType)),
_useState2 = _slicedToArray(_useState, 2),
width = _useState2[0],
setWidth = _useState2[1];
var _useState3 = (0, _hooks.useState)((_frameRef$current = frameRef.current) === null || _frameRef$current === void 0 ? void 0 : _frameRef$current.clientWidth),
_useState4 = _slicedToArray(_useState3, 2),
clientWidth = _useState4[0],
setClientWidth = _useState4[1];
var prevFormType = (0, _hooks2.usePrevious)(formType);
(0, _hooks.useEffect)(function () {
var handleResize = (0, _utils.debounce)(function () {
var _frameRef$current2;
var newWidth = getWidth(formType);
if (newWidth !== width) {
setWidth(newWidth);
}
setClientWidth((_frameRef$current2 = frameRef.current) === null || _frameRef$current2 === void 0 ? void 0 : _frameRef$current2.clientWidth);
}, 100);
window.addEventListener('resize', handleResize);
if (prevFormType && prevFormType !== formType) {
handleResize();
}
return function () {
window.removeEventListener('resize', handleResize);
};
}, [width, prevFormType, formType, frameRef]);
return {
formWidth: width,
widthClass: getWidthClass(((_frameRef$current3 = frameRef.current) === null || _frameRef$current3 === void 0 ? void 0 : _frameRef$current3.clientWidth) || clientWidth),
clientWidth: clientWidth
};
}
//# sourceMappingURL=useFormWidth.js.map