@vetc-miniapp/ui-react 0.0.13 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/dist/ui-react/index.js +1 -1
- package/src/ui-react/hooks/use-did-hide.js +3 -3
- package/src/ui-react/hooks/use-did-hide.ts +4 -4
- package/src/ui-react/hooks/use-did-show.js +3 -3
- package/src/ui-react/hooks/use-did-show.ts +4 -4
- package/src/ui-react/hooks/use-navigate.js +9 -1
- package/src/ui-react/hooks/use-navigate.ts +10 -1
package/package.json
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see index.js.LICENSE.txt */
|
|
2
|
-
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n(require("react")):"function"==typeof define&&define.amd?define(["react"],n):"object"==typeof exports?exports["vetc-miniapp/ui-react"]=n(require("react")):e["vetc-miniapp/ui-react"]=n(e.react)}(this,e=>(()=>{"use strict";var n={155(n){n.exports=e},698(e,n){var
|
|
2
|
+
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n(require("react")):"function"==typeof define&&define.amd?define(["react"],n):"object"==typeof exports?exports["vetc-miniapp/ui-react"]=n(require("react")):e["vetc-miniapp/ui-react"]=n(e.react)}(this,e=>(()=>{"use strict";var n={155(n){n.exports=e},698(e,n){var t=Symbol.for("react.transitional.element"),r=Symbol.for("react.fragment");function o(e,n,r){var o=null;if(void 0!==r&&(o=""+r),void 0!==n.key&&(o=""+n.key),"key"in n)for(var i in r={},n)"key"!==i&&(r[i]=n[i]);else r=n;return n=r.ref,{$$typeof:t,type:e,key:o,ref:void 0!==n?n:null,props:r}}n.Fragment=r,n.jsx=o,n.jsxs=o},848(e,n,t){e.exports=t(698)}},t={};function r(e){var o=t[e];if(void 0!==o)return o.exports;var i=t[e]={exports:{}};return n[e](i,i.exports,r),i.exports}r.n=e=>{var n=e&&e.__esModule?()=>e.default:()=>e;return r.d(n,{a:n}),n},r.d=(e,n)=>{for(var t in n)r.o(n,t)&&!r.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:n[t]})},r.o=(e,n)=>Object.prototype.hasOwnProperty.call(e,n),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};r.r(o),r.d(o,{App:()=>O,appConfig:()=>h,getCurrentPage:()=>S,initRouter:()=>M,setCurrentPath:()=>x,useAppPause:()=>a,useAppResume:()=>c,useDidHide:()=>p,useDidShow:()=>f,useNavigate:()=>g});var i=r(155),u=r.n(i);function a(e){var n=(0,i.useRef)(e);(0,i.useEffect)(function(){n.current=e},[e]),(0,i.useEffect)(function(){if("undefined"!=typeof window&&window.MiniApp){var e=function(){try{n.current&&n.current()}catch(e){console.error("[useAppPause error]",e)}};return window.MiniApp.on("appPause",e),function(){window.MiniApp.off("appPause",e)}}},[])}function c(e){var n=(0,i.useRef)(e);(0,i.useEffect)(function(){n.current=e},[e]),(0,i.useEffect)(function(){if(console.warn("MiniApp: useAppResume"),console.warn(window.MiniApp),"undefined"!=typeof window&&window.MiniApp){var e=function(){try{n.current&&n.current()}catch(e){console.error("[useAppResume error]",e)}};return window.MiniApp.on("appResume",e),function(){window.MiniApp.off("appResume",e)}}},[])}function f(e,n){var t=(0,i.useRef)(n);(0,i.useEffect)(function(){t.current=n},[n]),(0,i.useEffect)(function(){if(window.MiniApp){var n=function(n){var r;(null==n?void 0:n.route)===e&&(null===(r=t.current)||void 0===r||r.call(t,n))};return window.MiniApp.on("didShow",n),function(){window.MiniApp.off("didShow",n)}}},[e])}function p(e,n){var t=(0,i.useRef)(n);(0,i.useEffect)(function(){t.current=n},[n]),(0,i.useEffect)(function(){if(window.MiniApp){var n=function(n){var r;(null==n?void 0:n.route)===e&&(null===(r=t.current)||void 0===r||r.call(t,n))};return window.MiniApp.on("didHide",n),function(){window.MiniApp.off("didHide",n)}}},[e])}function l(){var e,n,t="function"==typeof Symbol?Symbol:{},r=t.iterator||"@@iterator",o=t.toStringTag||"@@toStringTag";function i(t,r,o,i){var c=r&&r.prototype instanceof a?r:a,f=Object.create(c.prototype);return s(f,"_invoke",function(t,r,o){var i,a,c,f=0,p=o||[],l=!1,s={p:0,n:0,v:e,a:d,f:d.bind(e,4),d:function(n,t){return i=n,a=0,c=e,s.n=t,u}};function d(t,r){for(a=t,c=r,n=0;!l&&f&&!o&&n<p.length;n++){var o,i=p[n],d=s.p,v=i[2];t>3?(o=v===r)&&(c=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=e):i[0]<=d&&((o=t<2&&d<i[1])?(a=0,s.v=r,s.n=i[1]):d<v&&(o=t<3||i[0]>r||r>v)&&(i[4]=t,i[5]=r,s.n=v,a=0))}if(o||t>1)return u;throw l=!0,r}return function(o,p,v){if(f>1)throw TypeError("Generator is already running");for(l&&1===p&&d(p,v),a=p,c=v;(n=a<2?e:c)||!l;){i||(a?a<3?(a>1&&(s.n=-1),d(a,c)):s.n=c:s.v=c);try{if(f=2,i){if(a||(o="next"),n=i[o]){if(!(n=n.call(i,c)))throw TypeError("iterator result is not an object");if(!n.done)return n;c=n.value,a<2&&(a=0)}else 1===a&&(n=i.return)&&n.call(i),a<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=e}else if((n=(l=s.n<0)?c:t.call(r,s))!==u)break}catch(n){i=e,a=1,c=n}finally{f=1}}return{value:n,done:l}}}(t,o,i),!0),f}var u={};function a(){}function c(){}function f(){}n=Object.getPrototypeOf;var p=[][r]?n(n([][r]())):(s(n={},r,function(){return this}),n),d=f.prototype=a.prototype=Object.create(p);function v(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,f):(e.__proto__=f,s(e,o,"GeneratorFunction")),e.prototype=Object.create(d),e}return c.prototype=f,s(d,"constructor",f),s(f,"constructor",c),c.displayName="GeneratorFunction",s(f,o,"GeneratorFunction"),s(d),s(d,o,"Generator"),s(d,r,function(){return this}),s(d,"toString",function(){return"[object Generator]"}),(l=function(){return{w:i,m:v}})()}function s(e,n,t,r){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}s=function(e,n,t,r){function i(n,t){s(e,n,function(e){return this._invoke(n,t,e)})}n?o?o(e,n,{value:t,enumerable:!r,configurable:!r,writable:!r}):e[n]=t:(i("next",0),i("throw",1),i("return",2))},s(e,n,t,r)}function d(e,n,t,r,o,i,u){try{var a=e[i](u),c=a.value}catch(e){return void t(e)}a.done?n(c):Promise.resolve(c).then(r,o)}var v="undefined"!=typeof window,y=function(){return v&&window.flutter_inappwebview?window.flutter_inappwebview:null},w=function(){var e,n=(e=l().m(function e(n){var t,r,o,i,u=arguments;return l().w(function(e){for(;;)switch(e.p=e.n){case 0:if(t=u.length>1&&void 0!==u[1]?u[1]:{},r=y()){e.n=1;break}return e.a(2,Promise.reject(new Error("MiniApp bridge not available")));case 1:return e.p=1,e.n=2,r.callHandler("MiniAppBridge",{action:n,payload:t});case 2:return o=e.v,e.a(2,o);case 3:throw e.p=3,i=e.v,console.error("Bridge error:",i),i;case 4:return e.a(2)}},e,null,[[1,3]])}),function(){var n=this,t=arguments;return new Promise(function(r,o){var i=e.apply(n,t);function u(e){d(i,r,o,u,a,"next",e)}function a(e){d(i,r,o,u,a,"throw",e)}u(void 0)})});return function(e){return n.apply(this,arguments)}}();function g(){return function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if("number"==typeof e)return w("navigate",{type:"native",action:"pop",delta:Math.abs(e)});w("navigate",{type:"native",action:"push",route:e,params:n,options:t})}}var h={locale:"vi",theme:"light",pages:[]},m=r(848);function b(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=Array(n);t<n;t++)r[t]=e[t];return r}var A=new Map,j="/";function M(e){console.log(e),A=new Map(e.pages.map(function(e){return[e.path,e]})),console.log(A)}function S(){return A.get(j)}function x(e){j=e}function O(e){var n,t,r=e.config,o=e.children,u=(n=(0,i.useState)(null),t=2,function(e){if(Array.isArray(e))return e}(n)||function(e,n){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var r,o,i,u,a=[],c=!0,f=!1;try{if(i=(t=t.call(e)).next,0===n){if(Object(t)!==t)return;c=!1}else for(;!(c=(r=i.call(t)).done)&&(a.push(r.value),a.length!==n);c=!0);}catch(e){f=!0,o=e}finally{try{if(!c&&null!=t.return&&(u=t.return(),Object(u)!==u))return}finally{if(f)throw o}}return a}}(n,t)||function(e,n){if(e){if("string"==typeof e)return b(e,n);var t={}.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?b(e,n):void 0}}(n,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),a=u[0],c=u[1];if((0,i.useEffect)(function(){var e;if(null!=r&&null!==(e=r.pages)&&void 0!==e&&e.length){M(r);var n=window.location.pathname||"/",t=n.replace("/miniapp","")||"/";console.log(n),x(t);var o=S();console.warn(o),o||(x("/"),o=S()),c(o),w("registerAppConfig",{config:r})}},[r]),!a||!a.Component)return console.warn("MiniApp: Page not found for current route"),(0,m.jsx)(m.Fragment,{children:(0,m.jsx)("div",{})});var f=a.Component||(0,m.jsx)("div",{});return(0,m.jsxs)(m.Fragment,{children:[(0,m.jsx)(f,{}),o]})}return console.log("SDK React object:",u()),o})());
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect, useRef } from "react";
|
|
2
2
|
|
|
3
|
-
export function useDidHide(
|
|
3
|
+
export function useDidHide(route, callback) {
|
|
4
4
|
const saved = useRef(callback);
|
|
5
5
|
|
|
6
6
|
useEffect(() => {
|
|
@@ -11,7 +11,7 @@ export function useDidHide(pathname, callback) {
|
|
|
11
11
|
if (!window.MiniApp) return;
|
|
12
12
|
|
|
13
13
|
const handler = (data) => {
|
|
14
|
-
if (data?.
|
|
14
|
+
if (data?.route === route) {
|
|
15
15
|
saved.current?.(data);
|
|
16
16
|
}
|
|
17
17
|
};
|
|
@@ -21,5 +21,5 @@ export function useDidHide(pathname, callback) {
|
|
|
21
21
|
return () => {
|
|
22
22
|
window.MiniApp.off("didHide", handler);
|
|
23
23
|
};
|
|
24
|
-
}, [
|
|
24
|
+
}, [route]);
|
|
25
25
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { useEffect, useRef } from "react";
|
|
2
2
|
|
|
3
3
|
type DidHidePayload = {
|
|
4
|
-
|
|
4
|
+
route?: string;
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
export function useDidHide(
|
|
9
|
-
|
|
9
|
+
route: string,
|
|
10
10
|
callback: (data?: DidHidePayload) => void
|
|
11
11
|
): void {
|
|
12
12
|
const saved = useRef(callback);
|
|
@@ -20,7 +20,7 @@ export function useDidHide(
|
|
|
20
20
|
if (typeof window === "undefined" || !window.MiniApp) return;
|
|
21
21
|
|
|
22
22
|
const handler = (data?: DidHidePayload) => {
|
|
23
|
-
if (data?.
|
|
23
|
+
if (data?.route === route) {
|
|
24
24
|
saved.current?.(data);
|
|
25
25
|
}
|
|
26
26
|
};
|
|
@@ -30,5 +30,5 @@ export function useDidHide(
|
|
|
30
30
|
return () => {
|
|
31
31
|
window.MiniApp.off("didHide", handler);
|
|
32
32
|
};
|
|
33
|
-
}, [
|
|
33
|
+
}, [route]);
|
|
34
34
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect, useRef } from "react";
|
|
2
2
|
|
|
3
|
-
export function useDidShow(
|
|
3
|
+
export function useDidShow(route, callback) {
|
|
4
4
|
const saved = useRef(callback);
|
|
5
5
|
|
|
6
6
|
useEffect(() => {
|
|
@@ -11,7 +11,7 @@ export function useDidShow(pathname, callback) {
|
|
|
11
11
|
if (!window.MiniApp) return;
|
|
12
12
|
|
|
13
13
|
const handler = (data) => {
|
|
14
|
-
if (data?.
|
|
14
|
+
if (data?.route === route) {
|
|
15
15
|
saved.current?.(data);
|
|
16
16
|
}
|
|
17
17
|
};
|
|
@@ -21,5 +21,5 @@ export function useDidShow(pathname, callback) {
|
|
|
21
21
|
return () => {
|
|
22
22
|
window.MiniApp.off("didShow", handler);
|
|
23
23
|
};
|
|
24
|
-
}, [
|
|
24
|
+
}, [route]);
|
|
25
25
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { useEffect, useRef } from "react";
|
|
2
2
|
|
|
3
3
|
type DidShowPayload = {
|
|
4
|
-
|
|
4
|
+
route?: string;
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
export function useDidShow(
|
|
9
|
-
|
|
9
|
+
route: string,
|
|
10
10
|
callback: (data?: DidShowPayload) => void
|
|
11
11
|
): void {
|
|
12
12
|
const saved = useRef(callback);
|
|
@@ -20,7 +20,7 @@ export function useDidShow(
|
|
|
20
20
|
if (typeof window === "undefined" || !window.MiniApp) return;
|
|
21
21
|
|
|
22
22
|
const handler = (data?: DidShowPayload) => {
|
|
23
|
-
if (data?.
|
|
23
|
+
if (data?.route === route) {
|
|
24
24
|
saved.current?.(data);
|
|
25
25
|
}
|
|
26
26
|
};
|
|
@@ -30,5 +30,5 @@ export function useDidShow(
|
|
|
30
30
|
return () => {
|
|
31
31
|
window.MiniApp.off("didShow", handler);
|
|
32
32
|
};
|
|
33
|
-
}, [
|
|
33
|
+
}, [route]);
|
|
34
34
|
}
|
|
@@ -2,6 +2,14 @@ import { callHost } from '../bridge.js';
|
|
|
2
2
|
|
|
3
3
|
export function useNavigate() {
|
|
4
4
|
return (route, params = {}, options = {}) => {
|
|
5
|
-
|
|
5
|
+
// POP navigation
|
|
6
|
+
if (typeof route === "number") {
|
|
7
|
+
return callHost("navigate", {
|
|
8
|
+
type: "native",
|
|
9
|
+
action: "pop",
|
|
10
|
+
delta: Math.abs(route)
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
callHost("navigate", { type: "native", action: "push", route, params, options });
|
|
6
14
|
};
|
|
7
15
|
}
|
|
@@ -11,7 +11,7 @@ export interface NavigateOptions {
|
|
|
11
11
|
|
|
12
12
|
export type NavigateParams = Record<string, unknown>;
|
|
13
13
|
|
|
14
|
-
export type MiniAppRoute = string; // có thể đổi sang union type nếu muốn strict
|
|
14
|
+
export type MiniAppRoute = string | number; // có thể đổi sang union type nếu muốn strict
|
|
15
15
|
|
|
16
16
|
/* ================= Hook ================= */
|
|
17
17
|
|
|
@@ -21,9 +21,18 @@ export function useNavigate() {
|
|
|
21
21
|
params: NavigateParams = {},
|
|
22
22
|
options: NavigateOptions = {}
|
|
23
23
|
): void => {
|
|
24
|
+
// POP navigation
|
|
25
|
+
if (typeof route === "number") {
|
|
26
|
+
return callHost("navigate", {
|
|
27
|
+
type: "native",
|
|
28
|
+
action: "pop",
|
|
29
|
+
delta: Math.abs(route),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
24
32
|
// Gửi sang native host
|
|
25
33
|
callHost("navigate", {
|
|
26
34
|
type: "native",
|
|
35
|
+
action: "push",
|
|
27
36
|
route,
|
|
28
37
|
params,
|
|
29
38
|
options,
|