@lemon-fe/mini-app 0.0.2

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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `app`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const app = require('app');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
@@ -0,0 +1,20 @@
1
+ .lemon-routes {
2
+ position: relative;
3
+ width: 100vw;
4
+ height: 100vh;
5
+ overflow: hidden;
6
+ }
7
+
8
+ .lemon-transition-enter {
9
+ transform: translate3d(100%, 0, 0);
10
+ }
11
+
12
+ .lemon-transition-enter-active {
13
+ transform: translate3d(0, 0, 0);
14
+ box-shadow: 0 0 20px #eee;
15
+ }
16
+
17
+ .lemon-transition-exit {
18
+ transform: translate3d(100%, 0, 0);
19
+ box-shadow: 0 0 20px #eee;
20
+ }
@@ -0,0 +1,9 @@
1
+ import { ReactElement } from 'react';
2
+ import type { NavigationStackScreen } from '../../typings';
3
+ import './index.css';
4
+ interface Props {
5
+ pages: NavigationStackScreen[];
6
+ children?: ReactElement | null;
7
+ }
8
+ declare function App(props: Props): JSX.Element;
9
+ export default App;
@@ -0,0 +1,143 @@
1
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
+
3
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+
5
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
6
+
7
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
8
+
9
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
10
+
11
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
12
+
13
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
+
15
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
16
+
17
+ 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."); }
18
+
19
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
20
+
21
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
22
+
23
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
24
+
25
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
26
+
27
+ import React, { cloneElement, useCallback, useEffect, useMemo, useState } from 'react';
28
+ import { CSSTransition, TransitionGroup } from 'react-transition-group';
29
+ import { parsePath } from '../../utils/path';
30
+ import { NavigationContext } from '../../context';
31
+ import Screen from '../Screen';
32
+ import { GO_BACK } from '../../constants';
33
+ import './index.css';
34
+
35
+ function App(props) {
36
+ var pages = props.pages,
37
+ children = props.children;
38
+
39
+ var _useState = useState({
40
+ routes: [{
41
+ route: pages[0]
42
+ }]
43
+ }),
44
+ _useState2 = _slicedToArray(_useState, 2),
45
+ state = _useState2[0],
46
+ setState = _useState2[1];
47
+
48
+ var navigate = useCallback(function (path) {
49
+ setState(function (prev) {
50
+ var routes = prev.routes;
51
+
52
+ var _parsePath = parsePath(path),
53
+ pathname = _parsePath.pathname,
54
+ search = _parsePath.search;
55
+
56
+ var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : undefined;
57
+ var routeIndex = routes.findIndex(function (item) {
58
+ return item.route.path === pathname;
59
+ });
60
+
61
+ if (routeIndex >= 0) {
62
+ var nextRoutes = routes.slice(0, routeIndex + 1);
63
+ nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
64
+ params: params
65
+ });
66
+ return {
67
+ routes: nextRoutes
68
+ };
69
+ }
70
+
71
+ var page = pages.find(function (item) {
72
+ return item.path === pathname;
73
+ });
74
+
75
+ if (page !== undefined) {
76
+ var _nextRoutes = _toConsumableArray(routes);
77
+
78
+ _nextRoutes.push({
79
+ route: page,
80
+ params: params
81
+ });
82
+
83
+ return {
84
+ routes: _nextRoutes
85
+ };
86
+ }
87
+
88
+ return prev;
89
+ });
90
+ }, []);
91
+ var goBack = useCallback(function () {
92
+ setState(function (prev) {
93
+ var _window$ReactNativeWe;
94
+
95
+ var nextRoutes = _toConsumableArray(prev.routes);
96
+
97
+ if (nextRoutes.length > 1) {
98
+ nextRoutes.pop();
99
+ return _objectSpread(_objectSpread({}, prev), {}, {
100
+ routes: nextRoutes
101
+ });
102
+ }
103
+
104
+ (_window$ReactNativeWe = window.ReactNativeWebView) === null || _window$ReactNativeWe === void 0 ? void 0 : _window$ReactNativeWe.postMessage(GO_BACK);
105
+ return prev;
106
+ });
107
+ }, []);
108
+ useEffect(function () {
109
+ var _window$mini;
110
+
111
+ return (_window$mini = window.mini) === null || _window$mini === void 0 ? void 0 : _window$mini.addListener(function (e) {
112
+ if (e === GO_BACK) {
113
+ goBack();
114
+ }
115
+ });
116
+ }, [goBack]);
117
+ var ctx = useMemo(function () {
118
+ return {
119
+ navigation: {
120
+ navigate: navigate,
121
+ goBack: goBack
122
+ }
123
+ };
124
+ }, [navigate, goBack]);
125
+ var screens = /*#__PURE__*/React.createElement(TransitionGroup, {
126
+ className: "lemon-routes"
127
+ }, state.routes.map(function (item, index) {
128
+ return /*#__PURE__*/React.createElement(CSSTransition, {
129
+ key: item.route.path,
130
+ timeout: 300,
131
+ classNames: "lemon-transition"
132
+ }, /*#__PURE__*/React.createElement(Screen, {
133
+ route: item.route,
134
+ params: item.params,
135
+ index: index
136
+ }));
137
+ }));
138
+ return /*#__PURE__*/React.createElement(NavigationContext.Provider, {
139
+ value: ctx
140
+ }, children ? /*#__PURE__*/cloneElement(children, {}, screens) : screens);
141
+ }
142
+
143
+ export default App;
@@ -0,0 +1,40 @@
1
+ .lemon-route {
2
+ display: flex;
3
+ flex-direction: column;
4
+ position: absolute;
5
+ top: 0;
6
+ right: 0;
7
+ bottom: 0;
8
+ left: 0;
9
+ background-color: #fff;
10
+ transition: transform 300ms;
11
+ }
12
+
13
+ .lemon-route-header-content {
14
+ position: relative;
15
+ height: 44px;
16
+ }
17
+
18
+ .lemon-route-header-left {
19
+ display: flex;
20
+ align-items: center;
21
+ position: absolute;
22
+ top: 0;
23
+ bottom: 0;
24
+ left: 0;
25
+ padding-left: 15px;
26
+ line-height: 1;
27
+ }
28
+
29
+ .lemon-route-header-center {
30
+ font-weight: bold;
31
+ font-size: 16px;
32
+ color: #333;
33
+ text-align: center;
34
+ line-height: 44px;
35
+ }
36
+
37
+ .lemon-route-body {
38
+ flex: 1;
39
+ min-height: 0;
40
+ }
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ import type { NavigationStackScreen } from '../../typings';
3
+ import './index.css';
4
+ interface Props {
5
+ route: NavigationStackScreen;
6
+ params?: Record<string, any>;
7
+ index: number;
8
+ }
9
+ declare function Screen(props: Props): JSX.Element;
10
+ declare const _default: React.MemoExoticComponent<typeof Screen>;
11
+ export default _default;
@@ -0,0 +1,52 @@
1
+ import React, { createElement, memo, useMemo } from 'react';
2
+ import { ScreenContext } from '../../context';
3
+ import { useNavigation } from '../../hooks';
4
+ import { mini } from '../../global';
5
+ import './index.css';
6
+
7
+ function Screen(props) {
8
+ var route = props.route,
9
+ params = props.params,
10
+ index = props.index;
11
+ var ctx = useMemo(function () {
12
+ return {
13
+ params: params
14
+ };
15
+ }, [params]);
16
+ var statusBarHeight = useMemo(function () {
17
+ return mini.getSystemInfoSync().statusBarHeight;
18
+ }, []);
19
+ var navigation = useNavigation();
20
+ return /*#__PURE__*/React.createElement(ScreenContext.Provider, {
21
+ value: ctx
22
+ }, /*#__PURE__*/React.createElement("div", {
23
+ className: "lemon-route"
24
+ }, /*#__PURE__*/React.createElement("div", {
25
+ className: "lemon-route-header",
26
+ style: {
27
+ paddingTop: statusBarHeight
28
+ }
29
+ }, /*#__PURE__*/React.createElement("div", {
30
+ className: "lemon-route-header-content"
31
+ }, index > 0 && /*#__PURE__*/React.createElement("div", {
32
+ className: "lemon-route-header-left",
33
+ onClick: function onClick() {
34
+ return navigation.goBack();
35
+ }
36
+ }, /*#__PURE__*/React.createElement("svg", {
37
+ width: "12",
38
+ height: "22",
39
+ viewBox: "0 0 23 41",
40
+ xmlns: "http://www.w3.org/2000/svg"
41
+ }, /*#__PURE__*/React.createElement("path", {
42
+ d: "m23.002 38-2.556 2.5L.713 21.2a.975.975 0 0 1 0-1.4L20.446.5 23.002 3 5.109 20.5 23.002 38Z",
43
+ fill: "#333",
44
+ fillRule: "evenodd"
45
+ }))), /*#__PURE__*/React.createElement("div", {
46
+ className: "lemon-route-header-center"
47
+ }, route.title))), /*#__PURE__*/React.createElement("div", {
48
+ className: "lemon-route-body"
49
+ }, /*#__PURE__*/createElement(route.screen))));
50
+ }
51
+
52
+ export default /*#__PURE__*/memo(Screen);
@@ -0,0 +1,2 @@
1
+ export { default as Screen } from './Screen';
2
+ export { default as AppContainer } from './AppContainer';
@@ -0,0 +1,2 @@
1
+ export { default as Screen } from './Screen';
2
+ export { default as AppContainer } from './AppContainer';
@@ -0,0 +1 @@
1
+ export declare const GO_BACK = "GO_BACK";
@@ -0,0 +1 @@
1
+ export var GO_BACK = 'GO_BACK';
@@ -0,0 +1,9 @@
1
+ export declare const NavigationContext: import("react").Context<{
2
+ navigation: {
3
+ navigate: (path: string) => void;
4
+ goBack: () => void;
5
+ };
6
+ }>;
7
+ export declare const ScreenContext: import("react").Context<{
8
+ params?: Record<string, any> | undefined;
9
+ }>;
@@ -0,0 +1,8 @@
1
+ import { createContext } from 'react';
2
+ export var NavigationContext = /*#__PURE__*/createContext({
3
+ navigation: {
4
+ navigate: function navigate() {},
5
+ goBack: function goBack() {}
6
+ }
7
+ });
8
+ export var ScreenContext = /*#__PURE__*/createContext({});
package/es/global.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ declare global {
2
+ interface Window {
3
+ ReactNativeWebView?: {
4
+ postMessage(e: string): void;
5
+ };
6
+ mini?: {
7
+ getSystemInfoSync(): {
8
+ statusBarHeight: number;
9
+ pixelRatio: number;
10
+ safeArea: {
11
+ top: number;
12
+ right: number;
13
+ bottom: number;
14
+ left: number;
15
+ };
16
+ };
17
+ addListener(cb: (e: string) => void): void;
18
+ getExtraPayload(): unknown;
19
+ };
20
+ }
21
+ }
22
+ export declare const mini: {
23
+ getSystemInfoSync(): {
24
+ statusBarHeight: number;
25
+ pixelRatio: number;
26
+ safeArea: {
27
+ top: number;
28
+ right: number;
29
+ bottom: number;
30
+ left: number;
31
+ };
32
+ };
33
+ addListener(cb: (e: string) => void): void;
34
+ getExtraPayload(): unknown;
35
+ };
package/es/global.js ADDED
@@ -0,0 +1,20 @@
1
+ if (window.mini === undefined) {
2
+ window.mini = {
3
+ getSystemInfoSync: function getSystemInfoSync() {
4
+ return {
5
+ statusBarHeight: 0,
6
+ pixelRatio: window.devicePixelRatio,
7
+ safeArea: {
8
+ top: 0,
9
+ right: 0,
10
+ bottom: 0,
11
+ left: 0
12
+ }
13
+ };
14
+ },
15
+ addListener: function addListener() {},
16
+ getExtraPayload: function getExtraPayload() {}
17
+ };
18
+ }
19
+
20
+ export var mini = window.mini;
@@ -0,0 +1,5 @@
1
+ export declare function useNavigation(): {
2
+ navigate: (path: string) => void;
3
+ goBack: () => void;
4
+ };
5
+ export declare function useParams<T extends Record<string, any> | void = void>(): T;
@@ -0,0 +1,10 @@
1
+ import { useContext } from 'react';
2
+ import { NavigationContext, ScreenContext } from '../context';
3
+ export function useNavigation() {
4
+ var ctx = useContext(NavigationContext);
5
+ return ctx.navigation;
6
+ }
7
+ export function useParams() {
8
+ var ctx = useContext(ScreenContext);
9
+ return ctx.params;
10
+ }
package/es/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './global';
2
+ export { AppContainer } from './components';
3
+ export * from './hooks';
4
+ export type { NavigationStackScreen, Path } from './typings';
package/es/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './global';
2
+ export { AppContainer } from './components';
3
+ export * from './hooks';
@@ -0,0 +1,13 @@
1
+ import type { ComponentType } from 'react';
2
+
3
+ export interface NavigationStackScreen {
4
+ title: string;
5
+ path: string;
6
+ screen: ComponentType;
7
+ }
8
+
9
+ export interface Path {
10
+ pathname: string;
11
+ search: string;
12
+ hash: string;
13
+ }
@@ -0,0 +1,3 @@
1
+ import type { Path } from '../typings';
2
+ export declare function createPath({ pathname, search, hash }: Partial<Path>): string;
3
+ export declare function parsePath(path: string): Partial<Path>;
@@ -0,0 +1,36 @@
1
+ export function createPath(_ref) {
2
+ var _ref$pathname = _ref.pathname,
3
+ pathname = _ref$pathname === void 0 ? '/' : _ref$pathname,
4
+ _ref$search = _ref.search,
5
+ search = _ref$search === void 0 ? '' : _ref$search,
6
+ _ref$hash = _ref.hash,
7
+ hash = _ref$hash === void 0 ? '' : _ref$hash;
8
+ if (search && search !== '?') pathname += search.charAt(0) === '?' ? search : '?' + search;
9
+ if (hash && hash !== '#') pathname += hash.charAt(0) === '#' ? hash : '#' + hash;
10
+ return pathname;
11
+ }
12
+ export function parsePath(path) {
13
+ var parsedPath = {};
14
+
15
+ if (path) {
16
+ var hashIndex = path.indexOf('#');
17
+
18
+ if (hashIndex >= 0) {
19
+ parsedPath.hash = path.substr(hashIndex);
20
+ path = path.substr(0, hashIndex);
21
+ }
22
+
23
+ var searchIndex = path.indexOf('?');
24
+
25
+ if (searchIndex >= 0) {
26
+ parsedPath.search = path.substr(searchIndex);
27
+ path = path.substr(0, searchIndex);
28
+ }
29
+
30
+ if (path) {
31
+ parsedPath.pathname = path;
32
+ }
33
+ }
34
+
35
+ return parsedPath;
36
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@lemon-fe/mini-app",
3
+ "version": "0.0.2",
4
+ "description": "> TODO: description",
5
+ "author": "鲁盛杰 <lusj@cnlemon.net>",
6
+ "homepage": "",
7
+ "license": "ISC",
8
+ "main": "es/index.js",
9
+ "directories": {
10
+ "es": "es"
11
+ },
12
+ "files": [
13
+ "es"
14
+ ],
15
+ "publishConfig": {
16
+ "registry": "https://registry.npmjs.org"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git@gitlab.nhsoft.cn:lemon-fe/kits-next.git"
21
+ },
22
+ "scripts": {
23
+ "test": "echo \"Error: run tests from root\" && exit 1"
24
+ },
25
+ "dependencies": {
26
+ "react": "^17.0.2",
27
+ "react-transition-group": "^4.4.2"
28
+ },
29
+ "peerDependencies": {
30
+ "react": ">=17.0.2"
31
+ },
32
+ "devDependencies": {
33
+ "@types/react-transition-group": "^4.4.4"
34
+ }
35
+ }