@jswork/react-pwa-promotion 1.0.1
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 +12 -0
- package/dist/index.cjs.js +4 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/style.css +2 -0
- package/dist/style.css.map +1 -0
- package/dist/style.scss +48 -0
- package/global.d.ts +3 -0
- package/package.json +43 -0
- package/src/index.tsx +88 -0
- package/src/style.scss +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var f=Object.defineProperty;var r=Object.getOwnPropertySymbols;var m=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var d=(a,t,e)=>t in a?f(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,N=(a,t)=>{for(var e in t||(t={}))m.call(t,e)&&d(a,e,t[e]);if(r)for(var e of r(t))v.call(t,e)&&d(a,e,t[e]);return a};var o=(a,t)=>f(a,"name",{value:t,configurable:!0});var b=(a,t)=>{var e={};for(var s in a)m.call(a,s)&&t.indexOf(s)<0&&(e[s]=a[s]);if(a!=null&&r)for(var s of r(a))t.indexOf(s)<0&&v.call(a,s)&&(e[s]=a[s]);return e};var n=(a,t,e)=>(d(a,typeof t!="symbol"?t+"":t,e),e);var _react = require('react'); var _react2 = _interopRequireDefault(_react);var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames);var _swruntime = require('@jswork/sw-runtime'); var _swruntime2 = _interopRequireDefault(_swruntime);var c="react-pwa-promotion",C={"en-US":{"update-available":"\u{1F680} New content available, click on reload button to update.",reload:"Reload",close:"Close"},"zh-CN":{"update-available":"\u6709\u65B0\u5185\u5BB9\u53EF\u7528\uFF0C\u70B9\u51FB\u91CD\u65B0\u52A0\u8F7D\u6309\u94AE\u66F4\u65B0\u3002",reload:"\u26A1\uFE0F \u91CD\u65B0\u52A0\u8F7D",close:"\u{1F9E8} \u5173\u95ED"}},i=class i extends _react2.default.Component{constructor(){super(...arguments);n(this,"state",{needRefresh:!1});n(this,"swInstance");n(this,"t",o(e=>{let{lang:s}=this.props;return C[s][e]||e},"t"));n(this,"updateServiceWorker",o(()=>{this.swInstance.applyUpdate().then(()=>{this.setNeedRefresh(!1)})},"updateServiceWorker"));n(this,"setNeedRefresh",o(e=>{this.setState({needRefresh:e})},"setNeedRefresh"))}componentDidMount(){let{disabled:e,interval:s,mute:u}=this.props;this.swInstance=_swruntime2.default.install({autoUpdate:!0,disabled:e,autoUpdateInterval:s,onAutoUpdate:function(){u||console.log("SW Event: onAutoUpdate")},onUpdateReady:function(){this.setNeedRefresh(!0)}})}render(){let h=this.props,{className:e,lang:s,interval:u,mute:k}=h,S=b(h,["className","lang","interval","mute"]),{needRefresh:U}=this.state;return U?_react2.default.createElement("div",N({className:_classnames2.default.call(void 0, c,e)},S),_react2.default.createElement("header",{className:`${c}__header`},this.t("update-available")),_react2.default.createElement("footer",{className:`${c}__footer`},_react2.default.createElement("button",{onClick:()=>this.updateServiceWorker()},this.t("reload")),_react2.default.createElement("button",{onClick:()=>this.setNeedRefresh(!1)},this.t("close")))):null}};o(i,"ReactPwaPromotion"),n(i,"defaultProps",{lang:"en-US",disabled:!1,interval:20*1e3,mute:!1});var p=i;exports.default = p;
|
|
2
|
+
|
|
3
|
+
module.exports = exports.default;
|
|
4
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":["React","cx","SwRuntime","CLASS_NAME","locals","ReactPwaPromotion","Component","state","needRefresh","swInstance","t","__name","inKey","lang","props","updateServiceWorker","applyUpdate","then","setNeedRefresh","setState","componentDidMount","disabled","interval","mute","install","autoUpdate","autoUpdateInterval","onAutoUpdate","console","log","onUpdateReady","render","_a","className","div","header","footer","button","onClick","_ReactPwaPromotion","__publicField","defaultProps"],"mappings":"kmBAAA,OAAOA,MAA+B,QACtC,OAAOC,MAAQ,aACf,OAAOC,MAAe,qBAEtB,IAAMC,EAAa,sBACbC,EAAS,CACb,QAAS,CACP,mBAAoB,qEACpB,OAAU,SACV,MAAS,OACX,EACA,QAAS,CACP,mBAAoB,+GACpB,OAAU,wCACV,MAAS,wBACX,CACF,EAUqBC,EAArB,MAAqBA,UAA0BL,EAAMM,SAAS,CAA9D,kCAQEC,EAAAA,aAAQ,CACNC,YAAa,EACf,GAEQC,EAAAA,mBAERC,EAAAA,SAAIC,EAACC,GAAAA,CACH,GAAM,CAAEC,KAAAA,CAAI,EAAK,KAAKC,MACtB,OAAOV,EAAOS,CAAAA,EAAOD,CAAAA,GAAUA,CACjC,EAHI,MAqBJG,EAAAA,2BAAsBJ,EAAA,IAAA,CACpB,KAAKF,WAAWO,YAAW,EAAGC,KAAK,IAAA,CACjC,KAAKC,eAAe,EAAA,CACtB,CAAA,CACF,EAJsB,wBAMtBA,EAAAA,sBAAiBP,EAACH,GAAAA,CAChB,KAAKW,SAAS,CAAEX,YAAAA,CAAY,CAAA,CAC9B,EAFiB,mBAtBjBY,mBAAoB,CAClB,GAAM,CAAEC,SAAAA,EAAUC,SAAAA,EAAUC,KAAAA,CAAI,EAAK,KAAKT,MAC1C,KAAKL,WAAaP,EAAUsB,QAAQ,CAClCC,WAAY,GACZJ,SAAAA,EACAK,mBAAoBJ,EACpBK,aAAc,UAAA,CACRJ,GACJK,QAAQC,IAAI,wBAAA,CACd,EACAC,cAAe,UAAA,CACb,KAAKZ,eAAe,EAAA,CACtB,CACF,CAAA,CACF,CAYAa,QAAS,CACP,IAAsDC,EAAA,KAAKlB,MAAnDmB,WAAAA,EAAWpB,KAAAA,EAAMS,SAAAA,EAAUC,KAAAA,CAxEvC,EAwE0DS,EAAVlB,EAAAA,EAAUkB,EAAVlB,CAApCmB,YAAWpB,OAAMS,WAAUC,SAC7B,CAAEf,YAAAA,CAAW,EAAK,KAAKD,MAE7B,OAAKC,EAGHR,EAAA,cAACkC,MAAAA,EAAAA,CAAID,UAAWhC,EAAGE,EAAY8B,CAAAA,GAAgBnB,GAC7Cd,EAAA,cAACmC,SAAAA,CAAOF,UAAW,GAAG9B,CAAAA,YAAuB,KAAKO,EAAE,kBAAA,CAAA,EACpDV,EAAA,cAACoC,SAAAA,CAAOH,UAAW,GAAG9B,CAAAA,YACpBH,EAAA,cAACqC,SAAAA,CAAOC,QAAS,IAAM,KAAKvB,oBAAmB,GAAK,KAAKL,EAAE,QAAA,CAAA,EAC3DV,EAAA,cAACqC,SAAAA,CAAOC,QAAS,IAAM,KAAKpB,eAAe,EAAA,GAAS,KAAKR,EAAE,OAAA,CAAA,CAAA,CAAA,EAPxC,IAW3B,CACF,EA7D8DC,EAAA4B,EAAA,qBAC5DC,EADmBnC,EACZoC,eAAe,CACpB5B,KAAM,QACNQ,SAAU,GACVC,SAAU,GAAK,IACfC,KAAM,EACR,GANF,IAAqBlB,EAArBkC","sourcesContent":["import React, { HTMLAttributes } from 'react';\nimport cx from 'classnames';\nimport SwRuntime from '@jswork/sw-runtime';\n\nconst CLASS_NAME = 'react-pwa-promotion';\nconst locals = {\n 'en-US': {\n 'update-available': '🚀 New content available, click on reload button to update.',\n 'reload': 'Reload',\n 'close': 'Close',\n },\n 'zh-CN': {\n 'update-available': '有新内容可用,点击重新加载按钮更新。',\n 'reload': '⚡️ 重新加载',\n 'close': '🧨 关闭',\n },\n};\n\ntype ReactPwaPromotionProps = {\n className?: string;\n lang?: string;\n disabled?: string;\n interval?: number;\n mute?: boolean;\n} & HTMLAttributes<any>;\n\nexport default class ReactPwaPromotion extends React.Component<ReactPwaPromotionProps> {\n static defaultProps = {\n lang: 'en-US',\n disabled: false,\n interval: 20 * 1000,\n mute: false,\n };\n\n state = {\n needRefresh: false,\n };\n\n private swInstance: any;\n\n t = (inKey) => {\n const { lang } = this.props;\n return locals[lang!][inKey] || inKey;\n };\n\n componentDidMount() {\n const { disabled, interval, mute } = this.props;\n this.swInstance = SwRuntime.install({\n autoUpdate: true,\n disabled,\n autoUpdateInterval: interval,\n onAutoUpdate: function () {\n if (mute) return;\n console.log('SW Event: onAutoUpdate');\n },\n onUpdateReady: function () {\n this.setNeedRefresh(true);\n },\n });\n }\n\n updateServiceWorker = () => {\n this.swInstance.applyUpdate().then(() => {\n this.setNeedRefresh(false);\n });\n };\n\n setNeedRefresh = (needRefresh: boolean) => {\n this.setState({ needRefresh });\n };\n\n render() {\n const { className, lang, interval, mute, ...props } = this.props;\n const { needRefresh } = this.state;\n\n if (!needRefresh) return null;\n\n return (\n <div className={cx(CLASS_NAME, className)} {...props}>\n <header className={`${CLASS_NAME}__header`}>{this.t('update-available')}</header>\n <footer className={`${CLASS_NAME}__footer`}>\n <button onClick={() => this.updateServiceWorker()}>{this.t('reload')}</button>\n <button onClick={() => this.setNeedRefresh(false)}>{this.t('close')}</button>\n </footer>\n </div>\n );\n }\n}\n"]}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React, { HTMLAttributes } from 'react';
|
|
2
|
+
|
|
3
|
+
type ReactPwaPromotionProps = {
|
|
4
|
+
className?: string;
|
|
5
|
+
lang?: string;
|
|
6
|
+
disabled?: string;
|
|
7
|
+
interval?: number;
|
|
8
|
+
mute?: boolean;
|
|
9
|
+
} & HTMLAttributes<any>;
|
|
10
|
+
declare class ReactPwaPromotion extends React.Component<ReactPwaPromotionProps> {
|
|
11
|
+
static defaultProps: {
|
|
12
|
+
lang: string;
|
|
13
|
+
disabled: boolean;
|
|
14
|
+
interval: number;
|
|
15
|
+
mute: boolean;
|
|
16
|
+
};
|
|
17
|
+
state: {
|
|
18
|
+
needRefresh: boolean;
|
|
19
|
+
};
|
|
20
|
+
private swInstance;
|
|
21
|
+
t: (inKey: any) => any;
|
|
22
|
+
componentDidMount(): void;
|
|
23
|
+
updateServiceWorker: () => void;
|
|
24
|
+
setNeedRefresh: (needRefresh: boolean) => void;
|
|
25
|
+
render(): React.JSX.Element | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { ReactPwaPromotion as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React, { HTMLAttributes } from 'react';
|
|
2
|
+
|
|
3
|
+
type ReactPwaPromotionProps = {
|
|
4
|
+
className?: string;
|
|
5
|
+
lang?: string;
|
|
6
|
+
disabled?: string;
|
|
7
|
+
interval?: number;
|
|
8
|
+
mute?: boolean;
|
|
9
|
+
} & HTMLAttributes<any>;
|
|
10
|
+
declare class ReactPwaPromotion extends React.Component<ReactPwaPromotionProps> {
|
|
11
|
+
static defaultProps: {
|
|
12
|
+
lang: string;
|
|
13
|
+
disabled: boolean;
|
|
14
|
+
interval: number;
|
|
15
|
+
mute: boolean;
|
|
16
|
+
};
|
|
17
|
+
state: {
|
|
18
|
+
needRefresh: boolean;
|
|
19
|
+
};
|
|
20
|
+
private swInstance;
|
|
21
|
+
t: (inKey: any) => any;
|
|
22
|
+
componentDidMount(): void;
|
|
23
|
+
updateServiceWorker: () => void;
|
|
24
|
+
setNeedRefresh: (needRefresh: boolean) => void;
|
|
25
|
+
render(): React.JSX.Element | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export = ReactPwaPromotion;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var f=Object.defineProperty;var r=Object.getOwnPropertySymbols;var m=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var d=(a,t,e)=>t in a?f(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,N=(a,t)=>{for(var e in t||(t={}))m.call(t,e)&&d(a,e,t[e]);if(r)for(var e of r(t))v.call(t,e)&&d(a,e,t[e]);return a};var o=(a,t)=>f(a,"name",{value:t,configurable:!0});var b=(a,t)=>{var e={};for(var s in a)m.call(a,s)&&t.indexOf(s)<0&&(e[s]=a[s]);if(a!=null&&r)for(var s of r(a))t.indexOf(s)<0&&v.call(a,s)&&(e[s]=a[s]);return e};var n=(a,t,e)=>(d(a,typeof t!="symbol"?t+"":t,e),e);import l from"react";import E from"classnames";import R from"@jswork/sw-runtime";var c="react-pwa-promotion",C={"en-US":{"update-available":"\u{1F680} New content available, click on reload button to update.",reload:"Reload",close:"Close"},"zh-CN":{"update-available":"\u6709\u65B0\u5185\u5BB9\u53EF\u7528\uFF0C\u70B9\u51FB\u91CD\u65B0\u52A0\u8F7D\u6309\u94AE\u66F4\u65B0\u3002",reload:"\u26A1\uFE0F \u91CD\u65B0\u52A0\u8F7D",close:"\u{1F9E8} \u5173\u95ED"}},i=class i extends l.Component{constructor(){super(...arguments);n(this,"state",{needRefresh:!1});n(this,"swInstance");n(this,"t",o(e=>{let{lang:s}=this.props;return C[s][e]||e},"t"));n(this,"updateServiceWorker",o(()=>{this.swInstance.applyUpdate().then(()=>{this.setNeedRefresh(!1)})},"updateServiceWorker"));n(this,"setNeedRefresh",o(e=>{this.setState({needRefresh:e})},"setNeedRefresh"))}componentDidMount(){let{disabled:e,interval:s,mute:u}=this.props;this.swInstance=R.install({autoUpdate:!0,disabled:e,autoUpdateInterval:s,onAutoUpdate:function(){u||console.log("SW Event: onAutoUpdate")},onUpdateReady:function(){this.setNeedRefresh(!0)}})}render(){let h=this.props,{className:e,lang:s,interval:u,mute:k}=h,S=b(h,["className","lang","interval","mute"]),{needRefresh:U}=this.state;return U?l.createElement("div",N({className:E(c,e)},S),l.createElement("header",{className:`${c}__header`},this.t("update-available")),l.createElement("footer",{className:`${c}__footer`},l.createElement("button",{onClick:()=>this.updateServiceWorker()},this.t("reload")),l.createElement("button",{onClick:()=>this.setNeedRefresh(!1)},this.t("close")))):null}};o(i,"ReactPwaPromotion"),n(i,"defaultProps",{lang:"en-US",disabled:!1,interval:20*1e3,mute:!1});var p=i;export{p as default};
|
|
2
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"sourcesContent":["import React, { HTMLAttributes } from 'react';\nimport cx from 'classnames';\nimport SwRuntime from '@jswork/sw-runtime';\n\nconst CLASS_NAME = 'react-pwa-promotion';\nconst locals = {\n 'en-US': {\n 'update-available': '🚀 New content available, click on reload button to update.',\n 'reload': 'Reload',\n 'close': 'Close',\n },\n 'zh-CN': {\n 'update-available': '有新内容可用,点击重新加载按钮更新。',\n 'reload': '⚡️ 重新加载',\n 'close': '🧨 关闭',\n },\n};\n\ntype ReactPwaPromotionProps = {\n className?: string;\n lang?: string;\n disabled?: string;\n interval?: number;\n mute?: boolean;\n} & HTMLAttributes<any>;\n\nexport default class ReactPwaPromotion extends React.Component<ReactPwaPromotionProps> {\n static defaultProps = {\n lang: 'en-US',\n disabled: false,\n interval: 20 * 1000,\n mute: false,\n };\n\n state = {\n needRefresh: false,\n };\n\n private swInstance: any;\n\n t = (inKey) => {\n const { lang } = this.props;\n return locals[lang!][inKey] || inKey;\n };\n\n componentDidMount() {\n const { disabled, interval, mute } = this.props;\n this.swInstance = SwRuntime.install({\n autoUpdate: true,\n disabled,\n autoUpdateInterval: interval,\n onAutoUpdate: function () {\n if (mute) return;\n console.log('SW Event: onAutoUpdate');\n },\n onUpdateReady: function () {\n this.setNeedRefresh(true);\n },\n });\n }\n\n updateServiceWorker = () => {\n this.swInstance.applyUpdate().then(() => {\n this.setNeedRefresh(false);\n });\n };\n\n setNeedRefresh = (needRefresh: boolean) => {\n this.setState({ needRefresh });\n };\n\n render() {\n const { className, lang, interval, mute, ...props } = this.props;\n const { needRefresh } = this.state;\n\n if (!needRefresh) return null;\n\n return (\n <div className={cx(CLASS_NAME, className)} {...props}>\n <header className={`${CLASS_NAME}__header`}>{this.t('update-available')}</header>\n <footer className={`${CLASS_NAME}__footer`}>\n <button onClick={() => this.updateServiceWorker()}>{this.t('reload')}</button>\n <button onClick={() => this.setNeedRefresh(false)}>{this.t('close')}</button>\n </footer>\n </div>\n );\n }\n}\n"],"mappings":"kmBAAA,OAAOA,MAA+B,QACtC,OAAOC,MAAQ,aACf,OAAOC,MAAe,qBAEtB,IAAMC,EAAa,sBACbC,EAAS,CACb,QAAS,CACP,mBAAoB,qEACpB,OAAU,SACV,MAAS,OACX,EACA,QAAS,CACP,mBAAoB,+GACpB,OAAU,wCACV,MAAS,wBACX,CACF,EAUqBC,EAArB,MAAqBA,UAA0BC,EAAMC,SAAS,CAA9D,kCAQEC,EAAAA,aAAQ,CACNC,YAAa,EACf,GAEQC,EAAAA,mBAERC,EAAAA,SAAIC,EAACC,GAAAA,CACH,GAAM,CAAEC,KAAAA,CAAI,EAAK,KAAKC,MACtB,OAAOX,EAAOU,CAAAA,EAAOD,CAAAA,GAAUA,CACjC,EAHI,MAqBJG,EAAAA,2BAAsBJ,EAAA,IAAA,CACpB,KAAKF,WAAWO,YAAW,EAAGC,KAAK,IAAA,CACjC,KAAKC,eAAe,EAAA,CACtB,CAAA,CACF,EAJsB,wBAMtBA,EAAAA,sBAAiBP,EAACH,GAAAA,CAChB,KAAKW,SAAS,CAAEX,YAAAA,CAAY,CAAA,CAC9B,EAFiB,mBAtBjBY,mBAAoB,CAClB,GAAM,CAAEC,SAAAA,EAAUC,SAAAA,EAAUC,KAAAA,CAAI,EAAK,KAAKT,MAC1C,KAAKL,WAAae,EAAUC,QAAQ,CAClCC,WAAY,GACZL,SAAAA,EACAM,mBAAoBL,EACpBM,aAAc,UAAA,CACRL,GACJM,QAAQC,IAAI,wBAAA,CACd,EACAC,cAAe,UAAA,CACb,KAAKb,eAAe,EAAA,CACtB,CACF,CAAA,CACF,CAYAc,QAAS,CACP,IAAsDC,EAAA,KAAKnB,MAAnDoB,WAAAA,EAAWrB,KAAAA,EAAMS,SAAAA,EAAUC,KAAAA,CAxEvC,EAwE0DU,EAAVnB,EAAAA,EAAUmB,EAAVnB,CAApCoB,YAAWrB,OAAMS,WAAUC,SAC7B,CAAEf,YAAAA,CAAW,EAAK,KAAKD,MAE7B,OAAKC,EAGHH,EAAA,cAAC8B,MAAAA,EAAAA,CAAID,UAAWE,EAAGlC,EAAYgC,CAAAA,GAAgBpB,GAC7CT,EAAA,cAACgC,SAAAA,CAAOH,UAAW,GAAGhC,CAAAA,YAAuB,KAAKQ,EAAE,kBAAA,CAAA,EACpDL,EAAA,cAACiC,SAAAA,CAAOJ,UAAW,GAAGhC,CAAAA,YACpBG,EAAA,cAACkC,SAAAA,CAAOC,QAAS,IAAM,KAAKzB,oBAAmB,GAAK,KAAKL,EAAE,QAAA,CAAA,EAC3DL,EAAA,cAACkC,SAAAA,CAAOC,QAAS,IAAM,KAAKtB,eAAe,EAAA,GAAS,KAAKR,EAAE,OAAA,CAAA,CAAA,CAAA,EAPxC,IAW3B,CACF,EA7D8DC,EAAA8B,EAAA,qBAC5DC,EADmBtC,EACZuC,eAAe,CACpB9B,KAAM,QACNQ,SAAU,GACVC,SAAU,GAAK,IACfC,KAAM,EACR,GANF,IAAqBnB,EAArBqC","names":["React","cx","SwRuntime","CLASS_NAME","locals","ReactPwaPromotion","React","Component","state","needRefresh","swInstance","t","__name","inKey","lang","props","updateServiceWorker","applyUpdate","then","setNeedRefresh","setState","componentDidMount","disabled","interval","mute","SwRuntime","install","autoUpdate","autoUpdateInterval","onAutoUpdate","console","log","onUpdateReady","render","_a","className","div","cx","header","footer","button","onClick","_ReactPwaPromotion","__publicField","defaultProps"]}
|
package/dist/style.css
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
.react-pwa-promotion{background-color:#fff;border:1px solid hsla(0,0%,53%,.333);border-radius:4px;box-shadow:3px 4px 5px 0 #eee;font-size:14px;margin:16px;min-width:280px;padding:12px;position:fixed;right:0;text-align:left;top:0;transition:all .3s;z-index:10000}.react-pwa-promotion:hover{box-shadow:3px 4px 5px 0 #ddd}.react-pwa-promotion__header{margin-bottom:10px}.react-pwa-promotion__footer{text-align:right}.react-pwa-promotion__footer button{border:1px solid hsla(0,0%,53%,.333);border-radius:2px;margin-right:5px;outline:none;padding:5px 14px;transition:all .2s}.react-pwa-promotion__footer button:hover{background:hsla(0,0%,53%,.333);border-color:hsla(0,0%,53%,.533);cursor:pointer}.react-pwa-promotion__footer button:focus{box-shadow:0 0 0 .1rem rgba(49,132,253,.5);outline:none}
|
|
2
|
+
/*# sourceMappingURL=src/style.scss.map */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["src/style.scss"],"names":[],"mappings":"AAAA,qBAYE,qBAAuB,CALvB,oCAAmD,CACnD,iBAAkB,CAGlB,6BAA8B,CAE9B,cAAe,CARf,WAAY,CAHZ,eAAgB,CAIhB,YAAa,CALb,cAAe,CAEf,OAAQ,CAOR,eAAgB,CANhB,KAAM,CAUN,kBAAoB,CALpB,aAMF,CACA,2BACE,6BACF,CACA,6BACE,kBACF,CACA,6BACE,gBACF,CACA,oCAEE,oCAAmD,CAGnD,iBAAkB,CADlB,gBAAiB,CADjB,YAAa,CAGb,gBAAiB,CALjB,kBAMF,CACA,0CAGE,8BAA6C,CAD7C,gCAA+C,CAD/C,cAGF,CACA,0CAEE,0CAAgD,CADhD,YAEF","file":"src/style.scss","sourcesContent":[".react-pwa-promotion {\n position: fixed;\n min-width: 280px;\n right: 0;\n top: 0;\n margin: 16px;\n padding: 12px;\n border: 1px solid rgba(136, 136, 136, 0.3333333333);\n border-radius: 4px;\n z-index: 10000;\n text-align: left;\n box-shadow: 3px 4px 5px 0 #eee;\n background-color: white;\n font-size: 14px;\n transition: all 0.3s;\n}\n.react-pwa-promotion:hover {\n box-shadow: 3px 4px 5px 0 #ddd;\n}\n.react-pwa-promotion__header {\n margin-bottom: 10px;\n}\n.react-pwa-promotion__footer {\n text-align: right;\n}\n.react-pwa-promotion__footer button {\n transition: all 0.2s;\n border: 1px solid rgba(136, 136, 136, 0.3333333333);\n outline: none;\n margin-right: 5px;\n border-radius: 2px;\n padding: 5px 14px;\n}\n.react-pwa-promotion__footer button:hover {\n cursor: pointer;\n border-color: rgba(136, 136, 136, 0.5333333333);\n background: rgba(136, 136, 136, 0.3333333333);\n}\n.react-pwa-promotion__footer button:focus {\n outline: none;\n box-shadow: 0 0 0 0.1rem rgba(49, 132, 253, 0.5);\n}"]}
|
package/dist/style.scss
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
.react-pwa-promotion {
|
|
2
|
+
position: fixed;
|
|
3
|
+
min-width: 280px;
|
|
4
|
+
right: 0;
|
|
5
|
+
top: 0;
|
|
6
|
+
margin: 16px;
|
|
7
|
+
padding: 12px;
|
|
8
|
+
border: 1px solid #8885;
|
|
9
|
+
border-radius: 4px;
|
|
10
|
+
z-index: 10000;
|
|
11
|
+
text-align: left;
|
|
12
|
+
box-shadow: 3px 4px 5px 0 #eee;
|
|
13
|
+
background-color: white;
|
|
14
|
+
font-size: 14px;
|
|
15
|
+
transition: all 0.3s;
|
|
16
|
+
|
|
17
|
+
&:hover {
|
|
18
|
+
box-shadow: 3px 4px 5px 0 #ddd;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
&__header {
|
|
22
|
+
margin-bottom: 10px;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
&__footer {
|
|
26
|
+
text-align: right;
|
|
27
|
+
|
|
28
|
+
button {
|
|
29
|
+
transition: all 0.2s;
|
|
30
|
+
border: 1px solid #8885;
|
|
31
|
+
outline: none;
|
|
32
|
+
margin-right: 5px;
|
|
33
|
+
border-radius: 2px;
|
|
34
|
+
padding: 5px 14px;
|
|
35
|
+
|
|
36
|
+
&:hover {
|
|
37
|
+
cursor: pointer;
|
|
38
|
+
border-color: #8888;
|
|
39
|
+
background: #8885;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
&:focus {
|
|
43
|
+
outline: none;
|
|
44
|
+
box-shadow: 0 0 0 0.1rem rgb(49 132 253 / 50%);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
package/global.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jswork/react-pwa-promotion",
|
|
3
|
+
"description": "A vite pwa promotion component for workbox.",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"homepage": "https://js.work",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup",
|
|
12
|
+
"postbuild": "postsass -s src/style.scss -d dist/style.css -c",
|
|
13
|
+
"release": "release-it --ci --npm.skipChecks"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@swc/core": "^1.3.93",
|
|
17
|
+
"@types/react": "^18.2.28",
|
|
18
|
+
"@types/react-dom": "^18.2.13",
|
|
19
|
+
"autoprefixer": "^10.4.16",
|
|
20
|
+
"cssnano": "^6.0.1",
|
|
21
|
+
"prettier": "^3.2.4",
|
|
22
|
+
"react": "^18.2.0",
|
|
23
|
+
"react-dom": "^18.2.0",
|
|
24
|
+
"tsup": "^7.2.0",
|
|
25
|
+
"typescript": "^5.2.2"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"registry": "https://registry.npmjs.org"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@jswork/sw-runtime": "^1.0.5",
|
|
33
|
+
"classnames": "^2.5.1"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"component",
|
|
37
|
+
"pwa",
|
|
38
|
+
"react",
|
|
39
|
+
"runtime",
|
|
40
|
+
"sw",
|
|
41
|
+
"workbox"
|
|
42
|
+
]
|
|
43
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import React, { HTMLAttributes } from 'react';
|
|
2
|
+
import cx from 'classnames';
|
|
3
|
+
import SwRuntime from '@jswork/sw-runtime';
|
|
4
|
+
|
|
5
|
+
const CLASS_NAME = 'react-pwa-promotion';
|
|
6
|
+
const locals = {
|
|
7
|
+
'en-US': {
|
|
8
|
+
'update-available': '🚀 New content available, click on reload button to update.',
|
|
9
|
+
'reload': 'Reload',
|
|
10
|
+
'close': 'Close',
|
|
11
|
+
},
|
|
12
|
+
'zh-CN': {
|
|
13
|
+
'update-available': '有新内容可用,点击重新加载按钮更新。',
|
|
14
|
+
'reload': '⚡️ 重新加载',
|
|
15
|
+
'close': '🧨 关闭',
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type ReactPwaPromotionProps = {
|
|
20
|
+
className?: string;
|
|
21
|
+
lang?: string;
|
|
22
|
+
disabled?: string;
|
|
23
|
+
interval?: number;
|
|
24
|
+
mute?: boolean;
|
|
25
|
+
} & HTMLAttributes<any>;
|
|
26
|
+
|
|
27
|
+
export default class ReactPwaPromotion extends React.Component<ReactPwaPromotionProps> {
|
|
28
|
+
static defaultProps = {
|
|
29
|
+
lang: 'en-US',
|
|
30
|
+
disabled: false,
|
|
31
|
+
interval: 20 * 1000,
|
|
32
|
+
mute: false,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
state = {
|
|
36
|
+
needRefresh: false,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
private swInstance: any;
|
|
40
|
+
|
|
41
|
+
t = (inKey) => {
|
|
42
|
+
const { lang } = this.props;
|
|
43
|
+
return locals[lang!][inKey] || inKey;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
componentDidMount() {
|
|
47
|
+
const { disabled, interval, mute } = this.props;
|
|
48
|
+
this.swInstance = SwRuntime.install({
|
|
49
|
+
autoUpdate: true,
|
|
50
|
+
disabled,
|
|
51
|
+
autoUpdateInterval: interval,
|
|
52
|
+
onAutoUpdate: function () {
|
|
53
|
+
if (mute) return;
|
|
54
|
+
console.log('SW Event: onAutoUpdate');
|
|
55
|
+
},
|
|
56
|
+
onUpdateReady: function () {
|
|
57
|
+
this.setNeedRefresh(true);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
updateServiceWorker = () => {
|
|
63
|
+
this.swInstance.applyUpdate().then(() => {
|
|
64
|
+
this.setNeedRefresh(false);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
setNeedRefresh = (needRefresh: boolean) => {
|
|
69
|
+
this.setState({ needRefresh });
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
render() {
|
|
73
|
+
const { className, lang, interval, mute, ...props } = this.props;
|
|
74
|
+
const { needRefresh } = this.state;
|
|
75
|
+
|
|
76
|
+
if (!needRefresh) return null;
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div className={cx(CLASS_NAME, className)} {...props}>
|
|
80
|
+
<header className={`${CLASS_NAME}__header`}>{this.t('update-available')}</header>
|
|
81
|
+
<footer className={`${CLASS_NAME}__footer`}>
|
|
82
|
+
<button onClick={() => this.updateServiceWorker()}>{this.t('reload')}</button>
|
|
83
|
+
<button onClick={() => this.setNeedRefresh(false)}>{this.t('close')}</button>
|
|
84
|
+
</footer>
|
|
85
|
+
</div>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/style.scss
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
.react-pwa-promotion {
|
|
2
|
+
position: fixed;
|
|
3
|
+
min-width: 280px;
|
|
4
|
+
right: 0;
|
|
5
|
+
top: 0;
|
|
6
|
+
margin: 16px;
|
|
7
|
+
padding: 12px;
|
|
8
|
+
border: 1px solid #8885;
|
|
9
|
+
border-radius: 4px;
|
|
10
|
+
z-index: 10000;
|
|
11
|
+
text-align: left;
|
|
12
|
+
box-shadow: 3px 4px 5px 0 #eee;
|
|
13
|
+
background-color: white;
|
|
14
|
+
font-size: 14px;
|
|
15
|
+
transition: all 0.3s;
|
|
16
|
+
|
|
17
|
+
&:hover {
|
|
18
|
+
box-shadow: 3px 4px 5px 0 #ddd;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
&__header {
|
|
22
|
+
margin-bottom: 10px;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
&__footer {
|
|
26
|
+
text-align: right;
|
|
27
|
+
|
|
28
|
+
button {
|
|
29
|
+
transition: all 0.2s;
|
|
30
|
+
border: 1px solid #8885;
|
|
31
|
+
outline: none;
|
|
32
|
+
margin-right: 5px;
|
|
33
|
+
border-radius: 2px;
|
|
34
|
+
padding: 5px 14px;
|
|
35
|
+
|
|
36
|
+
&:hover {
|
|
37
|
+
cursor: pointer;
|
|
38
|
+
border-color: #8888;
|
|
39
|
+
background: #8885;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
&:focus {
|
|
43
|
+
outline: none;
|
|
44
|
+
box-shadow: 0 0 0 0.1rem rgb(49 132 253 / 50%);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|