Skip to content

Commit 94fe6d4

Browse files
committed
move code from lib/addons to manager-api, reference manager-api or preview-api instead of lib/addons
1 parent 22f829e commit 94fe6d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+223
-198
lines changed

code/addons/a11y/src/manager.test.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { addons } from '@storybook/addons';
21
import * as api from '@storybook/manager-api';
32
import { PANEL_ID } from './constants';
43
import './manager';
@@ -7,7 +6,7 @@ jest.mock('@storybook/manager-api');
76
jest.mock('@storybook/addons');
87
const mockedApi = api as unknown as jest.Mocked<api.API>;
98
mockedApi.getAddonState = jest.fn();
10-
const mockedAddons = addons as jest.Mocked<typeof addons>;
9+
const mockedAddons = api.addons as jest.Mocked<typeof api.addons>;
1110
const registrationImpl = mockedAddons.register.mock.calls[0][1];
1211

1312
describe('A11yManager', () => {

code/addons/a11y/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types } from '@storybook/manager-api';
33
import { ADDON_ID, PANEL_ID, PARAM_KEY } from './constants';
44
import { VisionSimulator } from './components/VisionSimulator';
55
import { A11YPanel } from './components/A11YPanel';

code/addons/actions/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState, useEffect } from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types } from '@storybook/manager-api';
33
import { STORY_CHANGED } from '@storybook/core-events';
44
import ActionLogger from './containers/ActionLogger';
55
import { ADDON_ID, EVENT_ID, PANEL_ID, PARAM_KEY } from './constants';

code/addons/backgrounds/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Fragment } from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types } from '@storybook/manager-api';
33

44
import { ADDON_ID } from './constants';
55
import { BackgroundSelector } from './containers/BackgroundSelector';

code/addons/controls/src/manager.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types, type API, useArgTypes } from '@storybook/manager-api';
33
import { AddonPanel } from '@storybook/components';
4-
import { type API, useArgTypes } from '@storybook/manager-api';
54
import { ControlsPanel } from './ControlsPanel';
65
import { ADDON_ID, PARAM_KEY } from './constants';
76

code/addons/docs/docs/multiframework.md

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ This dynamic rendering is framework-specific, meaning it needs a custom implemen
112112

113113
Let's take a look at the React framework implementation of `dynamic` snippets as a reference for implementing this feature in other frameworks:
114114

115+
TODO: understanding this better, should probably import from `preview-api` instead of `addons`?
116+
115117
```tsx
116118
import { addons, StoryContext } from '@storybook/addons';
117119
import { SNIPPET_RENDERED } from '../../shared';

code/addons/docs/docs/theming.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Storybook theming is the **recommended way** to theme your docs. Docs uses the s
1414
Supposing you have a Storybook theme defined for the main UI in `.storybook/manager.js`:
1515

1616
```js
17-
import { addons } from '@storybook/addons';
17+
import { addons } from '@storybook/manager-api';
1818
// or a custom theme
1919
import { themes } from '@storybook/theming';
2020

code/addons/highlight/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Highlight DOM nodes by emitting the `HIGHLIGHT` event from within a story or an
2828

2929
```js
3030
import React, { useEffect } from 'react';
31-
import { useChannel } from '@storybook/addons';
31+
import { useChannel } from '@storybook/preview-api';
3232
import { HIGHLIGHT, RESET_HIGHLIGHT } from '@storybook/addon-highlight';
3333
import { MyComponent } form './MyComponent';
3434

code/addons/interactions/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addons, types } from '@storybook/addons';
1+
import { addons, types } from '@storybook/manager-api';
22

33
import { ADDON_ID, PANEL_ID } from './constants';
44
import { Panel } from './Panel';

code/addons/jest/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { addons } from '@storybook/addons';
2+
import { addons } from '@storybook/manager-api';
33
import { ADDON_ID, PANEL_ID, PARAM_KEY } from './shared';
44

55
import Panel from './components/Panel';

code/addons/links/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"@storybook/client-logger": "7.0.0-alpha.53",
8181
"@storybook/core-events": "7.0.0-alpha.53",
8282
"@storybook/csf": "next",
83+
"@storybook/manager-api": "7.0.0-alpha.53",
8384
"@storybook/preview-api": "7.0.0-alpha.53",
8485
"@storybook/router": "7.0.0-alpha.53",
8586
"@storybook/types": "7.0.0-alpha.53",

code/addons/links/src/manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addons } from '@storybook/addons';
1+
import { addons } from '@storybook/manager-api';
22

33
import EVENTS, { ADDON_ID } from './constants';
44

code/addons/measure/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types } from '@storybook/manager-api';
33

44
import { ADDON_ID, TOOL_ID } from './constants';
55
import { Tool } from './Tool';

code/addons/outline/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types } from '@storybook/manager-api';
33

44
import { ADDON_ID } from './constants';
55
import { OutlineSelector } from './OutlineSelector';

code/addons/storyshots/storyshots-core/src/api/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import global from 'global';
2-
import { addons, mockChannel } from '@storybook/addons';
2+
import { addons, mockChannel } from '@storybook/preview-api';
33
import ensureOptionsDefaults from './ensureOptionsDefaults';
44
import snapshotsTests from './snapshotsTestsTemplate';
55
import integrityTest from './integrityTestTemplate';

code/addons/storysource/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { addons } from '@storybook/addons';
2+
import { addons } from '@storybook/manager-api';
33

44
import { StoryPanel } from './StoryPanel';
55
import { ADDON_ID, PANEL_ID } from './index';

code/addons/toolbars/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types } from '@storybook/manager-api';
33
import { ToolbarManager } from './components/ToolbarManager';
44
import { ADDON_ID } from './constants';
55

code/addons/viewport/src/manager.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { addons, types } from '@storybook/addons';
2+
import { addons, types } from '@storybook/manager-api';
33

44
import { ADDON_ID } from './constants';
55

code/lib/addons/README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# Storybook Addons
1+
The contents of this package have moved to `@storybook/preview-api` and `@storybook/manager-api`. Please update your import depending on where are are using this API.
22

3-
Storybook Addons is a node module which is used to load custom addons to storybook.
4-
5-
It stores addon loaders, communication channel and other resources which can be used by storybook implementations where required.
6-
7-
---
8-
9-
For more information visit: [storybook.js.org](https://storybook.js.org)
3+
This package will no longer be released as part of the 8.0 release of storybook.

code/lib/addons/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
//
77
// See main.ts
88

9-
import { addons } from './main';
9+
import { type Addon_Type, Addon_TypesEnum } from '@storybook/types';
10+
11+
export { type Addon_Type as Addon, Addon_TypesEnum as types };
1012

1113
export * from './make-decorator';
1214

1315
export * from './main';
1416
export * from './storybook-channel-mock';
1517
// eslint-disable-next-line import/no-cycle
1618
export * from './hooks';
17-
18-
export default addons;

code/lib/addons/src/main.ts

+1-133
Original file line numberDiff line numberDiff line change
@@ -1,133 +1 @@
1-
import global from 'global';
2-
3-
import type { Channel } from '@storybook/channels';
4-
import { SET_CONFIG } from '@storybook/core-events';
5-
import type { API } from '@storybook/manager-api';
6-
import type {
7-
Addon_Collection,
8-
Addon_Config,
9-
Addon_Elements,
10-
Addon_Loaders,
11-
Addon_Type,
12-
Addon_Types,
13-
} from '@storybook/types';
14-
import { Addon_TypesEnum } from '@storybook/types';
15-
import { logger } from '@storybook/client-logger';
16-
import { mockChannel } from './storybook-channel-mock';
17-
18-
export { Addon_Type as Addon, Addon_TypesEnum as types };
19-
20-
export function isSupportedType(type: Addon_Types): boolean {
21-
return !!Object.values(Addon_TypesEnum).find((typeVal) => typeVal === type);
22-
}
23-
24-
export class AddonStore {
25-
constructor() {
26-
this.promise = new Promise((res) => {
27-
this.resolve = () => res(this.getChannel());
28-
}) as Promise<Channel>;
29-
}
30-
31-
private loaders: Addon_Loaders<API> = {};
32-
33-
private elements: Addon_Elements = {};
34-
35-
private config: Addon_Config = {};
36-
37-
private channel: Channel | undefined;
38-
39-
private serverChannel: Channel | undefined;
40-
41-
private promise: any;
42-
43-
private resolve: any;
44-
45-
getChannel = (): Channel => {
46-
// this.channel should get overwritten by setChannel. If it wasn't called (e.g. in non-browser environment), set a mock instead.
47-
if (!this.channel) {
48-
this.setChannel(mockChannel());
49-
}
50-
51-
return this.channel;
52-
};
53-
54-
getServerChannel = (): Channel => {
55-
if (!this.serverChannel) {
56-
throw new Error('Accessing non-existent serverChannel');
57-
}
58-
59-
return this.serverChannel;
60-
};
61-
62-
ready = (): Promise<Channel> => this.promise;
63-
64-
hasChannel = (): boolean => !!this.channel;
65-
66-
hasServerChannel = (): boolean => !!this.serverChannel;
67-
68-
setChannel = (channel: Channel): void => {
69-
this.channel = channel;
70-
this.resolve();
71-
};
72-
73-
setServerChannel = (channel: Channel): void => {
74-
this.serverChannel = channel;
75-
};
76-
77-
getElements = (type: Addon_Types): Addon_Collection => {
78-
if (!this.elements[type]) {
79-
this.elements[type] = {};
80-
}
81-
return this.elements[type];
82-
};
83-
84-
addPanel = (name: string, options: Addon_Type): void => {
85-
this.add(name, {
86-
type: Addon_TypesEnum.PANEL,
87-
...options,
88-
});
89-
};
90-
91-
add = (name: string, addon: Addon_Type) => {
92-
const { type } = addon;
93-
const collection = this.getElements(type);
94-
collection[name] = { id: name, ...addon };
95-
};
96-
97-
setConfig = (value: Addon_Config) => {
98-
Object.assign(this.config, value);
99-
if (this.hasChannel()) {
100-
this.getChannel().emit(SET_CONFIG, value);
101-
}
102-
};
103-
104-
getConfig = () => this.config;
105-
106-
register = (name: string, registerCallback: (api: API) => void): void => {
107-
if (this.loaders[name]) {
108-
logger.warn(`${name} was loaded twice, this could have bad side-effects`);
109-
}
110-
this.loaders[name] = registerCallback;
111-
};
112-
113-
loadAddons = (api: any) => {
114-
Object.values(this.loaders).forEach((value) => value(api));
115-
};
116-
}
117-
118-
// Enforce addons store to be a singleton
119-
const KEY = '__STORYBOOK_ADDONS';
120-
121-
function getAddonsStore(): AddonStore {
122-
if (!global[KEY]) {
123-
global[KEY] = new AddonStore();
124-
}
125-
return global[KEY];
126-
}
127-
128-
// Exporting this twice in order to to be able to import it like { addons } instead of 'addons'
129-
// prefer import { addons } from '@storybook/addons' over import addons from '@storybook/addons'
130-
//
131-
// See public_api.ts
132-
133-
export const addons = getAddonsStore();
1+
export { addons, type AddonStore } from '@storybook/manager-api';
+1-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
import { Channel } from '@storybook/channels';
2-
3-
export function mockChannel() {
4-
const transport = {
5-
setHandler: () => {},
6-
send: () => {},
7-
};
8-
9-
return new Channel({ transport });
10-
}
1+
export { mockChannel } from '@storybook/manager-api';

code/lib/channels/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Transport {
3131

3232
Currently, channels are baked into storybook implementations and therefore this module is not designed to be used directly by addon developers. When developing addons, use the `getChannel` method exported by `@storybook/addons` module. For this to work, Storybook implementations should use the `setChannel` method before loading addons.
3333

34+
TODO: this needs to be updated to reflect the manager/preview split
35+
3436
```js
3537
import { addons } from '@storybook/addons';
3638

code/lib/instrumenter/src/instrumenter.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference types="@types/jest" />;
22
/* eslint-disable no-underscore-dangle */
33

4-
import { addons, mockChannel } from '@storybook/addons';
4+
import { addons, mockChannel } from '@storybook/preview-api';
55
import { logger } from '@storybook/client-logger';
66
import {
77
FORCE_REMOUNT,

code/lib/manager-api/src/index.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,8 @@ export function useArgTypes(): API_ArgTypes {
483483
}
484484

485485
export { eventToShortcut, shortcutToHumanString, shortcutMatchesShortcut };
486+
487+
export { addons } from './lib/addons';
488+
489+
/* deprecated */
490+
export { mockChannel, types, type Addon, type AddonStore } from './lib/addons';

0 commit comments

Comments
 (0)