From 2b7ce51fdf403b5154cba760f3576a47bbdf6a4f Mon Sep 17 00:00:00 2001 From: Jon Palmer <328224+jonspalmer@users.noreply.github.com> Date: Sat, 22 Feb 2020 11:14:38 -0500 Subject: [PATCH 1/2] Add preset for Links Addon --- MIGRATION.md | 2 +- addons/links/preset.js | 1 + addons/links/register.js | 2 +- addons/links/src/manager.ts | 14 -------------- addons/links/src/preset/addDecorator.ts | 3 +++ addons/links/src/preset/index.ts | 15 +++++++++++++++ addons/links/src/register.ts | 12 ++++++++++++ 7 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 addons/links/preset.js delete mode 100644 addons/links/src/manager.ts create mode 100644 addons/links/src/preset/addDecorator.ts create mode 100644 addons/links/src/preset/index.ts create mode 100644 addons/links/src/register.ts diff --git a/MIGRATION.md b/MIGRATION.md index ff5d8e326e1..f4cbe745d89 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -88,7 +88,7 @@ In Storybook 5.3 we introduced a declarative [main.js configuration](#to-mainjs-configuration), which is now the recommended way to configure Storybook. Part of the change is a simplified syntax for registering addons, which in 6.0 automatically registers many addons _using a preset_, which is a slightly different behavior than in earlier versions. -This breaking change currently applies to: `addon-a11y`, `addon-knobs`. +This breaking change currently applies to: `addon-a11y`, `addon-knobs`, `addon-links`. Consider the following `main.js` config for the accessibility addon, `addon-a11y`: diff --git a/addons/links/preset.js b/addons/links/preset.js new file mode 100644 index 00000000000..a83f95279e7 --- /dev/null +++ b/addons/links/preset.js @@ -0,0 +1 @@ +module.exports = require('./dist/preset'); diff --git a/addons/links/register.js b/addons/links/register.js index 9232e6c069d..cc38cb06f1f 100644 --- a/addons/links/register.js +++ b/addons/links/register.js @@ -1 +1 @@ -require('./dist/manager').register(); +require('./dist/register'); diff --git a/addons/links/src/manager.ts b/addons/links/src/manager.ts deleted file mode 100644 index d5aa5b78898..00000000000 --- a/addons/links/src/manager.ts +++ /dev/null @@ -1,14 +0,0 @@ -import addons from '@storybook/addons'; - -import EVENTS, { ADDON_ID } from './constants'; - -export const register = () => { - addons.register(ADDON_ID, api => { - const channel = addons.getChannel(); - - channel.on(EVENTS.REQUEST, ({ kind, name }) => { - const id = api.storyId(kind, name); - api.emit(EVENTS.RECEIVE, id); - }); - }); -}; diff --git a/addons/links/src/preset/addDecorator.ts b/addons/links/src/preset/addDecorator.ts new file mode 100644 index 00000000000..ff68743b1a3 --- /dev/null +++ b/addons/links/src/preset/addDecorator.ts @@ -0,0 +1,3 @@ +import { withLinks } from '../index'; + +export const decorators = [withLinks]; diff --git a/addons/links/src/preset/index.ts b/addons/links/src/preset/index.ts new file mode 100644 index 00000000000..909f147b60e --- /dev/null +++ b/addons/links/src/preset/index.ts @@ -0,0 +1,15 @@ +type LinkOptions = { + addDecorator?: boolean; +}; + +export function managerEntries(entry: any[] = []) { + return [...entry, require.resolve('../register')]; +} + +export function config(entry: any[] = [], { addDecorator = true }: LinkOptions = {}) { + const linkConfig = []; + if (addDecorator) { + linkConfig.push(require.resolve('./addDecorator')); + } + return [...entry, ...linkConfig]; +} diff --git a/addons/links/src/register.ts b/addons/links/src/register.ts new file mode 100644 index 00000000000..cc5e5051f3d --- /dev/null +++ b/addons/links/src/register.ts @@ -0,0 +1,12 @@ +import addons from '@storybook/addons'; + +import EVENTS, { ADDON_ID } from './constants'; + +addons.register(ADDON_ID, api => { + const channel = addons.getChannel(); + + channel.on(EVENTS.REQUEST, ({ kind, name }) => { + const id = api.storyId(kind, name); + api.emit(EVENTS.RECEIVE, id); + }); +}); From 06bdaf5e92abe9780688e4e40de89016066a9673 Mon Sep 17 00:00:00 2001 From: Jon Palmer <328224+jonspalmer@users.noreply.github.com> Date: Sat, 22 Feb 2020 20:24:50 -0500 Subject: [PATCH 2/2] use makeDecorator to allow disabling via parameters --- addons/links/src/constants.ts | 1 + addons/links/src/preview.ts | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/addons/links/src/constants.ts b/addons/links/src/constants.ts index 8cd994edd00..7fb1ad05b5d 100644 --- a/addons/links/src/constants.ts +++ b/addons/links/src/constants.ts @@ -1,4 +1,5 @@ export const ADDON_ID = 'storybook/links'; +export const PARAM_KEY = `links`; export default { NAVIGATE: `${ADDON_ID}/navigate`, diff --git a/addons/links/src/preview.ts b/addons/links/src/preview.ts index 0aeb0ebd9ed..29cf67a850d 100644 --- a/addons/links/src/preview.ts +++ b/addons/links/src/preview.ts @@ -5,7 +5,8 @@ import { __STORYBOOK_CLIENT_API__ as clientApi, } from 'global'; import qs from 'qs'; -import addons from '@storybook/addons'; +import addons, { makeDecorator } from '@storybook/addons'; +import { PARAM_KEY } from './constants'; import { STORY_CHANGED, SELECT_STORY } from '@storybook/core-events'; import { toId } from '@storybook/csf'; import { logger } from '@storybook/client-logger'; @@ -109,8 +110,12 @@ const off = () => { } }; -export const withLinks = (storyFn: () => void) => { - on(); - addons.getChannel().once(STORY_CHANGED, off); - return storyFn(); -}; +export const withLinks = makeDecorator({ + name: 'withLinks', + parameterName: PARAM_KEY, + wrapper: (getStory, context, { parameters }) => { + on(); + addons.getChannel().once(STORY_CHANGED, off); + return getStory(context); + } +});