@gravity-ui/uikit
Version:
Gravity UI base styling and components
48 lines (47 loc) • 1.81 kB
JavaScript
'use client';
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MobileProvider = MobileProvider;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const MobileContext_1 = require("./MobileContext.js");
const constants_1 = require("./constants.js");
function useHistoryMock() {
return { action: '', replace() { }, push() { }, goBack() { } };
}
function useLocationMock() {
return { pathname: '', search: '', hash: '' };
}
function MobileProvider({ mobile = false, platform = constants_1.Platform.BROWSER, useHistory = useHistoryMock, useLocation = useLocationMock, children, }) {
const useHistoryFunction = React.useCallback(function useHistoryFunction() {
const { goBack, back, ...props } = useHistory();
let goBackFunction;
if (typeof goBack === 'function') {
goBackFunction = goBack;
}
else if (typeof back === 'function') {
goBackFunction = back;
}
else {
goBackFunction = () => { };
}
return {
...props,
goBack: goBackFunction,
};
}, [useHistory]);
React.useEffect(() => {
document.body.classList.toggle(constants_1.rootMobileClassName, mobile);
}, [constants_1.rootMobileClassName, mobile]);
const contextValue = React.useMemo(() => {
return {
mobile,
platform,
useLocation,
useHistory: useHistoryFunction,
};
}, [mobile, platform, useLocation, useHistoryFunction]);
return (0, jsx_runtime_1.jsx)(MobileContext_1.MobileContext.Provider, { value: contextValue, children: children });
}
//# sourceMappingURL=MobileProvider.js.map