-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5018 from storybooks/ts-migration/addons
Typescript-Migration: @storybook/addons
- Loading branch information
Showing
23 changed files
with
231 additions
and
166 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ lib/cli/test | |
*.bundle.js | ||
*.js.map | ||
*.ts | ||
*.tsx | ||
|
||
!.remarkrc.js | ||
!.babelrc.js | ||
|
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
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
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
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// Fixes 'noImplicitAny' lint error because @storybook/addons isn't migrated to typescript yet | ||
declare module '@storybook/addons'; | ||
|
||
// Only necessary for 0.x.x. Version 10.x.x has definition files included | ||
declare module '@emotion/styled'; | ||
declare module 'global'; |
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
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
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
This file was deleted.
Oops, something went wrong.
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,80 @@ | ||
import global from 'global'; | ||
import { Channel } from '@storybook/channels'; | ||
import { ReactElement } from 'react'; | ||
|
||
export interface PanelOptions { | ||
active: boolean; | ||
} | ||
|
||
export interface Panel { | ||
title: string; | ||
|
||
render(options: PanelOptions): ReactElement<any>; | ||
} | ||
|
||
export type Loader = (callback: (api: any) => void) => void; | ||
|
||
interface LoaderKeyValue { | ||
[key: string]: Loader; | ||
} | ||
|
||
interface PanelKeyValue { | ||
[key: string]: Panel; | ||
} | ||
|
||
export class AddonStore { | ||
private loaders: LoaderKeyValue = {}; | ||
private panels: PanelKeyValue = {}; | ||
private channel: Channel | undefined; | ||
|
||
getChannel() { | ||
// this.channel should get overwritten by setChannel. If it wasn't called (e.g. in non-browser environment), throw. | ||
if (!this.channel) { | ||
throw new Error( | ||
'Accessing nonexistent addons channel, see https://storybook.js.org/basics/faq/#why-is-there-no-addons-channel' | ||
); | ||
} | ||
|
||
return this.channel; | ||
} | ||
|
||
hasChannel() { | ||
return !!this.channel; | ||
} | ||
|
||
setChannel(channel: Channel) { | ||
this.channel = channel; | ||
} | ||
|
||
getPanels() { | ||
return this.panels; | ||
} | ||
|
||
addPanel(name: string, panel: Panel) { | ||
this.panels[name] = panel; | ||
} | ||
|
||
register(name: string, registerCallback: (api: any) => void) { | ||
this.loaders[name] = registerCallback; | ||
} | ||
|
||
loadAddons(api: any) { | ||
Object.values(this.loaders).forEach(value => value(api)); | ||
} | ||
} | ||
|
||
// Enforce addons store to be a singleton | ||
const KEY = '__STORYBOOK_ADDONS'; | ||
|
||
function getAddonsStore() { | ||
if (!global[KEY]) { | ||
global[KEY] = new AddonStore(); | ||
} | ||
return global[KEY]; | ||
} | ||
|
||
// Exporting this twice in order to to be able to import it like { addons } instead of 'addons' | ||
// prefer import { addons } from '@storybook/addons' over import addons from '@storybook/addons' | ||
// | ||
// See public_api.ts | ||
export const addons = getAddonsStore(); |
15 changes: 11 additions & 4 deletions
15
lib/addons/src/make-decorator.test.js → lib/addons/src/make-decorator.test.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
Oops, something went wrong.