Skip to content

Commit

Permalink
feat(Loading): add web support
Browse files Browse the repository at this point in the history
  • Loading branch information
rorya.lyj committed Sep 4, 2020
1 parent b4c3bed commit 877033e
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/loading/README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
加载提示的过渡效果。

## 支持
<img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px">
<img alt="browser" src="https://gw.alicdn.com/tfs/TB1uYFobGSs3KVjSZPiXXcsiVXa-200-200.svg" width="25px" height="25px" /> <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px">

## 安装

Expand All @@ -20,7 +20,7 @@ $ npm install universal-loading --save
#### 参数
| 属性 | 类型 | 默认值 | 必选 | 描述 | 支持 |
| -------- | -------- | ------ | ---- | -------- | --------------------------------------- |
| content | `String` | || 提示中的文字内容 | <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> |
| content | `String` | || 提示中的文字内容 | <img alt="browser" src="https://gw.alicdn.com/tfs/TB1uYFobGSs3KVjSZPiXXcsiVXa-200-200.svg" width="25px" height="25px" /> <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> |
| delay | `Number` | `0` | x | 延迟显示,单位 ms | <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> |
| mask | `Boolean` | `false` | x | 是否显示透明蒙层,防止触摸穿透 | <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> |

Expand Down
4 changes: 2 additions & 2 deletions packages/loading/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Load the transition effect of the prompt.

## Support
<img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px">
<img alt="browser" src="https://gw.alicdn.com/tfs/TB1uYFobGSs3KVjSZPiXXcsiVXa-200-200.svg" width="25px" height="25px" /> <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px">

## Install

Expand All @@ -20,7 +20,7 @@ Display loading prompt box.
#### Parameters
| Property | Type | Default | Required | Description | Support |
| -------- | --------- | ------- | -------- | ---------------------------------------------------------------- | --------------------------------------- |
| content | `String` | || The content in the prompt | <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> |
| content | `String` | || The content in the prompt | <img alt="browser" src="https://gw.alicdn.com/tfs/TB1uYFobGSs3KVjSZPiXXcsiVXa-200-200.svg" width="25px" height="25px" /> <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> |
| delay | `Number` | `0` | x | Delay display, unit ms | <img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> |
| mask | `Boolean` | `false` | x | Whether to display transparent mask to prevent touch penetration | <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> |

Expand Down
23 changes: 23 additions & 0 deletions packages/loading/demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable import/no-extraneous-dependencies */
// @ts-ignore
import { createElement, render } from 'rax';
import DriverUniversal from 'driver-universal';
import View from 'rax-view';
import Text from 'rax-text';
import Loading from '../src/';

const App = () => {
const handleClick = () => {
Loading.show({
content: 'Loading, please wait'
});
setTimeout(() => Loading.hide(), 3000);
};
return (
<View onClick={handleClick}>
<Text>Start loading</Text>
</View>
);
};

render(<App />, document.body, { driver: DriverUniversal });
4 changes: 2 additions & 2 deletions packages/loading/docs-template/README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
加载提示的过渡效果。

## 支持
__icon_miniapp_mp__ __icon_miniapp_wx__
__icon_web__ __icon_miniapp_mp__ __icon_miniapp_wx__

## 安装

Expand All @@ -20,7 +20,7 @@ $ npm install universal-loading --save
#### 参数
| 属性 | 类型 | 默认值 | 必选 | 描述 | 支持 |
| -------- | -------- | ------ | ---- | -------- | --------------------------------------- |
| content | `String` | || 提示中的文字内容 | __icon_miniapp_mp__ __icon_miniapp_wx__ |
| content | `String` | || 提示中的文字内容 | __icon_web__ __icon_miniapp_mp__ __icon_miniapp_wx__ |
| delay | `Number` | `0` | x | 延迟显示,单位 ms | __icon_miniapp_mp__ |
| mask | `Boolean` | `false` | x | 是否显示透明蒙层,防止触摸穿透 | __icon_miniapp_wx__ |

Expand Down
4 changes: 2 additions & 2 deletions packages/loading/docs-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Load the transition effect of the prompt.

## Support
__icon_miniapp_mp__ __icon_miniapp_wx__
__icon_web__ __icon_miniapp_mp__ __icon_miniapp_wx__

## Install

Expand All @@ -20,7 +20,7 @@ Display loading prompt box.
#### Parameters
| Property | Type | Default | Required | Description | Support |
| -------- | --------- | ------- | -------- | ---------------------------------------------------------------- | --------------------------------------- |
| content | `String` | || The content in the prompt | __icon_miniapp_mp__ __icon_miniapp_wx__ |
| content | `String` | || The content in the prompt | __icon_web__ __icon_miniapp_mp__ __icon_miniapp_wx__ |
| delay | `Number` | `0` | x | Delay display, unit ms | __icon_miniapp_mp__ |
| mask | `Boolean` | `false` | x | Whether to display transparent mask to prevent touch penetration | __icon_miniapp_wx__ |

Expand Down
3 changes: 3 additions & 0 deletions packages/loading/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
"@alib/build-scripts": "^0.1.0",
"@types/rax": "^1.0.1",
"build-plugin-rax-component": "^0.2.0",
"driver-universal": "^3.1.0",
"rax": "^1.1.0",
"rax-test-renderer": "^1.0.0",
"rax-text": "^1.3.1",
"rax-view": "^1.1.2",
"typescript": "^3.7.5"
}
}
6 changes: 5 additions & 1 deletion packages/loading/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isMiniApp, isWeChatMiniProgram } from 'universal-env';
import { isMiniApp, isWeChatMiniProgram, isWeb } from 'universal-env';
import * as miniAppModule from './miniapp/ali';
import * as weChatModule from './miniapp/wechat';
import * as webModule from './web';

import { Loading } from './types';

Expand All @@ -11,5 +12,8 @@ if (isMiniApp) {
if (isWeChatMiniProgram) {
Loading = weChatModule;
}
if (isWeb) {
Loading = webModule;
}

export default Loading;
111 changes: 111 additions & 0 deletions packages/loading/src/web/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
let loadingWin: HTMLElement | null = null;

const clsPrefix = "__universal_loading";

const styles = `.${clsPrefix} {
background-color: rgba(0, 0, 0, 0.6);
box-sizing: border-box;
max-width: 80%;
color: #ffffff;
padding: 15px;
position: fixed;
left: 50%;
bottom: 50%;
border-radius: 4px;
text-align: center;
transform: translateX(-50%);
-webkit-transform: translateX(-50%);
z-index: 9999;
outline: none;
}
.${clsPrefix}_circle {
border: 2px solid rgba(200, 200, 200, 0.5);
border-radius: 50%;
position: relative;
height: 40px;
width: 40px;
margin: auto;
}
.${clsPrefix}_circle:after {
content: "";
display: block;
border-radius: 50%;
width: 100%;
height: 100%;
position: absolute;
top: -2px;
left: -2px;
border: 2px solid;
border-color: transparent;
border-left-color: #fff;
box-sizing: content-box;
animation: ${clsPrefix}_roller 1s infinite linear;
}
.${clsPrefix}_text {
margin-top: 6px;
color: #fff;
line-height: 1.5;
font-size: 14px;
text-align: center;
font-weight: normal;
}
@keyframes ${clsPrefix}_roller {
0% {
transform: rotateZ(0);
}
100% {
transform: rotateZ(360deg);
}
}`.replace(/\n/g, "");

/*
* @param message {String}
*/
export function show({ content = "" }): Promise<any> {
if (!loadingWin) {
// create a style tag for keyframes
const style = document.createElement("style");
style.innerHTML = styles;
document.body.appendChild(style);
// create loading win
loadingWin = document.createElement("div");
loadingWin.className = clsPrefix;
loadingWin.setAttribute("role", "alert");
// support for ARIA, add tabindex for focus
// https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/tabindex
loadingWin.setAttribute("tabindex", "-1");
// add a circle element
const circle = document.createElement("div");
circle.className = `${clsPrefix}_circle`;
loadingWin.appendChild(circle);
// add text element
const text = document.createElement("div");
text.className = `${clsPrefix}_text`;
loadingWin.appendChild(text);

document.body.appendChild(loadingWin);
}

const text = loadingWin.querySelector(`.${clsPrefix}_text`) as HTMLDivElement;
if (content) {
text.style.display = "block";
text.innerText = content;
} else {
text.style.display = "none";
text.innerText = "";
}

return Promise.resolve();
}

export function hide(): Promise<any> {
return new Promise((resolve) => {
setTimeout(() => {
if (loadingWin && loadingWin.parentNode) {
loadingWin.parentNode.removeChild(loadingWin);
}
loadingWin = null;
resolve();
}, 0);
});
}

0 comments on commit 877033e

Please sign in to comment.