Socket
Socket
Sign inDemoInstall

storybook-vue3-router

Package Overview
Dependencies
227
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.25.0 to 5.0.0

18

dist/index.d.ts

@@ -31,2 +31,18 @@ import * as vue_router from 'vue-router';

*/
declare function withAsyncVueRouter(routes?: vue_router.RouteRecordRaw[], options?: {
initialRoute?: string;
beforeEach?: NavigationGuard;
vueRouterOptions?: RouterOptions;
}): Decorator;
/**
* Add a vue router instance to Storybook stories
* @param routes (optional) custom routes for story
* @param options (optional) custom options
*
* @remarks
*
* If there is a previously initialized story using vue-router and you wish to use `beforeEach` to apply global router guards via `options` param,
* we must reload the story in order to apply the global route guards, this can have a minor performance impact.
*/
declare function withMockRouter(options: {

@@ -38,2 +54,2 @@ meta?: Array<string>;

export { withMockRouter as mockRouter, withVueRouter as vueRouter };
export { withAsyncVueRouter as asyncVueRouter, withMockRouter as mockRouter, withVueRouter as vueRouter };

@@ -101,2 +101,31 @@ 'use strict';

function withAsyncVueRouter(routes = defaultRoutes, options) {
return () => ({
async setup() {
const { app } = vue.getCurrentInstance().appContext;
let router;
const existingRouter = app.config.globalProperties.$router;
const existingRoute = app.config.globalProperties.$route;
if ((!existingRouter || existingRouter.isMocked === true) && (!existingRoute || existingRoute.isMocked === true)) {
router = vueRouter.createRouter({
history: vueRouter.createWebHashHistory(),
routes,
...options?.vueRouterOptions
});
globalRouterGuardFn(router, options?.beforeEach);
app.use(router);
} else {
router = existingRouter;
resetRoutes(router, routes);
globalRouterGuardFn(existingRouter, options?.beforeEach, true);
}
initialRoute(router, options?.initialRoute);
await router.isReady();
},
template: `
<story/>
`
});
}
function withMockRouter(options) {

@@ -148,4 +177,5 @@ return (_, ctx) => ({

exports.asyncVueRouter = withAsyncVueRouter;
exports.mockRouter = withMockRouter;
exports.vueRouter = withVueRouter;
//# sourceMappingURL=index.js.map

62

package.json
{
"name": "storybook-vue3-router",
"version": "4.25.0",
"version": "5.0.0",
"description": "A Storybook decorator that allows you to build stories for your routing-aware components.",

@@ -24,33 +24,18 @@ "keywords": [

],
"scripts": {
"clean": "rimraf ./dist",
"prebuild": "pnpm clean",
"build": "rollup -c",
"build:watch": "rollup -c --watch",
"storybook": "storybook dev -p 6006",
"dev": "concurrently \"pnpm storybook\" \"pnpm build:watch\"",
"build-storybook": "storybook build",
"publish": "pnpm build && auto shipit",
"publish:dry": "pnpm build && auto shipit --dry-run",
"changelog": "auto changelog",
"test": "npx playwright test",
"eject-ts": "zx scripts/eject-typescript.mjs"
},
"dependencies": {
"vue-router": "^4.2.4"
"vue-router": "^4.2.5"
},
"devDependencies": {
"@playwright/test": "^1.38.0",
"@storybook/addon-actions": "^7.4.2",
"@storybook/addon-essentials": "^7.4.2",
"@playwright/test": "^1.40.1",
"@storybook/addon-actions": "^7.6.3",
"@storybook/addon-essentials": "^7.6.3",
"@storybook/addon-mdx-gfm": "7.4.2",
"@storybook/addons": "^7.4.2",
"@storybook/blocks": "^7.4.2",
"@storybook/preview-api": "^7.4.2",
"@storybook/types": "^7.4.2",
"@storybook/vue3": "^7.4.2",
"@storybook/vue3-vite": "^7.4.2",
"@types/node": "^18.17.17",
"@vitejs/plugin-vue": "^4.3.4",
"auto": "^10.46.0",
"@storybook/addons": "^7.6.3",
"@storybook/blocks": "^7.6.3",
"@storybook/preview-api": "^7.6.3",
"@storybook/types": "^7.6.3",
"@storybook/vue3": "^7.6.3",
"@storybook/vue3-vite": "^7.6.3",
"@types/node": "^18.19.2",
"@vitejs/plugin-vue": "^4.5.1",
"concurrently": "^6.5.1",

@@ -62,9 +47,9 @@ "esbuild": "^0.17.19",

"rimraf": "^3.0.2",
"rollup": "^3.29.2",
"rollup": "^3.29.4",
"rollup-plugin-dts": "^5.3.1",
"rollup-plugin-esbuild": "^5.0.0",
"storybook": "^7.4.2",
"storybook": "^7.6.3",
"typescript": "^4.9.5",
"vite": "^4.4.9",
"vue": "^3.3.4",
"vite": "^4.5.1",
"vue": "^3.3.10",
"zx": "^1.15.2"

@@ -95,3 +80,14 @@ },

"icon": "https://github.com/NickMcBurney/storybook-vue3-router/blob/main/vue-logo.png?raw=true"
},
"scripts": {
"clean": "rimraf ./dist",
"prebuild": "pnpm clean",
"build": "rollup -c",
"build:watch": "rollup -c --watch",
"storybook": "storybook dev -p 6006",
"dev": "concurrently \"pnpm storybook\" \"pnpm build:watch\"",
"build-storybook": "storybook build",
"prepublish": "pnpm build",
"test": "npx playwright test"
}
}
}

@@ -19,3 +19,3 @@ # Storybook Vue3 Router

### Storybook v6: Please use package version 2.x
### Storybook v7: Please use package version 3.x
### Storybook v7: Please use package version 3+
See [migration guides](#v2x--v3x-migration).

@@ -120,11 +120,2 @@

/* adding storybook-vue3-router decorator */
Default.decorators = [
/* pass initialRoute to the decorator */
vueRouter(customRoutes, {
initialRoute: '/intro'
})
]
```
### With Router Options

@@ -147,2 +138,49 @@ We can pass [Vue Router options](https://router.vuejs.org/api/index.html#history) into our decorator.

## `router.isReady()`
If you have a router setup using `router.isReady()` and / or you have components which require specific route / route data on created lifecycle hook you may need to use the `asyncVueRouter` export.
This export provides router which won't render story until router is ready.
### Story setup
```typescript
import { asyncVueRouter } from 'storybook-vue3-router'
/* define our custom routes */
const customRoutes = [
{
path: '/',
name: 'dashboard',
component: Dashboard
},
{
path: '/intro',
name: 'intro',
component: Intro
}
]
/* adding storybook-vue3-router decorator */
Default.decorators = [
/* pass initialRoute to the decorator */
asyncVueRouter(customRoutes, {
initialRoute: '/intro'
})
]
```
### Preview.js Async Setup
In order to use `async` router setup method, you will need to amend your .storybook/preview.js file to wrap stories in Vue 3's `<Suspense>` component. This is because the decorator requires an `async setup()` to correctly `await router.isReady()`. You can modify preview to:
```typescript
const preview = {
decorators: [
(story) => ({
components: { story },
template: '<Suspense><story /></Suspense>',
}),
],
};
export default preview;
```
See [the examples folder](https://github.com/NickMcBurney/storybook-vue3-router/tree/main/examples) for more advanced usage.

@@ -154,7 +192,10 @@

function vueRouter(routes: RouteRecordRaw[], options?: { initialRoute?: string, beforeEach?: NavigationGuard, vueRouterOptions?: RouterOptions })
function asyncVueRouter(routes: RouteRecordRaw[], options?: { initialRoute?: string, beforeEach?: NavigationGuard, vueRouterOptions?: RouterOptions })
```
## Mock Router
The full `vue-router` is not always needed - for example if you don't have components using `<router-view>` or `<router-link>` then using the `mockRouter` export may cover your needs (and reduce the imports being used in your project).
The full `vue-router` is not always needed - for example if you don't have components using `<router-view>` or `<router-link>` then using the `mockRouter` export may cover your needs (and reduce the imports being used in your stories).
Note: `mockRouter` will only work in instances where you are using options API `this.$route` and/or `this.$router`, it is not suitable for use-cases using vue router composables such as `useRoute()` and `useRouter()`.
### Use `mockRouter` in your stories

@@ -193,3 +234,3 @@ The default setup will create mock `$router` and `$route` from `vue-router`, this allows you to create stories for components using programatic navigation and route based logic.

## v2.x > v3.x Migration
## v2.x > v3+ Migration
### ⚠️ BREAKING CHANGE ⚠️

@@ -196,0 +237,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc