Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-pwa-popup): extract pwa popup plugin
- Loading branch information
Showing
10 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@vuepress/plugin-pwa-popup", | ||
"version": "2.0.0-alpha.7", | ||
"description": "VuePress plugin - PWA popup", | ||
"keywords": [ | ||
"vuepress", | ||
"plugin", | ||
"pwa", | ||
"popup" | ||
], | ||
"homepage": "https://github.com/vuepress", | ||
"bugs": { | ||
"url": "https://github.com/vuepress/vuepress-next/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vuepress/vuepress-next.git" | ||
}, | ||
"license": "MIT", | ||
"author": "meteorlxy", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib", | ||
"styles" | ||
], | ||
"scripts": { | ||
"build": "tsc -b tsconfig.build.json", | ||
"clean": "rimraf lib *.tsbuildinfo", | ||
"copy": "cpx \"src/**/*.css\" lib" | ||
}, | ||
"dependencies": { | ||
"@vuepress/client": "2.0.0-alpha.7", | ||
"@vuepress/core": "2.0.0-alpha.7", | ||
"@vuepress/plugin-pwa": "2.0.0-alpha.7", | ||
"@vuepress/shared": "2.0.0-alpha.7", | ||
"@vuepress/utils": "2.0.0-alpha.7" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/@vuepress/plugin-pwa-popup/src/components/PwaPopup.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
:root { | ||
--pwa-popup-color: #3eaf7c; | ||
} | ||
|
||
.pwa-popup { | ||
position: fixed; | ||
right: 1em; | ||
bottom: 1em; | ||
padding: 1em; | ||
border: 1px solid #3eaf7c; | ||
border-radius: 3px; | ||
background: #fff; | ||
box-shadow: 0 4px 16px var(--pwa-popup-color); | ||
text-align: center; | ||
z-index: 3; | ||
} | ||
|
||
.pwa-popup > button { | ||
margin-top: 0.5em; | ||
padding: 0.25em 2em; | ||
} | ||
|
||
.pwa-popup-enter-active, | ||
.pwa-popup-leave-active { | ||
transition: opacity 0.3s, transform 0.3s; | ||
} | ||
|
||
.pwa-popup-enter-from, | ||
.pwa-popup-leave-to { | ||
opacity: 0; | ||
transform: translate(0, 50%) scale(0.5); | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/@vuepress/plugin-pwa-popup/src/components/PwaPopup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { computed, defineComponent, h, ref, Transition } from 'vue' | ||
import type { PropType } from 'vue' | ||
import { useRouteLocale } from '@vuepress/client' | ||
import { | ||
usePwaEvent, | ||
useSkipWaiting, | ||
} from '@vuepress/plugin-pwa/lib/composables' | ||
import type { LocaleConfig } from '@vuepress/shared' | ||
|
||
import './PwaPopup.css' | ||
|
||
export type PwaPopupLocales = LocaleConfig<{ | ||
message: string | ||
buttonText: string | ||
}> | ||
|
||
export const PwaPopup = defineComponent({ | ||
name: 'PwaPopup', | ||
|
||
props: { | ||
locales: { | ||
type: Object as PropType<PwaPopupLocales>, | ||
required: true, | ||
default: () => ({}), | ||
}, | ||
}, | ||
|
||
setup(props) { | ||
const event = usePwaEvent() | ||
const routeLocale = useRouteLocale() | ||
|
||
const locale = computed( | ||
() => | ||
props.locales[routeLocale.value] ?? { | ||
message: 'New content is available.', | ||
buttonText: 'Refresh', | ||
} | ||
) | ||
|
||
const show = ref(false) | ||
const registration = ref<ServiceWorkerRegistration | null>(null) | ||
const onClick = () => { | ||
show.value = false | ||
if (registration.value) { | ||
useSkipWaiting(registration.value) | ||
location.reload(true) | ||
} | ||
} | ||
|
||
event.on('updated', (reg) => { | ||
if (reg) { | ||
registration.value = reg | ||
show.value = true | ||
} | ||
}) | ||
|
||
return () => | ||
h( | ||
Transition, | ||
{ | ||
name: 'pwa-popup', | ||
}, | ||
{ | ||
default: () => | ||
show.value | ||
? h( | ||
'div', | ||
{ | ||
class: 'pwa-popup', | ||
}, | ||
[ | ||
locale.value.message, | ||
h('br'), | ||
h( | ||
'button', | ||
{ | ||
onClick, | ||
}, | ||
locale.value.buttonText | ||
), | ||
] | ||
) | ||
: null, | ||
} | ||
) | ||
}, | ||
}) | ||
|
||
export default PwaPopup |
22 changes: 22 additions & 0 deletions
22
packages/@vuepress/plugin-pwa-popup/src/components/PwaPopupWrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { h } from 'vue' | ||
import type { FunctionalComponent } from 'vue' | ||
import { PwaPopup } from './PwaPopup' | ||
import type { PwaPopupLocales } from './PwaPopup' | ||
|
||
declare const __DEV__: boolean | ||
declare const __SSR__: boolean | ||
declare const __PWA_POPUP_LOCALES__: PwaPopupLocales | ||
|
||
const locales = __PWA_POPUP_LOCALES__ | ||
|
||
export const PwaPopupWrapper: FunctionalComponent = () => { | ||
if (__DEV__ || __SSR__) return null | ||
|
||
return h(PwaPopup, { | ||
locales, | ||
}) | ||
} | ||
|
||
PwaPopupWrapper.displayName = 'PwaPopupWrapper' | ||
|
||
export default PwaPopupWrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Plugin } from '@vuepress/core' | ||
import type { LocaleConfig } from '@vuepress/shared' | ||
import { path } from '@vuepress/utils' | ||
|
||
/** | ||
* Options for @vuepress/plugin-pwa-popup | ||
*/ | ||
export interface PwaPopupPluginOptions { | ||
locales?: LocaleConfig<{ | ||
message: string | ||
buttonText: string | ||
}> | ||
} | ||
|
||
export const pwaPopupPlugin: Plugin<PwaPopupPluginOptions> = ({ locales }) => ({ | ||
name: '@vuepress/plugin-pwa-popup', | ||
|
||
clientAppRootComponentFiles: path.resolve( | ||
__dirname, | ||
'./components/PwaPopupWrapper.js' | ||
), | ||
|
||
define: { | ||
__PWA_POPUP_LOCALES__: locales, | ||
}, | ||
}) | ||
|
||
export default pwaPopupPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"references": [ | ||
{ "path": "../client/tsconfig.build.json" }, | ||
{ "path": "../core/tsconfig.build.json" }, | ||
{ "path": "../plugin-pwa/tsconfig.build.json" }, | ||
{ "path": "../shared/tsconfig.build.json" }, | ||
{ "path": "../utils/tsconfig.build.json" }, | ||
{ "path": "./tsconfig.esm.json" }, | ||
{ "path": "./tsconfig.cjs.json" } | ||
], | ||
"files": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"rootDir": "./src", | ||
"outDir": "./lib" | ||
}, | ||
"include": ["./src/index.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "ES2020", | ||
"rootDir": "./src", | ||
"outDir": "./lib" | ||
}, | ||
"include": ["./src"], | ||
"exclude": ["./src/index.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"include": ["./src", "./__tests__"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters