UNPKG

@itznevikat/router

Version:

πŸ“¦ ΠœΠ½ΠΎΠ³ΠΎΡ„ΡƒΠ½ΠΊΡ†ΠΈΠΎΠ½Π°Π»ΡŒΠ½Ρ‹ΠΉ Ρ€ΠΎΡƒΡ‚Π΅Ρ€ для ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΉ Π½Π° React ΠΈ VKUI

2 lines (1 loc) β€’ 6.68 kB
var e=require("history"),t=require("react"),n=require("react-children-utilities"),r=require("@vkontakte/vkui"),o=require("@vkontakte/vkui/dist/lib/getNavId"),a=require("@vkontakte/vk-bridge");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=/*#__PURE__*/i(t),l=/*#__PURE__*/i(a),c=e.createHashHistory({window:window});function s(){return c.back()}function f(){return f=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},f.apply(this,arguments)}function p(e){if(t.isValidElement(e))return e.props["data-node-id"]}function d(e){if(t.isValidElement(e))return o.getNavId(e.props)}function v(e,n,r){var o={},a=r.split(/(?=\/)/),i=a.shift();return function e(r,u){if(t.isValidElement(r)&&t.isValidElement(u)){var l=d(r),c=p(u);if(l&&c&&i===l){var s=n.find(function(e){return e.nodeID===c});null!=s&&s.availableTransitionIDs.includes(l)&&(o[c]=l,i=a.shift())}r.props.children&&t.Children.toArray(r.props.children).forEach(function(t){return e(t,l?r:u)})}}(e,e),o}var h=location;function m(){var e;return null!=(e=Object.fromEntries(new URLSearchParams(c.location.search.slice(1))))?e:{}}function y(){var e=t.useState(m),n=e[0],r=e[1];return t.useEffect(function(){return c.listen(function(){return r(m)})},[]),n}function E(){var e,t;return null!=(e=null==(t=c.location.state)?void 0:t.meta)?e:{}}function b(){return c.location}var x=null;function k(){return t.useContext(r.ConfigProviderContext).platform===r.Platform.VKCOM?exports.Style.DESKTOP:exports.Style.MOBILE}var w,S=function(e){return u.default.createElement(r.View,f({},e),e.children)},M=function(e){return u.default.createElement(r.Root,f({},e),e.children)},P=function(e){return u.default.createElement(r.Epic,f({},e),e.children)},R=t.createContext({});exports.Style=void 0,(w=exports.Style||(exports.Style={})).MOBILE="MOBILE",w.DESKTOP="DESKTOP",exports.Epic=P,exports.Match=function(o){var a=o.children,i=o.style,s=o.initialURL,h=o.fallbackURL,m=o.disableSetLocation,y=t.useState()[1],E=t.useRef(!0),b=t.useMemo(function(){return function(e){return n.deepMap(e,function(e){var o;if(!t.isValidElement(e))return e;if(!d(e)){switch(e.type){case S:case M:case P:console.warn("[router] found known navigation layout but no `nav` property. Maybe you forgot about it?");break;case r.View:case r.Root:case r.Epic:console.warn("[router] use View, Root and Epic imported from the router to work correctly.")}return e}return t.cloneElement(e,f({},e.props,((o={})["data-node-id"]=function(e){var r="";return n.deepForEach(e,function(e){var n;t.isValidElement(e)&&(r+=null!=(n=e.key)?n:e.type.name)}),r}(e),o)))})[0]}(a)},[a]),x=t.useMemo(function(){return function(e){var r=[];return n.deepForEach(e,function(e){if(t.isValidElement(e)){var n,o=d(e);if(!o)return e;switch(e.type){case S:n="view";break;case M:n="root";break;case P:n="epic";break;default:return e}var a=t.Children.toArray(e.props.children).map(function(e){return t.isValidElement(e)&&d(e)}).filter(function(e){return!!e});r.unshift(function(e,t,n,r){return{type:e,navID:t,nodeID:r,availableTransitionIDs:n,transitions:[n[0]]}}(n,o,a,e.props["data-node-id"]))}}),r}(b)},[]),w=E.current&&null!=s?s:c.location.pathname,I=null!=i?i:k();return t.useEffect(function(){var t=c.listen(function(t){var n,r,o,a=t.location,i=t.action,u=a.state,s=v(b,x,a.pathname);if(0===Object.keys(s).length&&(console.warn("[router] route not found."),h))return(null==u||null==(n=u.meta)?void 0:n.retry)>0?console.error("[router] fallback route not found."):c.replace(h,{force:!0,meta:{from:e.createPath(a),retry:null!=u&&null!=(r=u.meta)&&r.retry?(null==u||null==(o=u.meta)?void 0:o.retry)+1:1,meta:null==u?void 0:u.meta}});null!=u&&u.force&&(i=e.Action.Push),x.forEach(function(t){var n,r=t.transitions,o=null!=(n=s[t.nodeID])?n:"/";r[r.length-1]!==o&&(i===e.Action.Push&&r.push(o),i===e.Action.Replace&&r.splice(-1,1,o),i===e.Action.Pop&&(r.includes(o)?r.pop():r.push(o)))}),m||function(t){l.default.send("VKWebAppSetLocation",{location:e.createPath(t),replace_state:!0})}(a),y({})}),n=e.createPath(c.location);return s?s!==n&&(w=s,c.replace(s)):c.location.search.slice(1)?(c.replace(c.location.pathname),c.push(n)):c.location.pathname.slice(1)&&(c.replace("/"),c.push(n)),E.current=!1,t},[]),u.default.createElement(R.Provider,{value:{root:b,navs:x,style:I,initialURL:s,fallbackURL:h,disableSetLocation:m}},function(e,r,o,a){var i=v(r,o,e);return n.deepMap(r,function(e){var n;if(!t.isValidElement(e))return e;var r=p(e);if(!r)return e;var u=null!=(n=i[r])?n:"/",l=f({},e.props);switch(e.type){case S:if(l.activePanel=u,a===exports.Style.MOBILE){var s=o.find(function(e){return e.nodeID===r});l.history=s.transitions,l.onSwipeBack=c.back}break;case M:l.activeView=u;break;case P:l.activeStory=u;break;default:return e}return t.cloneElement(e,l)})[0]}(w,b,x,I))},exports.MatchContext=R,exports.ModalRoot=function(e){var t=y().modal;return u.default.createElement(r.ModalRoot,f({activeModal:void 0===t?null:t,onClose:s},e),e.children)},exports.Root=M,exports.View=S,exports.back=s,exports.block=function(e){return c.block(e)},exports.forward=function(){return c.forward()},exports.go=function(e){return c.go(e)},exports.listen=function(e){return c.listen(e)},exports.matchPopout=function(e,t){var n;return e&&null!=(n=t.find(function(t){return d(t)===e}))?n:null},exports.push=function(e,t){return c.push(e,{meta:t})},exports.replace=function(e,t){return c.replace(e,{meta:t})},exports.useActionRef=function(e){var n=t.useCallback(function(t){x=t,e&&e(x)},[e]),r=t.useCallback(function(t){x=null==t?void 0:t.target,e&&e(x)},[e]);return{actionRef:x,setActionRef:n,setActionRefHandler:r}},exports.useDeserialized=function(){var e=t.useContext(R),n=e.root,r=e.navs,o=t.useMemo(function(){return v(n,r,c.location.pathname)},[n,c.location.pathname]),a=t.useMemo(function(){var e,t,n,a,i,u,l=null!=(e=null==(t=r.find(function(e){var t=e.type;return"root"===t||"epic"===t}))?void 0:t.nodeID)?e:"/",c=null!=(n=o[l])?n:"/",s=null!=(a=null==(i=r.find(function(e){return"view"===e.type&&e.navID===c}))?void 0:i.nodeID)?a:"/",f=null!=(u=o[s])?u:"/";return{view:c,panel:f}},[n,r,c.location.pathname]);return f({},o,{view:a.view,panel:a.panel})},exports.useInitialLocation=function(){return h},exports.useLocation=function(){var e=t.useState(b),n=e[0],r=e[1];return t.useEffect(function(){return c.listen(function(){return r(b)})},[]),n},exports.useMeta=function(){var n=t.useState(E),r=n[0],o=n[1];return t.useEffect(function(){return c.listen(function(t){[e.Action.Push,e.Action.Replace].includes(t.action)&&o(E)})},[]),r},exports.useParams=y,exports.useStyle=k;