Skip to content

Commit f5d69aa

Browse files
authored
Merge pull request #9879 from storybookjs/ts/improvements
TS improvements
2 parents 1b0ce17 + b1500ab commit f5d69aa

File tree

6 files changed

+42
-37
lines changed

6 files changed

+42
-37
lines changed

lib/addons/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@storybook/channels": "6.0.0-alpha.13",
3232
"@storybook/client-logger": "6.0.0-alpha.13",
3333
"@storybook/core-events": "6.0.0-alpha.13",
34+
"@storybook/router": "6.0.0-alpha.13",
3435
"core-js": "^3.0.1",
3536
"global": "^4.3.2",
3637
"util-deprecate": "^1.0.2"

lib/addons/src/index.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,21 @@ import global from 'global';
33
import { ReactElement } from 'react';
44
import { Channel } from '@storybook/channels';
55
import { API } from '@storybook/api';
6+
import { RenderData as RouterData } from '@storybook/router';
67
import { logger } from '@storybook/client-logger';
7-
// eslint-disable-next-line import/no-extraneous-dependencies
8-
import { WindowLocation } from '@reach/router';
98
import { types, Types } from './types';
109

11-
export type ViewMode = 'story' | 'info' | 'settings' | undefined | string;
12-
1310
export interface RenderOptions {
1411
active?: boolean;
1512
key?: string;
1613
}
17-
export interface RouteOptions {
18-
storyId: string;
19-
viewMode: ViewMode;
20-
location: WindowLocation;
21-
path: string;
22-
}
23-
export interface MatchOptions {
24-
storyId: string;
25-
viewMode: ViewMode;
26-
location: WindowLocation;
27-
path: string;
28-
}
2914

3015
export interface Addon {
3116
title: string;
3217
type?: Types;
3318
id?: string;
34-
route?: (routeOptions: RouteOptions) => string;
35-
match?: (matchOptions: MatchOptions) => boolean;
19+
route?: (routeOptions: RouterData) => string;
20+
match?: (matchOptions: RouterData) => boolean;
3621
render: (renderOptions: RenderOptions) => ReactElement<any>;
3722
paramKey?: string;
3823
disabled?: boolean;

lib/api/src/init-provider-api.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { ReactNode } from 'react';
22
import { Channel } from '@storybook/channels';
3+
import { ThemeVars } from '@storybook/theming';
34

45
import { API, State } from './index';
56
import Store from './store';
7+
import { UIOptions } from './modules/layout';
68

79
type IframeRenderer = (
810
storyId: string,
@@ -17,7 +19,10 @@ export interface Provider {
1719
channel?: Channel;
1820
renderPreview?: IframeRenderer;
1921
handleAPI(api: API): void;
20-
getConfig(): Record<string, any>;
22+
getConfig(): {
23+
theme?: ThemeVars;
24+
[k: string]: any;
25+
} & Partial<UIOptions>;
2126
[key: string]: any;
2227
}
2328

lib/api/src/lib/stories.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export interface Root {
1414
isRoot: true;
1515
isLeaf: false;
1616
// MDX stories are "Group" type
17-
parameters?: any;
17+
parameters?: {
18+
docsOnly?: boolean;
19+
[k: string]: any;
20+
};
1821
}
1922

2023
export interface Group {
@@ -27,7 +30,10 @@ export interface Group {
2730
isRoot: false;
2831
isLeaf: false;
2932
// MDX stories are "Group" type
30-
parameters?: any;
33+
parameters?: {
34+
docsOnly?: boolean;
35+
[k: string]: any;
36+
};
3137
}
3238

3339
export interface Story {
@@ -48,6 +54,7 @@ export interface Story {
4854
showRoots?: boolean;
4955
[k: string]: any;
5056
};
57+
docsOnly?: boolean;
5158
[k: string]: any;
5259
};
5360
}
@@ -65,6 +72,7 @@ export interface StoryInput {
6572
showRoots?: boolean;
6673
[key: string]: any;
6774
};
75+
docsOnly?: boolean;
6876
[parameterName: string]: any;
6977
};
7078
isLeaf: boolean;

lib/api/src/modules/layout.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ export interface SubAPI {
5454
type PartialSubState = Partial<SubState>;
5555
type PartialThemeVars = Partial<ThemeVars>;
5656
type PartialLayout = Partial<Layout>;
57-
type PartialUI = Partial<UI>;
5857

59-
interface Options extends ThemeVars {
58+
export interface UIOptions {
6059
name?: string;
6160
url?: string;
6261
goFullScreen: boolean;
@@ -96,16 +95,19 @@ const deprecationMessage = (optionsMap: OptionsMap, prefix = '') =>
9695
prefix ? `${prefix}'s` : ''
9796
} { ${Object.values(optionsMap).join(', ')} } instead.`;
9897

99-
const applyDeprecatedThemeOptions = deprecate(({ name, url, theme }: Options): PartialThemeVars => {
100-
const { brandTitle, brandUrl, brandImage }: PartialThemeVars = theme || {};
101-
return {
102-
brandTitle: brandTitle || name,
103-
brandUrl: brandUrl || url,
104-
brandImage: brandImage || null,
105-
};
106-
}, deprecationMessage(deprecatedThemeOptions));
98+
const applyDeprecatedThemeOptions = deprecate(
99+
({ name, url, theme }: UIOptions): PartialThemeVars => {
100+
const { brandTitle, brandUrl, brandImage }: PartialThemeVars = theme || {};
101+
return {
102+
brandTitle: brandTitle || name,
103+
brandUrl: brandUrl || url,
104+
brandImage: brandImage || null,
105+
};
106+
},
107+
deprecationMessage(deprecatedThemeOptions)
108+
);
107109

108-
const applyDeprecatedLayoutOptions = deprecate((options: Partial<Options>): PartialLayout => {
110+
const applyDeprecatedLayoutOptions = deprecate((options: Partial<UIOptions>): PartialLayout => {
109111
const layoutUpdate: PartialLayout = {};
110112

111113
['goFullScreen', 'showStoriesPanel', 'showAddonPanel'].forEach(
@@ -123,14 +125,14 @@ const applyDeprecatedLayoutOptions = deprecate((options: Partial<Options>): Part
123125
return layoutUpdate;
124126
}, deprecationMessage(deprecatedLayoutOptions));
125127

126-
const checkDeprecatedThemeOptions = (options: Options) => {
128+
const checkDeprecatedThemeOptions = (options: UIOptions) => {
127129
if (Object.keys(deprecatedThemeOptions).find(v => v in options)) {
128130
return applyDeprecatedThemeOptions(options);
129131
}
130132
return {};
131133
};
132134

133-
const checkDeprecatedLayoutOptions = (options: Partial<Options>) => {
135+
const checkDeprecatedLayoutOptions = (options: Partial<UIOptions>) => {
134136
if (Object.keys(deprecatedLayoutOptions).find(v => v in options)) {
135137
return applyDeprecatedLayoutOptions(options);
136138
}

lib/router/src/router.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import {
77
navigate,
88
LocationProvider,
99
RouteComponentProps,
10+
LocationContext,
1011
NavigateFn,
1112
} from '@reach/router';
1213
import { ToggleVisibility } from './visibility';
1314
import { queryFromString, parsePath, getMatch } from './utils';
1415

1516
interface Other {
16-
viewMode?: string;
17-
storyId?: string;
17+
viewMode: string;
18+
storyId: string;
19+
path: string;
1820
}
1921

20-
export type RenderData = RouteComponentProps & Other;
22+
export type RenderData = Pick<LocationContext, 'location'> &
23+
Partial<Pick<LocationContext, 'navigate'>> &
24+
Other;
2125

2226
interface MatchingData {
2327
match: null | { path: string };

0 commit comments

Comments
 (0)