@jswork/react-pwa-promotion 1.0.15 → 1.0.16

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 CHANGED
@@ -1,12 +1,91 @@
1
- # react-pwa-promotion
2
- > A vite pwa promotion component for workbox.
1
+ # @jswork/react-pwa-promotion
2
+ > React PWA 版本升级提示组件(纯 UI,不绑定构建工具)。
3
+
4
+ ## 安装
3
5
 
4
- ## installation
5
6
  ```shell
6
- yarn add @jswork/react-pwa-promotion
7
+ pnpm add @jswork/react-pwa-promotion
8
+ ```
9
+
10
+ 样式是独立文件,在入口引入一次:
11
+
12
+ ```ts
13
+ import '@jswork/react-pwa-promotion/dist/style.css';
14
+ ```
15
+
16
+ ## 构建
17
+
18
+ ```shell
19
+ pnpm build
20
+ ```
21
+
22
+ - `tsup` → `dist/index.cjs.js` / `dist/index.esm.js` / `dist/index.d.ts`
23
+ - `@jswork/tsup-plugin-lightningcss`(挂在配置 `esbuildPlugins` 上)→ 构建结束时用 [Lightning CSS](https://lightningcss.dev/) 把 `src/style.css` 压缩并按 targets 降级,输出 `dist/style.css`
24
+
25
+ 插件已抽成独立仓库 `/Users/afei/github/tsup-plugin-lightningcss`,本包用 `link:../../../tsup-plugin-lightningcss` 做本地联调;插件发布到 npm 后把依赖改成版本号即可。可选参数:`entry`、`outFile`、`targets`、`minify`、`silent`,输出目录自动跟随 tsup 的 `outDir`。
26
+
27
+ `src/style.css` 用**原生 CSS Nesting** 写(嵌套更好维护),构建时按 targets 展开成扁平选择器,产物不依赖浏览器对 nesting 的支持。注意写法遵循 CSS Nesting 规范(`& .xxx` / `&.xxx`),不是 Sass 的 `&__xxx` 后缀拼接。
28
+
29
+ 组件本身不往 DOM 注入 `<style>`,样式完全由 CSS 文件控制。
30
+
31
+ ## 配合 vite-plugin-pwa
32
+
33
+ `vite.config.ts`:
34
+
35
+ ```ts
36
+ import { VitePWA } from 'vite-plugin-pwa';
37
+
38
+ export default defineConfig({
39
+ plugins: [
40
+ react(),
41
+ VitePWA({ registerType: 'prompt', injectRegister: null })
42
+ ]
43
+ });
7
44
  ```
8
45
 
9
- ## types
46
+ `src/vite-env.d.ts` 加一行:
47
+
10
48
  ```ts
11
- /// <reference types="@jswork/react-pwa-promotion/global.d.ts" />
49
+ /// <reference types="vite-plugin-pwa/client" />
12
50
  ```
51
+
52
+ `App.tsx`:
53
+
54
+ ```tsx
55
+ import { useRegisterSW } from 'virtual:pwa-register/react';
56
+ import PwaUpdatePrompt from '@jswork/react-pwa-promotion';
57
+
58
+ export default function App() {
59
+ const {
60
+ needRefresh: [needRefresh, setNeedRefresh],
61
+ updateServiceWorker
62
+ } = useRegisterSW();
63
+
64
+ return (
65
+ <PwaUpdatePrompt
66
+ open={needRefresh}
67
+ onUpdate={() => void updateServiceWorker(true)}
68
+ onDismiss={() => setNeedRefresh(false)}
69
+ />
70
+ );
71
+ }
72
+ ```
73
+
74
+ ## Props
75
+
76
+ | 名称 | 类型 | 默认值 | 说明 |
77
+ | --- | --- | --- | --- |
78
+ | `open` | `boolean` | — | 是否有新版本待更新,由宿主 SW 状态提供 |
79
+ | `onUpdate` | `() => void` | — | 点击「立即更新」,宿主负责让新 SW 接管并刷新 |
80
+ | `onDismiss` | `() => void` | — | 点击「稍后」 |
81
+ | `position` | `PwaUpdatePosition \| CSSProperties` | `'bottom-right'` | 四角,或传样式对象自定义坐标 |
82
+ | `className` | `string` | — | 追加根类名 |
83
+ | `texts` | `Partial<PwaUpdateTexts>` | 中文 | 覆盖标题 / 描述 / 两个按钮文案 |
84
+
85
+ `PwaUpdatePosition` = `'bottom-right' | 'bottom-left' | 'top-right' | 'top-left'`。
86
+
87
+ ## 特性
88
+
89
+ - 240px 紧凑卡片、无圆角、小按钮;按钮带 hover / 按下 / 键盘焦点反馈
90
+ - 自带入场动画,并尊重 `prefers-reduced-motion`
91
+ - 不依赖 `vite-plugin-pwa`,非 Vite 项目也可以自己控制 `open` 使用
package/dist/index.cjs.js CHANGED
@@ -1,4 +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 i=(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)&&i(a,e,t[e]);if(r)for(var e of r(t))v.call(t,e)&&i(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 l=(a,t,e)=>(i(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"}},d=class d extends _react2.default.Component{constructor(){super(...arguments);l(this,"state",{needRefresh:!1});l(this,"swInstance");l(this,"t",o(e=>{let{lang:s}=this.props;return C[s][e]||e},"t"));l(this,"updateServiceWorker",o(()=>{this.swInstance.applyUpdate().then(()=>{this.setNeedRefresh(!1),window.location.reload()})},"updateServiceWorker"));l(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:()=>{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(d,"ReactPwaPromotion"),l(d,"defaultProps",{lang:"en-US",disabled:!1,interval:20*1e3,mute:!1});var p=d;exports.default = p;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var u=Object.defineProperty;var p=Object.getOwnPropertySymbols;var _=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var d=(i,t,e)=>t in i?u(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,n=(i,t)=>{for(var e in t||(t={}))_.call(t,e)&&d(i,e,t[e]);if(p)for(var e of p(t))b.call(t,e)&&d(i,e,t[e]);return i};var _jsxruntime = require('react/jsx-runtime');var s="react-pwa-promotion",f={title:"\u53D1\u73B0\u65B0\u7248\u672C",description:"\u65B0\u7248\u672C\u5DF2\u5C31\u7EEA\uFF0C\u5237\u65B0\u5373\u53EF\u4F7F\u7528\u3002",update:"\u7ACB\u5373\u66F4\u65B0",dismiss:"\u7A0D\u540E"};function g({open:i,onUpdate:t,onDismiss:e,position:a="top-right",className:c,texts:m}){if(!i)return null;let o=n(n({},f),m),P=[s,typeof a=="string"?`${s}--${a}`:"",c].filter(Boolean).join(" ");return _jsxruntime.jsxs.call(void 0, "div",{className:P,style:typeof a=="string"?void 0:a,role:"dialog","aria-live":"polite","aria-label":o.title,children:[_jsxruntime.jsx.call(void 0, "div",{className:`${s}__title`,children:o.title}),_jsxruntime.jsx.call(void 0, "div",{className:`${s}__desc`,children:o.description}),_jsxruntime.jsxs.call(void 0, "div",{className:`${s}__actions`,children:[_jsxruntime.jsx.call(void 0, "button",{type:"button",className:`${s}__btn ${s}__btn--ghost`,onClick:e,children:o.dismiss}),_jsxruntime.jsx.call(void 0, "button",{type:"button",className:`${s}__btn ${s}__btn--primary`,onClick:t,children:o.update})]})]})}exports.default = g;
2
2
 
3
3
  module.exports = exports.default;
4
4
  //# sourceMappingURL=index.cjs.js.map
@@ -1 +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","window","location","reload","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,EACpBC,OAAOC,SAASC,OAAM,CACxB,CAAA,CACF,EALsB,wBAOtBH,EAAAA,sBAAiBP,EAACH,GAAAA,CAChB,KAAKc,SAAS,CAAEd,YAAAA,CAAY,CAAA,CAC9B,EAFiB,mBAvBjBe,mBAAoB,CAClB,GAAM,CAAEC,SAAAA,EAAUC,SAAAA,EAAUC,KAAAA,CAAI,EAAK,KAAKZ,MAC1C,KAAKL,WAAaP,EAAUyB,QAAQ,CAClCC,WAAY,GACZJ,SAAAA,EACAK,mBAAoBJ,EACpBK,aAAc,UAAA,CACRJ,GACJK,QAAQC,IAAI,wBAAA,CACd,EACAC,cAAe,IAAA,CACb,KAAKf,eAAe,EAAA,CACtB,CACF,CAAA,CACF,CAaAgB,QAAS,CACP,IAAsDC,EAAA,KAAKrB,MAAnDsB,WAAAA,EAAWvB,KAAAA,EAAMY,SAAAA,EAAUC,KAAAA,CAzEvC,EAyE0DS,EAAVrB,EAAAA,EAAUqB,EAAVrB,CAApCsB,YAAWvB,OAAMY,WAAUC,SAC7B,CAAElB,YAAAA,CAAW,EAAK,KAAKD,MAE7B,OAAKC,EAGHR,EAAA,cAACqC,MAAAA,EAAAA,CAAID,UAAWnC,EAAGE,EAAYiC,CAAAA,GAAgBtB,GAC7Cd,EAAA,cAACsC,SAAAA,CAAOF,UAAW,GAAGjC,CAAAA,YAAuB,KAAKO,EAAE,kBAAA,CAAA,EACpDV,EAAA,cAACuC,SAAAA,CAAOH,UAAW,GAAGjC,CAAAA,YACpBH,EAAA,cAACwC,SAAAA,CAAOC,QAAS,IAAM,KAAK1B,oBAAmB,GAAK,KAAKL,EAAE,QAAA,CAAA,EAC3DV,EAAA,cAACwC,SAAAA,CAAOC,QAAS,IAAM,KAAKvB,eAAe,EAAA,GAAS,KAAKR,EAAE,OAAA,CAAA,CAAA,CAAA,EAPxC,IAW3B,CACF,EA9D8DC,EAAA+B,EAAA,qBAC5DC,EADmBtC,EACZuC,eAAe,CACpB/B,KAAM,QACNW,SAAU,GACVC,SAAU,GAAK,IACfC,KAAM,EACR,GANF,IAAqBrB,EAArBqC","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?: boolean;\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: () => {\n this.setNeedRefresh(true);\n },\n });\n }\n\n updateServiceWorker = () => {\n this.swInstance.applyUpdate().then(() => {\n this.setNeedRefresh(false);\n window.location.reload();\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"]}
1
+ {"version":3,"sources":["../src/index.tsx"],"names":["jsx","jsxs","CLASS_NAME","defaultTexts","PwaUpdatePrompt","open","onUpdate","onDismiss","position","className","texts","t","__spreadValues","classes"],"mappings":"yVA2DM,cAAAA,EAEA,QAAAC,MAFA,oBAlCN,IAAMC,EAAa,sBAEbC,EAA+B,CACnC,MAAO,iCACP,YAAa,uFACb,OAAQ,2BACR,QAAS,cACX,EAIe,SAARC,EAAiC,CACtC,KAAAC,EACA,SAAAC,EACA,UAAAC,EACA,SAAAC,EAAW,YACX,UAAAC,EACA,MAAAC,CACF,EAAyB,CACvB,GAAI,CAACL,EAAM,OAAO,KAElB,IAAMM,EAAIC,IAAA,GAAKT,GAAiBO,GAE1BG,EAAU,CAACX,EADF,OAAOM,GAAa,SACG,GAAGN,CAAU,KAAKM,CAAQ,GAAK,GAAIC,CAAS,EAC/E,OAAO,OAAO,EACd,KAAK,GAAG,EAEX,OACER,EAAC,OACC,UAAWY,EACX,MAAO,OAAOL,GAAa,SAAW,OAAYA,EAClD,KAAK,SACL,YAAU,SACV,aAAYG,EAAE,MACd,UAAAX,EAAC,OAAI,UAAW,GAAGE,CAAU,UAAY,SAAAS,EAAE,MAAM,EACjDX,EAAC,OAAI,UAAW,GAAGE,CAAU,SAAW,SAAAS,EAAE,YAAY,EACtDV,EAAC,OAAI,UAAW,GAAGC,CAAU,YAC3B,UAAAF,EAAC,UACC,KAAK,SACL,UAAW,GAAGE,CAAU,SAASA,CAAU,eAC3C,QAASK,EACR,SAAAI,EAAE,QACL,EACAX,EAAC,UACC,KAAK,SACL,UAAW,GAAGE,CAAU,SAASA,CAAU,iBAC3C,QAASI,EACR,SAAAK,EAAE,OACL,GACF,GACF,CAEJ","sourcesContent":["import type { CSSProperties } from 'react';\n\nexport type PwaUpdatePosition = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';\n\nexport type PwaUpdateTexts = {\n title: string;\n description: string;\n update: string;\n dismiss: string;\n};\n\nexport interface PwaUpdatePromptProps {\n /** 是否有新版本待更新,由宿主的 Service Worker 状态提供 */\n open: boolean;\n /** 点击「立即更新」:宿主负责让新 SW 接管并刷新页面 */\n onUpdate: () => void;\n /** 点击「稍后」 */\n onDismiss: () => void;\n /** 四个角,或传样式对象自定义坐标(如 { left: 40, top: 40 }) */\n position?: PwaUpdatePosition | CSSProperties;\n className?: string;\n /** 覆盖默认中文文案 */\n texts?: Partial<PwaUpdateTexts>;\n}\n\nconst CLASS_NAME = 'react-pwa-promotion';\n\nconst defaultTexts: PwaUpdateTexts = {\n title: '发现新版本',\n description: '新版本已就绪,刷新即可使用。',\n update: '立即更新',\n dismiss: '稍后'\n};\n\n// 样式独立在 src/style.css(构建时用 Lightning CSS 编译到 dist/style.css),\n// 使用方需引入一次 CSS,组件本身不往 DOM 注入 <style>。\nexport default function PwaUpdatePrompt({\n open,\n onUpdate,\n onDismiss,\n position = 'top-right',\n className,\n texts\n}: PwaUpdatePromptProps) {\n if (!open) return null;\n\n const t = { ...defaultTexts, ...texts };\n const preset = typeof position === 'string';\n const classes = [CLASS_NAME, preset ? `${CLASS_NAME}--${position}` : '', className]\n .filter(Boolean)\n .join(' ');\n\n return (\n <div\n className={classes}\n style={typeof position === 'string' ? undefined : position}\n role=\"dialog\"\n aria-live=\"polite\"\n aria-label={t.title}>\n <div className={`${CLASS_NAME}__title`}>{t.title}</div>\n <div className={`${CLASS_NAME}__desc`}>{t.description}</div>\n <div className={`${CLASS_NAME}__actions`}>\n <button\n type=\"button\"\n className={`${CLASS_NAME}__btn ${CLASS_NAME}__btn--ghost`}\n onClick={onDismiss}>\n {t.dismiss}\n </button>\n <button\n type=\"button\"\n className={`${CLASS_NAME}__btn ${CLASS_NAME}__btn--primary`}\n onClick={onUpdate}>\n {t.update}\n </button>\n </div>\n </div>\n );\n}\n"]}
package/dist/index.d.mts CHANGED
@@ -1,28 +1,26 @@
1
- import React, { HTMLAttributes } from 'react';
1
+ import * as react from 'react';
2
+ import { CSSProperties } from 'react';
2
3
 
3
- type ReactPwaPromotionProps = {
4
+ type PwaUpdatePosition = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
5
+ type PwaUpdateTexts = {
6
+ title: string;
7
+ description: string;
8
+ update: string;
9
+ dismiss: string;
10
+ };
11
+ interface PwaUpdatePromptProps {
12
+ /** 是否有新版本待更新,由宿主的 Service Worker 状态提供 */
13
+ open: boolean;
14
+ /** 点击「立即更新」:宿主负责让新 SW 接管并刷新页面 */
15
+ onUpdate: () => void;
16
+ /** 点击「稍后」 */
17
+ onDismiss: () => void;
18
+ /** 四个角,或传样式对象自定义坐标(如 { left: 40, top: 40 }) */
19
+ position?: PwaUpdatePosition | CSSProperties;
4
20
  className?: string;
5
- lang?: string;
6
- disabled?: boolean;
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;
21
+ /** 覆盖默认中文文案 */
22
+ texts?: Partial<PwaUpdateTexts>;
26
23
  }
24
+ declare function PwaUpdatePrompt({ open, onUpdate, onDismiss, position, className, texts }: PwaUpdatePromptProps): react.JSX.Element | null;
27
25
 
28
- export { ReactPwaPromotion as default };
26
+ export { type PwaUpdatePosition, type PwaUpdatePromptProps, type PwaUpdateTexts, PwaUpdatePrompt as default };
package/dist/index.d.ts CHANGED
@@ -1,28 +1,26 @@
1
- import React, { HTMLAttributes } from 'react';
1
+ import * as react from 'react';
2
+ import { CSSProperties } from 'react';
2
3
 
3
- type ReactPwaPromotionProps = {
4
+ type PwaUpdatePosition = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
5
+ type PwaUpdateTexts = {
6
+ title: string;
7
+ description: string;
8
+ update: string;
9
+ dismiss: string;
10
+ };
11
+ interface PwaUpdatePromptProps {
12
+ /** 是否有新版本待更新,由宿主的 Service Worker 状态提供 */
13
+ open: boolean;
14
+ /** 点击「立即更新」:宿主负责让新 SW 接管并刷新页面 */
15
+ onUpdate: () => void;
16
+ /** 点击「稍后」 */
17
+ onDismiss: () => void;
18
+ /** 四个角,或传样式对象自定义坐标(如 { left: 40, top: 40 }) */
19
+ position?: PwaUpdatePosition | CSSProperties;
4
20
  className?: string;
5
- lang?: string;
6
- disabled?: boolean;
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;
21
+ /** 覆盖默认中文文案 */
22
+ texts?: Partial<PwaUpdateTexts>;
26
23
  }
24
+ declare function PwaUpdatePrompt({ open, onUpdate, onDismiss, position, className, texts }: PwaUpdatePromptProps): react.JSX.Element | null;
27
25
 
28
- export = ReactPwaPromotion;
26
+ export { type PwaUpdatePosition, type PwaUpdatePromptProps, type PwaUpdateTexts, PwaUpdatePrompt as default };
package/dist/index.esm.js CHANGED
@@ -1,2 +1,2 @@
1
- var f=Object.defineProperty;var r=Object.getOwnPropertySymbols;var m=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var i=(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)&&i(a,e,t[e]);if(r)for(var e of r(t))v.call(t,e)&&i(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 l=(a,t,e)=>(i(a,typeof t!="symbol"?t+"":t,e),e);import n 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"}},d=class d extends n.Component{constructor(){super(...arguments);l(this,"state",{needRefresh:!1});l(this,"swInstance");l(this,"t",o(e=>{let{lang:s}=this.props;return C[s][e]||e},"t"));l(this,"updateServiceWorker",o(()=>{this.swInstance.applyUpdate().then(()=>{this.setNeedRefresh(!1),window.location.reload()})},"updateServiceWorker"));l(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:()=>{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?n.createElement("div",N({className:E(c,e)},S),n.createElement("header",{className:`${c}__header`},this.t("update-available")),n.createElement("footer",{className:`${c}__footer`},n.createElement("button",{onClick:()=>this.updateServiceWorker()},this.t("reload")),n.createElement("button",{onClick:()=>this.setNeedRefresh(!1)},this.t("close")))):null}};o(d,"ReactPwaPromotion"),l(d,"defaultProps",{lang:"en-US",disabled:!1,interval:20*1e3,mute:!1});var p=d;export{p as default};
1
+ var u=Object.defineProperty;var p=Object.getOwnPropertySymbols;var _=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var d=(i,t,e)=>t in i?u(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,n=(i,t)=>{for(var e in t||(t={}))_.call(t,e)&&d(i,e,t[e]);if(p)for(var e of p(t))b.call(t,e)&&d(i,e,t[e]);return i};import{jsx as r,jsxs as l}from"react/jsx-runtime";var s="react-pwa-promotion",f={title:"\u53D1\u73B0\u65B0\u7248\u672C",description:"\u65B0\u7248\u672C\u5DF2\u5C31\u7EEA\uFF0C\u5237\u65B0\u5373\u53EF\u4F7F\u7528\u3002",update:"\u7ACB\u5373\u66F4\u65B0",dismiss:"\u7A0D\u540E"};function g({open:i,onUpdate:t,onDismiss:e,position:a="top-right",className:c,texts:m}){if(!i)return null;let o=n(n({},f),m),P=[s,typeof a=="string"?`${s}--${a}`:"",c].filter(Boolean).join(" ");return l("div",{className:P,style:typeof a=="string"?void 0:a,role:"dialog","aria-live":"polite","aria-label":o.title,children:[r("div",{className:`${s}__title`,children:o.title}),r("div",{className:`${s}__desc`,children:o.description}),l("div",{className:`${s}__actions`,children:[r("button",{type:"button",className:`${s}__btn ${s}__btn--ghost`,onClick:e,children:o.dismiss}),r("button",{type:"button",className:`${s}__btn ${s}__btn--primary`,onClick:t,children:o.update})]})]})}export{g as default};
2
2
  //# sourceMappingURL=index.esm.js.map
@@ -1 +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?: boolean;\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: () => {\n this.setNeedRefresh(true);\n },\n });\n }\n\n updateServiceWorker = () => {\n this.swInstance.applyUpdate().then(() => {\n this.setNeedRefresh(false);\n window.location.reload();\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,EACpBC,OAAOC,SAASC,OAAM,CACxB,CAAA,CACF,EALsB,wBAOtBH,EAAAA,sBAAiBP,EAACH,GAAAA,CAChB,KAAKc,SAAS,CAAEd,YAAAA,CAAY,CAAA,CAC9B,EAFiB,mBAvBjBe,mBAAoB,CAClB,GAAM,CAAEC,SAAAA,EAAUC,SAAAA,EAAUC,KAAAA,CAAI,EAAK,KAAKZ,MAC1C,KAAKL,WAAakB,EAAUC,QAAQ,CAClCC,WAAY,GACZL,SAAAA,EACAM,mBAAoBL,EACpBM,aAAc,UAAA,CACRL,GACJM,QAAQC,IAAI,wBAAA,CACd,EACAC,cAAe,IAAA,CACb,KAAKhB,eAAe,EAAA,CACtB,CACF,CAAA,CACF,CAaAiB,QAAS,CACP,IAAsDC,EAAA,KAAKtB,MAAnDuB,WAAAA,EAAWxB,KAAAA,EAAMY,SAAAA,EAAUC,KAAAA,CAzEvC,EAyE0DU,EAAVtB,EAAAA,EAAUsB,EAAVtB,CAApCuB,YAAWxB,OAAMY,WAAUC,SAC7B,CAAElB,YAAAA,CAAW,EAAK,KAAKD,MAE7B,OAAKC,EAGHH,EAAA,cAACiC,MAAAA,EAAAA,CAAID,UAAWE,EAAGrC,EAAYmC,CAAAA,GAAgBvB,GAC7CT,EAAA,cAACmC,SAAAA,CAAOH,UAAW,GAAGnC,CAAAA,YAAuB,KAAKQ,EAAE,kBAAA,CAAA,EACpDL,EAAA,cAACoC,SAAAA,CAAOJ,UAAW,GAAGnC,CAAAA,YACpBG,EAAA,cAACqC,SAAAA,CAAOC,QAAS,IAAM,KAAK5B,oBAAmB,GAAK,KAAKL,EAAE,QAAA,CAAA,EAC3DL,EAAA,cAACqC,SAAAA,CAAOC,QAAS,IAAM,KAAKzB,eAAe,EAAA,GAAS,KAAKR,EAAE,OAAA,CAAA,CAAA,CAAA,EAPxC,IAW3B,CACF,EA9D8DC,EAAAiC,EAAA,qBAC5DC,EADmBzC,EACZ0C,eAAe,CACpBjC,KAAM,QACNW,SAAU,GACVC,SAAU,GAAK,IACfC,KAAM,EACR,GANF,IAAqBtB,EAArBwC","names":["React","cx","SwRuntime","CLASS_NAME","locals","ReactPwaPromotion","React","Component","state","needRefresh","swInstance","t","__name","inKey","lang","props","updateServiceWorker","applyUpdate","then","setNeedRefresh","window","location","reload","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"]}
1
+ {"version":3,"sources":["../src/index.tsx"],"sourcesContent":["import type { CSSProperties } from 'react';\n\nexport type PwaUpdatePosition = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';\n\nexport type PwaUpdateTexts = {\n title: string;\n description: string;\n update: string;\n dismiss: string;\n};\n\nexport interface PwaUpdatePromptProps {\n /** 是否有新版本待更新,由宿主的 Service Worker 状态提供 */\n open: boolean;\n /** 点击「立即更新」:宿主负责让新 SW 接管并刷新页面 */\n onUpdate: () => void;\n /** 点击「稍后」 */\n onDismiss: () => void;\n /** 四个角,或传样式对象自定义坐标(如 { left: 40, top: 40 }) */\n position?: PwaUpdatePosition | CSSProperties;\n className?: string;\n /** 覆盖默认中文文案 */\n texts?: Partial<PwaUpdateTexts>;\n}\n\nconst CLASS_NAME = 'react-pwa-promotion';\n\nconst defaultTexts: PwaUpdateTexts = {\n title: '发现新版本',\n description: '新版本已就绪,刷新即可使用。',\n update: '立即更新',\n dismiss: '稍后'\n};\n\n// 样式独立在 src/style.css(构建时用 Lightning CSS 编译到 dist/style.css),\n// 使用方需引入一次 CSS,组件本身不往 DOM 注入 <style>。\nexport default function PwaUpdatePrompt({\n open,\n onUpdate,\n onDismiss,\n position = 'top-right',\n className,\n texts\n}: PwaUpdatePromptProps) {\n if (!open) return null;\n\n const t = { ...defaultTexts, ...texts };\n const preset = typeof position === 'string';\n const classes = [CLASS_NAME, preset ? `${CLASS_NAME}--${position}` : '', className]\n .filter(Boolean)\n .join(' ');\n\n return (\n <div\n className={classes}\n style={typeof position === 'string' ? undefined : position}\n role=\"dialog\"\n aria-live=\"polite\"\n aria-label={t.title}>\n <div className={`${CLASS_NAME}__title`}>{t.title}</div>\n <div className={`${CLASS_NAME}__desc`}>{t.description}</div>\n <div className={`${CLASS_NAME}__actions`}>\n <button\n type=\"button\"\n className={`${CLASS_NAME}__btn ${CLASS_NAME}__btn--ghost`}\n onClick={onDismiss}>\n {t.dismiss}\n </button>\n <button\n type=\"button\"\n className={`${CLASS_NAME}__btn ${CLASS_NAME}__btn--primary`}\n onClick={onUpdate}>\n {t.update}\n </button>\n </div>\n </div>\n );\n}\n"],"mappings":"yVA2DM,cAAAA,EAEA,QAAAC,MAFA,oBAlCN,IAAMC,EAAa,sBAEbC,EAA+B,CACnC,MAAO,iCACP,YAAa,uFACb,OAAQ,2BACR,QAAS,cACX,EAIe,SAARC,EAAiC,CACtC,KAAAC,EACA,SAAAC,EACA,UAAAC,EACA,SAAAC,EAAW,YACX,UAAAC,EACA,MAAAC,CACF,EAAyB,CACvB,GAAI,CAACL,EAAM,OAAO,KAElB,IAAMM,EAAIC,IAAA,GAAKT,GAAiBO,GAE1BG,EAAU,CAACX,EADF,OAAOM,GAAa,SACG,GAAGN,CAAU,KAAKM,CAAQ,GAAK,GAAIC,CAAS,EAC/E,OAAO,OAAO,EACd,KAAK,GAAG,EAEX,OACER,EAAC,OACC,UAAWY,EACX,MAAO,OAAOL,GAAa,SAAW,OAAYA,EAClD,KAAK,SACL,YAAU,SACV,aAAYG,EAAE,MACd,UAAAX,EAAC,OAAI,UAAW,GAAGE,CAAU,UAAY,SAAAS,EAAE,MAAM,EACjDX,EAAC,OAAI,UAAW,GAAGE,CAAU,SAAW,SAAAS,EAAE,YAAY,EACtDV,EAAC,OAAI,UAAW,GAAGC,CAAU,YAC3B,UAAAF,EAAC,UACC,KAAK,SACL,UAAW,GAAGE,CAAU,SAASA,CAAU,eAC3C,QAASK,EACR,SAAAI,EAAE,QACL,EACAX,EAAC,UACC,KAAK,SACL,UAAW,GAAGE,CAAU,SAASA,CAAU,iBAC3C,QAASI,EACR,SAAAK,EAAE,OACL,GACF,GACF,CAEJ","names":["jsx","jsxs","CLASS_NAME","defaultTexts","PwaUpdatePrompt","open","onUpdate","onDismiss","position","className","texts","t","__spreadValues","classes"]}
package/dist/style.css CHANGED
@@ -1,2 +1 @@
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 */
1
+ .react-pwa-promotion{--rpwa-bg:#fff;--rpwa-color:#111827;--rpwa-muted:#6b7280;--rpwa-border:#e5e7eb;--rpwa-radius:8px;--rpwa-radius-sm:4px;--rpwa-shadow:0 8px 24px #0000001f;--rpwa-accent:#6366f1;--rpwa-btn-ghost-bg:#fff;--rpwa-btn-ghost-color:#374151;--rpwa-btn-ghost-bg-hover:#f3f4f6;--rpwa-btn-primary-bg:#111827;--rpwa-btn-primary-color:#fff;--rpwa-btn-primary-bg-hover:#374151;z-index:9999;box-sizing:border-box;border-radius:var(--rpwa-radius);border:1px solid var(--rpwa-border);background:var(--rpwa-bg);width:240px;color:var(--rpwa-color);box-shadow:var(--rpwa-shadow);padding:8px 12px;font:12px/1.35 system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Noto Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif;animation:.18s ease-out both react-pwa-promotion-in;position:fixed}.react-pwa-promotion.react-pwa-promotion--bottom-right{bottom:16px;right:16px}.react-pwa-promotion.react-pwa-promotion--bottom-left{bottom:16px;left:16px}.react-pwa-promotion.react-pwa-promotion--top-right{animation-name:react-pwa-promotion-in-top;top:16px;right:16px}.react-pwa-promotion.react-pwa-promotion--top-left{animation-name:react-pwa-promotion-in-top;top:16px;left:16px}.react-pwa-promotion .react-pwa-promotion__title{margin-bottom:2px;font-size:13px;font-weight:600}.react-pwa-promotion .react-pwa-promotion__desc{color:var(--rpwa-muted);margin-bottom:6px}.react-pwa-promotion .react-pwa-promotion__actions{justify-content:flex-end;gap:4px;display:flex}.react-pwa-promotion .react-pwa-promotion__actions .react-pwa-promotion__btn{border-radius:var(--rpwa-radius-sm);font:inherit;cursor:pointer;padding:3px 8px;font-size:11px;transition:background .15s,border-color .15s,color .15s,box-shadow .15s,transform .15s}.react-pwa-promotion .react-pwa-promotion__actions .react-pwa-promotion__btn:active{transform:translateY(1px)}.react-pwa-promotion .react-pwa-promotion__actions .react-pwa-promotion__btn:focus-visible{outline:2px solid var(--rpwa-accent);outline-offset:2px}.react-pwa-promotion .react-pwa-promotion__actions .react-pwa-promotion__btn.react-pwa-promotion__btn--ghost{border:1px solid var(--rpwa-border);background:var(--rpwa-btn-ghost-bg);color:var(--rpwa-btn-ghost-color)}.react-pwa-promotion .react-pwa-promotion__actions .react-pwa-promotion__btn.react-pwa-promotion__btn--ghost:hover{background:var(--rpwa-btn-ghost-bg-hover);color:var(--rpwa-color);border-color:#d1d5db}.react-pwa-promotion .react-pwa-promotion__actions .react-pwa-promotion__btn.react-pwa-promotion__btn--primary{border:1px solid var(--rpwa-btn-primary-bg);background:var(--rpwa-btn-primary-bg);color:var(--rpwa-btn-primary-color)}.react-pwa-promotion .react-pwa-promotion__actions .react-pwa-promotion__btn.react-pwa-promotion__btn--primary:hover{background:var(--rpwa-btn-primary-bg-hover);border-color:var(--rpwa-btn-primary-bg-hover);box-shadow:0 3px 10px #11182740}@media (prefers-reduced-motion:reduce){.react-pwa-promotion{animation:none}.react-pwa-promotion .react-pwa-promotion__btn{transition:none}}@keyframes react-pwa-promotion-in{0%{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}@keyframes react-pwa-promotion-in-top{0%{opacity:0;transform:translateY(-6px)}to{opacity:1;transform:none}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jswork/react-pwa-promotion",
3
- "description": "A vite pwa promotion component for workbox.",
4
- "version": "1.0.15",
3
+ "description": "A tiny React update prompt for Vite PWA (vite-plugin-pwa).",
4
+ "version": "1.0.16",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "types": "dist/index.d.ts",
@@ -9,36 +9,34 @@
9
9
  "license": "MIT",
10
10
  "scripts": {
11
11
  "build": "tsup",
12
- "postbuild": "postsass -s src/style.scss -d dist/style.css -c",
13
12
  "release": "release-it --ci --npm.skipChecks"
14
13
  },
15
14
  "devDependencies": {
16
- "@swc/core": "^1.3.93",
15
+ "@jswork/tsup-plugin-lightningcss": "link:../../../tsup-plugin-lightningcss",
16
+ "@types/node": "^20.11.0",
17
17
  "@types/react": "^18.2.28",
18
18
  "@types/react-dom": "^18.2.13",
19
- "autoprefixer": "^10.4.16",
20
- "cssnano": "^6.0.1",
21
- "prettier": "^3.2.4",
19
+ "lightningcss": "^1.33.0",
22
20
  "react": "^18.2.0",
23
21
  "react-dom": "^18.2.0",
24
22
  "tsup": "^7.2.0",
25
23
  "typescript": "^5.2.2"
26
24
  },
25
+ "peerDependencies": {
26
+ "react": ">=18",
27
+ "react-dom": ">=18"
28
+ },
27
29
  "publishConfig": {
28
30
  "access": "public",
29
31
  "registry": "https://registry.npmjs.org"
30
32
  },
31
- "dependencies": {
32
- "@jswork/sw-runtime": "^1.0.10",
33
- "classnames": "^2.5.1"
34
- },
35
33
  "keywords": [
36
34
  "component",
37
35
  "pwa",
38
36
  "react",
39
- "runtime",
40
- "sw",
41
- "workbox",
42
- "serviceworker"
37
+ "vite",
38
+ "vite-plugin-pwa",
39
+ "serviceworker",
40
+ "update"
43
41
  ]
44
42
  }
package/src/index.tsx CHANGED
@@ -1,89 +1,78 @@
1
- import React, { HTMLAttributes } from 'react';
2
- import cx from 'classnames';
3
- import SwRuntime from '@jswork/sw-runtime';
1
+ import type { CSSProperties } from 'react';
4
2
 
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
- },
3
+ export type PwaUpdatePosition = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
4
+
5
+ export type PwaUpdateTexts = {
6
+ title: string;
7
+ description: string;
8
+ update: string;
9
+ dismiss: string;
17
10
  };
18
11
 
19
- type ReactPwaPromotionProps = {
12
+ export interface PwaUpdatePromptProps {
13
+ /** 是否有新版本待更新,由宿主的 Service Worker 状态提供 */
14
+ open: boolean;
15
+ /** 点击「立即更新」:宿主负责让新 SW 接管并刷新页面 */
16
+ onUpdate: () => void;
17
+ /** 点击「稍后」 */
18
+ onDismiss: () => void;
19
+ /** 四个角,或传样式对象自定义坐标(如 { left: 40, top: 40 }) */
20
+ position?: PwaUpdatePosition | CSSProperties;
20
21
  className?: string;
21
- lang?: string;
22
- disabled?: boolean;
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: () => {
57
- this.setNeedRefresh(true);
58
- },
59
- });
60
- }
22
+ /** 覆盖默认中文文案 */
23
+ texts?: Partial<PwaUpdateTexts>;
24
+ }
61
25
 
62
- updateServiceWorker = () => {
63
- this.swInstance.applyUpdate().then(() => {
64
- this.setNeedRefresh(false);
65
- window.location.reload();
66
- });
67
- };
26
+ const CLASS_NAME = 'react-pwa-promotion';
68
27
 
69
- setNeedRefresh = (needRefresh: boolean) => {
70
- this.setState({ needRefresh });
71
- };
28
+ const defaultTexts: PwaUpdateTexts = {
29
+ title: '发现新版本',
30
+ description: '新版本已就绪,刷新即可使用。',
31
+ update: '立即更新',
32
+ dismiss: '稍后'
33
+ };
72
34
 
73
- render() {
74
- const { className, lang, interval, mute, ...props } = this.props;
75
- const { needRefresh } = this.state;
35
+ // 样式独立在 src/style.css(构建时用 Lightning CSS 编译到 dist/style.css),
36
+ // 使用方需引入一次 CSS,组件本身不往 DOM 注入 <style>。
37
+ export default function PwaUpdatePrompt({
38
+ open,
39
+ onUpdate,
40
+ onDismiss,
41
+ position = 'top-right',
42
+ className,
43
+ texts
44
+ }: PwaUpdatePromptProps) {
45
+ if (!open) return null;
76
46
 
77
- if (!needRefresh) return null;
47
+ const t = { ...defaultTexts, ...texts };
48
+ const preset = typeof position === 'string';
49
+ const classes = [CLASS_NAME, preset ? `${CLASS_NAME}--${position}` : '', className]
50
+ .filter(Boolean)
51
+ .join(' ');
78
52
 
79
- return (
80
- <div className={cx(CLASS_NAME, className)} {...props}>
81
- <header className={`${CLASS_NAME}__header`}>{this.t('update-available')}</header>
82
- <footer className={`${CLASS_NAME}__footer`}>
83
- <button onClick={() => this.updateServiceWorker()}>{this.t('reload')}</button>
84
- <button onClick={() => this.setNeedRefresh(false)}>{this.t('close')}</button>
85
- </footer>
53
+ return (
54
+ <div
55
+ className={classes}
56
+ style={typeof position === 'string' ? undefined : position}
57
+ role="dialog"
58
+ aria-live="polite"
59
+ aria-label={t.title}>
60
+ <div className={`${CLASS_NAME}__title`}>{t.title}</div>
61
+ <div className={`${CLASS_NAME}__desc`}>{t.description}</div>
62
+ <div className={`${CLASS_NAME}__actions`}>
63
+ <button
64
+ type="button"
65
+ className={`${CLASS_NAME}__btn ${CLASS_NAME}__btn--ghost`}
66
+ onClick={onDismiss}>
67
+ {t.dismiss}
68
+ </button>
69
+ <button
70
+ type="button"
71
+ className={`${CLASS_NAME}__btn ${CLASS_NAME}__btn--primary`}
72
+ onClick={onUpdate}>
73
+ {t.update}
74
+ </button>
86
75
  </div>
87
- );
88
- }
76
+ </div>
77
+ );
89
78
  }
package/src/style.css ADDED
@@ -0,0 +1,135 @@
1
+ /* 源码用原生 CSS Nesting 组织,构建时由 Lightning CSS 按 targets 展开成扁平选择器。
2
+ 注意:这里不能用 Sass 风格的 &__xxx 后缀拼接(Lightning CSS 不支持)。
3
+ 主题定制:覆盖 .react-pwa-promotion 上的 --rpwa-* 变量即可。 */
4
+
5
+ .react-pwa-promotion {
6
+ /* 主题变量 */
7
+ --rpwa-bg: #fff;
8
+ --rpwa-color: #111827;
9
+ --rpwa-muted: #6b7280;
10
+ --rpwa-border: #e5e7eb;
11
+ --rpwa-radius: 8px;
12
+ --rpwa-radius-sm: 4px;
13
+ --rpwa-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
14
+ --rpwa-accent: #6366f1;
15
+ --rpwa-btn-ghost-bg: #fff;
16
+ --rpwa-btn-ghost-color: #374151;
17
+ --rpwa-btn-ghost-bg-hover: #f3f4f6;
18
+ --rpwa-btn-primary-bg: #111827;
19
+ --rpwa-btn-primary-color: #fff;
20
+ --rpwa-btn-primary-bg-hover: #374151;
21
+
22
+ position: fixed;
23
+ z-index: 9999;
24
+ box-sizing: border-box;
25
+ width: 240px;
26
+ padding: 8px 12px;
27
+ border-radius: var(--rpwa-radius);
28
+ border: 1px solid var(--rpwa-border);
29
+ background: var(--rpwa-bg);
30
+ color: var(--rpwa-color);
31
+ box-shadow: var(--rpwa-shadow);
32
+ font: 12px/1.35 system-ui, sans-serif;
33
+ animation: react-pwa-promotion-in 0.18s ease-out both;
34
+
35
+ /* 位置变体:& 后接完整类选择器,展开后根类与变体类同时命中 */
36
+ &.react-pwa-promotion--bottom-right {
37
+ right: 16px;
38
+ bottom: 16px;
39
+ }
40
+
41
+ &.react-pwa-promotion--bottom-left {
42
+ left: 16px;
43
+ bottom: 16px;
44
+ }
45
+
46
+ &.react-pwa-promotion--top-right {
47
+ right: 16px;
48
+ top: 16px;
49
+ animation-name: react-pwa-promotion-in-top;
50
+ }
51
+
52
+ &.react-pwa-promotion--top-left {
53
+ left: 16px;
54
+ top: 16px;
55
+ animation-name: react-pwa-promotion-in-top;
56
+ }
57
+
58
+ & .react-pwa-promotion__title {
59
+ font-size: 13px;
60
+ font-weight: 600;
61
+ margin-bottom: 2px;
62
+ }
63
+
64
+ & .react-pwa-promotion__desc {
65
+ color: var(--rpwa-muted);
66
+ margin-bottom: 6px;
67
+ }
68
+
69
+ & .react-pwa-promotion__actions {
70
+ display: flex;
71
+ gap: 4px;
72
+ justify-content: flex-end;
73
+
74
+ & .react-pwa-promotion__btn {
75
+ padding: 3px 8px;
76
+ border-radius: var(--rpwa-radius-sm);
77
+ font: inherit;
78
+ font-size: 11px;
79
+ cursor: pointer;
80
+ transition: background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s,
81
+ transform 0.15s;
82
+
83
+ &:active {
84
+ transform: translateY(1px);
85
+ }
86
+
87
+ &:focus-visible {
88
+ outline: 2px solid var(--rpwa-accent);
89
+ outline-offset: 2px;
90
+ }
91
+
92
+ &.react-pwa-promotion__btn--ghost {
93
+ border: 1px solid var(--rpwa-border);
94
+ background: var(--rpwa-btn-ghost-bg);
95
+ color: var(--rpwa-btn-ghost-color);
96
+
97
+ &:hover {
98
+ background: var(--rpwa-btn-ghost-bg-hover);
99
+ border-color: #d1d5db;
100
+ color: var(--rpwa-color);
101
+ }
102
+ }
103
+
104
+ &.react-pwa-promotion__btn--primary {
105
+ border: 1px solid var(--rpwa-btn-primary-bg);
106
+ background: var(--rpwa-btn-primary-bg);
107
+ color: var(--rpwa-btn-primary-color);
108
+
109
+ &:hover {
110
+ background: var(--rpwa-btn-primary-bg-hover);
111
+ border-color: var(--rpwa-btn-primary-bg-hover);
112
+ box-shadow: 0 3px 10px rgba(17, 24, 39, 0.25);
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ @media (prefers-reduced-motion: reduce) {
119
+ animation: none;
120
+
121
+ & .react-pwa-promotion__btn {
122
+ transition: none;
123
+ }
124
+ }
125
+ }
126
+
127
+ @keyframes react-pwa-promotion-in {
128
+ from { opacity: 0; transform: translateY(6px); }
129
+ to { opacity: 1; transform: none; }
130
+ }
131
+
132
+ @keyframes react-pwa-promotion-in-top {
133
+ from { opacity: 0; transform: translateY(-6px); }
134
+ to { opacity: 1; transform: none; }
135
+ }
@@ -1 +0,0 @@
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 DELETED
@@ -1,48 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
1
- interface NxStatic {
2
- // $say: (msg: string) => void;
3
- }
package/src/style.scss DELETED
@@ -1,48 +0,0 @@
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
- }