Skip to content

Commit

Permalink
Merge pull request #9791 from storybookjs/ts/ui-slow
Browse files Browse the repository at this point in the history
MIGRATE lib/ui to Typescript

Self-merge @gaetanmaisse @kroeder
  • Loading branch information
ndelangen committed Feb 10, 2020
2 parents c208d80 + 643bc2e commit 23a46cb
Show file tree
Hide file tree
Showing 67 changed files with 2,243 additions and 2,098 deletions.
2 changes: 1 addition & 1 deletion addons/a11y/src/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ addons.register(ADDON_ID, api => {
addons.add(PANEL_ID, {
title: 'Accessibility',
type: types.PANEL,
render: ({ active, key }) => <A11YPanel key={key} api={api} active={active} />,
render: ({ active = true, key }) => <A11YPanel key={key} api={api} active={active} />,
paramKey: PARAM_KEY,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Parser } from 'acorn';
// @ts-ignore
import jsx from 'acorn-jsx';
// eslint-disable-next-line import/no-extraneous-dependencies
import estree from 'estree';
// @ts-ignore
import * as acornWalk from 'acorn-walk';
Expand Down
1 change: 0 additions & 1 deletion app/riot/src/client/preview/rendering/stringified.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable import/no-duplicates */
import { mount, unregister, tag2 as tag } from 'riot';
import * as riot from 'riot';
import compiler from 'riot-compiler';
Expand Down
6 changes: 3 additions & 3 deletions examples/angular-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"license": "MIT",
"scripts": {
"build": "ng build",
"prebuild-storybook": "npm run storybook:prebuild",
"prebuild-storybook": "yarn storybook:prebuild",
"build-storybook": "build-storybook -s src/assets",
"docs:json": "compodoc -p ./tsconfig.json -e json -d .",
"e2e": "ng e2e",
"ng": "ng",
"start": "ng serve",
"storybook": "npm run storybook:prebuild && start-storybook -p 9008 -s src/assets",
"storybook:prebuild": "npm run test:generate-output && npm run docs:json",
"storybook": "yarn storybook:prebuild && start-storybook -p 9008 -s src/assets",
"storybook:prebuild": "yarn test:generate-output && yarn docs:json",
"test": "jest",
"test:coverage": "jest --coverage",
"test:generate-output": "jest --json --config=jest.addon-config.js --outputFile=addon-jest.testresults.json || true",
Expand Down
2 changes: 1 addition & 1 deletion examples/marko-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"serve-static": "NODE_ENV=production marko-starter serve-static",
"start": "marko-starter server",
"storybook": "start-storybook -p 9005",
"test": "npm run lint"
"test": "yarn lint"
},
"dependencies": {
"marko": "^4.18.39",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Object {
"__": null,
"__b": 0,
"__c": null,
"__d": null,
"__d": undefined,
"__e": null,
"__k": null,
"constructor": undefined,
Expand All @@ -15,7 +15,7 @@ Object {
"__": null,
"__b": 0,
"__c": null,
"__d": null,
"__d": undefined,
"__e": null,
"__k": null,
"constructor": undefined,
Expand Down
1 change: 1 addition & 0 deletions lib/addons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"util-deprecate": "^1.0.2"
},
"devDependencies": {
"@reach/router": "^1.2.1",
"@types/util-deprecate": "^1.0.0"
},
"peerDependencies": {
Expand Down
18 changes: 15 additions & 3 deletions lib/addons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import { ReactElement } from 'react';
import { Channel } from '@storybook/channels';
import { API } from '@storybook/api';
import { logger } from '@storybook/client-logger';
// eslint-disable-next-line import/no-extraneous-dependencies
import { WindowLocation } from '@reach/router';
import { types, Types } from './types';

export type ViewMode = 'story' | 'info' | 'settings' | undefined | string;

export interface RenderOptions {
active: boolean;
key: string;
active?: boolean;
key?: string;
}
export interface RouteOptions {
storyId: string;
viewMode: ViewMode;
location: WindowLocation;
path: string;
}
export interface MatchOptions {
viewMode: string;
storyId: string;
viewMode: ViewMode;
location: WindowLocation;
path: string;
}

export interface Addon {
Expand All @@ -25,6 +35,8 @@ export interface Addon {
match?: (matchOptions: MatchOptions) => boolean;
render: (renderOptions: RenderOptions) => ReactElement<any>;
paramKey?: string;
disabled?: boolean;
hidden?: boolean;
}

export type Loader = (api: API) => void;
Expand Down
1 change: 1 addition & 0 deletions lib/addons/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum types {
TAB = 'tab',
PANEL = 'panel',
TOOL = 'tool',
TOOLEXTRA = 'toolextra',
PREVIEW = 'preview',
NOTES_ELEMENT = 'notes-element',
}
Expand Down
21 changes: 14 additions & 7 deletions lib/api/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import React, { ReactElement, Component, useContext, useEffect, useMemo, useRef } from 'react';
import React, {
ReactElement,
Component,
useContext,
useEffect,
useMemo,
useRef,
ReactNode,
} from 'react';
import memoize from 'memoizerific';
// @ts-ignore shallow-equal is not in DefinitelyTyped
import shallowEqualObjects from 'shallow-equal/objects';
Expand Down Expand Up @@ -104,7 +112,7 @@ interface StoreData {
}

interface Children {
children: Component | ((props: Combo) => Component);
children: ReactNode | ((props: Combo) => ReactNode);
}

type StatePartial = Partial<State>;
Expand Down Expand Up @@ -248,11 +256,10 @@ class ManagerProvider extends Component<Props, State> {
api: this.api,
};

return (
<ManagerContext.Provider value={value}>
{typeof children === 'function' ? children(value) : children}
</ManagerContext.Provider>
);
// @ts-ignore
const content: ReactNode = typeof children === 'function' ? children(value) : children;

return <ManagerContext.Provider value={value}>{content}</ManagerContext.Provider>;
}
}

Expand Down
15 changes: 12 additions & 3 deletions lib/api/src/init-provider-api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { ReactElement } from 'react';
import { ReactNode } from 'react';
import { Channel } from '@storybook/channels';

import { API } from './index';
import { API, State } from './index';
import Store from './store';

type IframeRenderer = (
storyId: string,
viewMode: State['viewMode'],
id: string,
baseUrl: string,
scale: number,
queryParams: Record<string, any>
) => ReactNode;

export interface Provider {
channel?: Channel;
renderPreview?: () => ReactElement;
renderPreview?: IframeRenderer;
handleAPI(api: API): void;
[key: string]: any;
}
Expand Down
13 changes: 12 additions & 1 deletion lib/api/src/modules/addons.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { ReactElement } from 'react';

import { WindowLocation } from '@reach/router';
import { Module } from '../index';
import { Options } from '../store';

export type ViewMode = 'story' | 'info' | 'settings' | undefined | string;

export enum types {
TAB = 'tab',
PANEL = 'panel',
Expand All @@ -19,9 +22,15 @@ export interface RenderOptions {

export interface RouteOptions {
storyId: string;
viewMode: ViewMode;
location: WindowLocation;
path: string;
}
export interface MatchOptions {
viewMode: string;
storyId: string;
viewMode: ViewMode;
location: WindowLocation;
path: string;
}

export interface Addon {
Expand All @@ -32,6 +41,8 @@ export interface Addon {
match?: (matchOptions: MatchOptions) => boolean;
render: (renderOptions: RenderOptions) => ReactElement<any>;
paramKey?: string;
disabled?: boolean;
hidden?: boolean;
}
export interface Collection {
[key: string]: Addon;
Expand Down
2 changes: 2 additions & 0 deletions lib/api/src/modules/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Module } from '../index';

export interface Notification {
id: string;
link: string;
content: string;
onClear?: () => void;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Direction = -1 | 1;
type StoryId = string;
type ParameterName = string;

type ViewMode = 'story' | 'info' | 'settings' | undefined;
type ViewMode = 'story' | 'info' | 'settings' | undefined | string;

export interface SubState {
storiesHash: StoriesHash;
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/modules/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Module, API } from '../index';

export interface Version {
version: string;
info?: string;
info?: { plain: string };
[key: string]: any;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/components/src/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export interface TabsProps {
selected?: string;
actions?: {
onSelect: (id: string) => void;
};
} & Record<string, any>;
backgroundColor?: string;
absolute?: boolean;
bordered?: boolean;
Expand Down
3 changes: 1 addition & 2 deletions lib/core-events/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable import/no-duplicates */
import * as EventsPackageExport from '.';
import * as EventsPackageExport from './index';
import EventsDefaultExport, { CHANNEL_CREATED } from './index';

describe('Core Events', () => {
Expand Down
8 changes: 4 additions & 4 deletions lib/router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ interface QueryMatchProps {
}
interface RouteProps {
path: string;
startsWith: boolean;
hideOnly: boolean;
children: (renderData: RenderData) => ReactNode;
startsWith?: boolean;
hideOnly?: boolean;
children: ReactNode;
}

interface QueryLinkProps {
export interface QueryLinkProps {
to: string;
children: ReactNode;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@storybook/core-events": "6.0.0-alpha.9",
"@storybook/router": "6.0.0-alpha.9",
"@storybook/theming": "6.0.0-alpha.9",
"@types/markdown-to-jsx":"^6.9.1",
"copy-to-clipboard": "^3.0.8",
"core-js": "^3.0.1",
"core-js-pure": "^3.0.1",
Expand Down Expand Up @@ -63,6 +64,7 @@
"util-deprecate": "^1.0.2"
},
"devDependencies": {
"@storybook/react": "6.0.0-alpha.9",
"@storybook/addon-actions": "6.0.0-alpha.9",
"@storybook/addon-knobs": "6.0.0-alpha.9",
"corejs-upgrade-webpack-plugin": "^3.0.1",
Expand Down
36 changes: 15 additions & 21 deletions lib/ui/src/app.js → lib/ui/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import React, { FunctionComponent } from 'react';
import { Global, createGlobal, styled } from '@storybook/theming';
import memoize from 'memoizerific';
import sizeMe from 'react-sizeme';

import { Route } from '@storybook/router';

import { State } from '@storybook/api';
import { Mobile } from './components/layout/mobile';
import { Desktop } from './components/layout/desktop';
import Nav from './containers/nav';
Expand All @@ -24,12 +24,11 @@ const createProps = memoize(1)(() => ({
{
key: 'settings',
render: () => <SettingsPages />,
// eslint-disable-next-line react/prop-types
route: ({ children }) => (
route: (({ children }) => (
<Route path="/settings" startsWith>
{children}
</Route>
),
)) as FunctionComponent,
},
],
}));
Expand All @@ -41,7 +40,16 @@ const View = styled.div({
width: '100vw',
});

const App = React.memo(({ viewMode, docsOnly, layout, panelCount, size: { width, height } }) => {
const App = React.memo<{
viewMode: State['viewMode'];
docsOnly: boolean;
layout: State['layout'];
panelCount: number;
size: {
width: number;
height: number;
};
}>(({ viewMode, docsOnly, layout, panelCount, size: { width, height } }) => {
const props = createProps();

let content;
Expand All @@ -53,7 +61,7 @@ const App = React.memo(({ viewMode, docsOnly, layout, panelCount, size: { width,
</div>
);
} else if (width < 600) {
content = <Mobile {...props} viewMode={viewMode} options={layout} panelCount={panelCount} />;
content = <Mobile {...props} viewMode={viewMode} options={layout} />;
} else {
content = (
<Desktop
Expand All @@ -74,20 +82,6 @@ const App = React.memo(({ viewMode, docsOnly, layout, panelCount, size: { width,
</View>
);
});
App.propTypes = {
viewMode: PropTypes.string,
panelCount: PropTypes.number.isRequired,
layout: PropTypes.shape({}).isRequired,
size: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number,
}).isRequired,
docsOnly: PropTypes.bool,
};
App.defaultProps = {
viewMode: undefined,
docsOnly: false,
};

const SizedApp = sizeMe({ monitorHeight: true })(App);

Expand Down
10 changes: 10 additions & 0 deletions lib/ui/src/components/layout/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { styled } from '@storybook/theming';

export const Root = styled.div({
position: 'fixed',
left: 0,
top: 0,
width: '100vw',
height: '100vh',
overflow: 'hidden',
});

0 comments on commit 23a46cb

Please sign in to comment.