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

Setup Storybook for testing UI components #1497

Merged
merged 8 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitattributes
Expand Up @@ -18,4 +18,8 @@ yarn.lock merge=binary
# https://mirrors.edge.kernel.org/pub/software/scm/git/docs/gitattributes.html
# suggests that this might interleave lines arbitrarily, but empirically
# it keeps added chunks contiguous
CHANGELOG.md merge=union
CHANGELOG.md merge=union

# Mark some JSON files containing test data as generated so they are not included
# as part of diffs or language statistics.
extensions/ql-vscode/src/stories/remote-queries/data/*.json linguist-generated
8 changes: 8 additions & 0 deletions .vscode/launch.json
Expand Up @@ -124,6 +124,14 @@
"outFiles": [
"${workspaceRoot}/extensions/ql-vscode/out/**/*.js",
],
},
{
"name": "Launch Storybook",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}/extensions/ql-vscode",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "storybook"]
}
]
}
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -77,6 +77,18 @@ $ vscode/scripts/code-cli.sh --install-extension dist/vscode-codeql-*.vsix # if

You can use VS Code to debug the extension without explicitly installing it. Just open this directory as a workspace in VS Code, and hit `F5` to start a debugging session.

### Storybook

You can use [Storybook](https://storybook.js.org/) to preview React components outside VSCode. Inside the `extensions/ql-vscode` directory, run:

```shell
npm run storybook
```

Your browser should automatically open to the Storybook UI. Stories live in the `src/stories` directory.

Alternatively, you can start Storybook inside of VSCode. There is a VSCode launch configuration for starting Storybook. It can be found in the debug view.

### Running the unit tests and integration tests that do not require a CLI instance

Unit tests and many integration tests do not require a copy of the CodeQL CLI.
Expand Down
1 change: 1 addition & 0 deletions extensions/ql-vscode/.npmrc
@@ -0,0 +1 @@
legacy-peer-deps=true
koesie10 marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions extensions/ql-vscode/.storybook/main.js
@@ -0,0 +1,15 @@
module.exports = {
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions"
],
"framework": "@storybook/react",
"core": {
"builder": "@storybook/builder-webpack5"
}
};
7 changes: 7 additions & 0 deletions extensions/ql-vscode/.storybook/manager.js
@@ -0,0 +1,7 @@
import { addons } from '@storybook/addons';
import { themes } from '@storybook/theming';

addons.setConfig({
theme: themes.dark,
enableShortcuts: false,
});
38 changes: 38 additions & 0 deletions extensions/ql-vscode/.storybook/preview.js
@@ -0,0 +1,38 @@
import { themes } from '@storybook/theming';
import { action } from '@storybook/addon-actions';

// Allow all stories/components to use Codicons
import '@vscode/codicons/dist/codicon.css';
charisk marked this conversation as resolved.
Show resolved Hide resolved

import '../src/stories/vscode-theme.css';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having our own file that we have to manage/keep up-to date etc., is there a way to get everything from VSCode automatically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be able to generate it from the VSCode source code, but I don't think we can import it from VSCode itself.

The colors are defined here and the styles are created here. They are then injected into the WebView using this on the host side and this on the WebView side.

I think it will be a lot more work to generate the CSS variables from this than to copy the variables from VSCode once in a while.

Copy link
Contributor

@charisk charisk Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that's a shame. I suspected it would be a can of worms, but I thought I'd ask anyway 😂

In that case I think we should have some docs to tell people about this (maybe in the storybook part in the README?) and how to update them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I can also add it to an Overview page in Storybook itself and refer to that from the CONTRIBUTING documentation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had a brainwave! Feel free to ignore this if you want (totally up to you), but the actual toolkit has a storybook, so perhaps this is something they've solved already? See https://github.com/microsoft/vscode-webview-ui-toolkit/tree/main/.storybook

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did see whether it was possible to replicate the way they are using the VSCode variables, but they are using a completely custom system. They have a system of "design tokens" (https://github.com/microsoft/vscode-webview-ui-toolkit/blob/6fc339ce26e7da15b9ba97c063f56d8a4eae85ad/src/design-tokens.ts) which specify a variable and a default. These are then used in the styles and will automatically use either the variable or the default. I don't think that is going to work for us since we'd need to modify the way we write the CSS files and set defaults for all variables we use. It could also result in having different defaults than the toolkit, so it might also get out of sync at some point.


export const parameters = {
koesie10 marked this conversation as resolved.
Show resolved Hide resolved
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
docs: {
theme: themes.dark,
},
backgrounds: {
default: 'dark',
values: [
{
name: 'dark',
value: '#1e1e1e',
},
],
},
options: {
storySort: {
order: ['WebView UI Toolkit', 'MRVA'],
koesie10 marked this conversation as resolved.
Show resolved Hide resolved
koesie10 marked this conversation as resolved.
Show resolved Hide resolved
},
},
};

window.acquireVsCodeApi = () => ({
postMessage: action('post-vscode-message')
});