Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: improve HTML framework's generated typescript stories #18509

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ npx sb init -t html

For more information visit: [storybook.js.org](https://storybook.js.org)

### Typescript

`npx sb init` will select `.ts` starter stories if your `package.json` has typescript as a dependency. If starting a new project,
run `npm init` and `npm install typescript --save-dev` before initializing storybook to get the typescript starter stories.

---

Storybook also comes with a lot of [addons](https://storybook.js.org/addons) and a great API to customize as you wish.
Expand Down
6 changes: 3 additions & 3 deletions lib/cli/src/frameworks/html/ts/Button.stories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Story, Meta } from '@storybook/html';
import { StoryFn, Meta } from '@storybook/html';
import { createButton, ButtonProps } from './Button';

// More on default export: https://storybook.js.org/docs/html/writing-stories/introduction#default-export
Expand All @@ -15,10 +15,10 @@ export default {
options: ['small', 'medium', 'large'],
},
},
} as Meta;
} as Meta<ButtonProps>;

// More on component templates: https://storybook.js.org/docs/html/writing-stories/introduction#using-args
const Template: Story<ButtonProps> = (args) => {
const Template: StoryFn<ButtonProps> = (args) => {
// You can either use a function to create DOM elements or use a plain html string!
// return `<div>${label}</div>`;
return createButton(args);
Expand Down
8 changes: 6 additions & 2 deletions lib/cli/src/frameworks/html/ts/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ export const createButton = ({
const btn = document.createElement('button');
btn.type = 'button';
btn.innerText = label;
btn.addEventListener('click', onClick);
if (onClick) {
btn.addEventListener('click', onClick);
}

const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
btn.className = ['storybook-button', `storybook-button--${size}`, mode].join(' ');

btn.style.backgroundColor = backgroundColor;
if (backgroundColor) {
btn.style.backgroundColor = backgroundColor;
}

return btn;
};
6 changes: 3 additions & 3 deletions lib/cli/src/frameworks/html/ts/Header.stories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Story, Meta } from '@storybook/html';
import { StoryFn, Meta } from '@storybook/html';
import { createHeader, HeaderProps } from './Header';

export default {
Expand All @@ -13,9 +13,9 @@ export default {
onLogout: { action: 'onLogout' },
onCreateAccount: { action: 'onCreateAccount' },
},
} as Meta;
} as Meta<HeaderProps>;

const Template: Story<HeaderProps> = (args) => createHeader(args);
const Template: StoryFn<HeaderProps> = (args) => createHeader(args);

export const LoggedIn = Template.bind({});
LoggedIn.args = {
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/src/frameworks/html/ts/Page.stories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { within, userEvent } from '@storybook/testing-library';
import { Story, Meta } from '@storybook/html';
import { StoryFn, Meta } from '@storybook/html';
import { createPage } from './Page';

export default {
Expand All @@ -10,7 +10,7 @@ export default {
},
} as Meta;

const Template: Story = () => createPage();
const Template: StoryFn = () => createPage();

export const LoggedOut = Template.bind({});

Expand Down
8 changes: 4 additions & 4 deletions lib/cli/src/frameworks/html/ts/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ type User = {

export const createPage = () => {
const article = document.createElement('article');
let user: User = null;
let header = null;
let user: User | undefined;
let header: HTMLElement | null = null;

const rerenderHeader = () => {
const wrapper = document.getElementsByTagName('article')[0];
wrapper.replaceChild(createHeaderElement(), wrapper.firstChild);
wrapper.replaceChild(createHeaderElement(), wrapper.firstChild as HTMLElement);
};

const onLogin = () => {
Expand All @@ -21,7 +21,7 @@ export const createPage = () => {
};

const onLogout = () => {
user = null;
user = undefined;
rerenderHeader();
};

Expand Down