Skip to content

Commit

Permalink
storybook v8
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrikMatiasko committed Apr 13, 2024
1 parent 961d2eb commit 5f4141f
Show file tree
Hide file tree
Showing 19 changed files with 661 additions and 261 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build/*
node_modules/*
src/react-app-env.d.ts
src/declarations.d.ts
src/components/Atomic/Icon/components/*
src/components/Atomic/Icon/components/*
!.storybook
13 changes: 12 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ module.exports = {
'no-useless-constructor': 'off',
'@typescript-eslint/no-useless-constructor': 'warn'
}
}],
},
{
// or whatever matches stories specified in .storybook/main.js
"files": ['*.stories.@(ts|tsx|js|jsx|mjs|cjs)'],
"rules": {
// example of overriding a rule
'storybook/hierarchy-separator': 'error',
// example of disabling a rule
'storybook/default-exports': 'off',
}
}
],
rules: {
// http://eslint.org/docs/rules/
'array-callback-return': 'warn',
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

*storybook.log
test-report.xml
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"

npm run test
87 changes: 39 additions & 48 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,45 @@
import { dirname, join } from "path";
import type { StorybookConfig } from '@storybook/react-webpack5';
import type { StorybookConfig } from '@storybook/react-webpack5'

import { join, dirname } from 'path'

/**
* This function is used to resolve the absolute path of a package.
* It is needed in projects that use Yarn PnP or are set up within a monorepo.
*/
function getAbsolutePath(value: string): any {
return dirname(require.resolve(join(value, 'package.json')))
}
const config: StorybookConfig = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
getAbsolutePath("@storybook/preset-scss"),
getAbsolutePath("@storybook/addon-links"),
getAbsolutePath("@storybook/addon-essentials"),
getAbsolutePath("@storybook/addon-interactions"),
{
name: '@storybook/addon-styling',
options: {
sass: {
// Require your Sass preprocessor here
implementation: require('sass'),
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: [
getAbsolutePath('@storybook/addon-webpack5-compiler-swc'),
getAbsolutePath('@storybook/addon-onboarding'),
getAbsolutePath('@storybook/addon-links'),
getAbsolutePath('@storybook/addon-essentials'),
getAbsolutePath('@chromatic-com/storybook'),
getAbsolutePath('@storybook/addon-interactions'),
getAbsolutePath('@storybook/addon-themes'),
],
framework: {
name: getAbsolutePath('@storybook/react-webpack5'),
options: {},
},
babel: async (options:any) => {
options.plugins.push('babel-plugin-inline-react-svg')
return options;
},
swc: () => ({
jsc: {
transform: {
react: {
runtime: 'automatic',
},
},
},
},
}),
staticDirs: ['../public'],
docs: {
autodocs: false,
},
getAbsolutePath("storybook-addon-themes")
],
framework: {
name: getAbsolutePath("@storybook/react-webpack5"),

options: {
builder: {
fsCache: true,
lazyCompilation: true
}
}
},
babel: async (options:any) => {
options.plugins.push('babel-plugin-inline-react-svg')
return options;
},
// webpackFinal: async config => {
// config.resolve.alias = {
// ...(config.resolve.alias || [])
// };
// config.resolve.extensions.push('.ts', '.tsx')
// return config
// },
core: {},
staticDirs: ['../public'],
docs: {
autodocs: false
}
}

export default config

function getAbsolutePath(value: string): any {
return dirname(require.resolve(join(value, "package.json")));
}
3 changes: 0 additions & 3 deletions .storybook/preview-body.html
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
<div id="root" class='main-root'></div>
<div id="modal-root"></div>
<div id="modal-floating"></div>
63 changes: 0 additions & 63 deletions .storybook/preview.js

This file was deleted.

44 changes: 44 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react'
import type { Preview } from '@storybook/react'
import {ThemeProvider} from "@emotion/react";
import { withThemeByClassName } from '@storybook/addon-themes'

import plgd from '../src/components/Atomic/_theme/plgd'
import siemens from "../src/components/Atomic/_theme/siemens";
import Aoo from "../src/components/Atomic/App/App";

const preview: Preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
}

export default preview


const withTheme = (StoryFn: any, context: any) => {
const getThemeByKey = (themeName: string) => themeName === 'plgd' ? plgd : siemens

return (
<ThemeProvider theme={getThemeByKey(context.globals.theme)}>
<Aoo>
<StoryFn/>
</Aoo>
<div id="modal-root"></div>
<div id="modal-floating"></div>
</ThemeProvider>
)
}

export const decorators = [withTheme, withThemeByClassName({
themes: {
plgd: 'plgd-theme',
siemens: 'siemens-theme',
},
defaultTheme: 'plgd',
})]
14 changes: 14 additions & 0 deletions .storybook/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require("path");

module.exports = function({ config }: any) {
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve("babel-loader"),
options: {
presets: [["react-app", { flow: false, typescript: true }], require.resolve("@emotion/babel-preset-css-prop")],
},
});

config.resolve.extensions.push(".ts", ".tsx");
return config;
};
35 changes: 17 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
":lint:eslint": "eslint --ext .js,.jsx,.ts,.tsx -c .eslintrc.js --max-warnings 0 --fix --format=pretty ./src",
":lint:prettier": "prettier ./src --check",
"prebuild": "rimraf build",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"deploy-storybook": "storybook-to-ghpages --dry-run",
"icons:create": "npx @svgr/cli --config-file ./config/.svgrrc.js -d ./src/components/Atomic/Icon/components ./src/components/Atomic/Icon/assets",
":generate:theme:build": "node ./scripts/build.theme.js",
":generate:theme:clean": "rimraf ./build/lib",
":generate:theme": "npm-run-all :generate:theme:build :generate:theme:clean"
":generate:theme": "npm-run-all :generate:theme:build :generate:theme:clean",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"deploy-storybook": "storybook-to-ghpages --dry-run"
},
"dependencies": {
"@babel/core": "^7.23.3",
Expand Down Expand Up @@ -58,7 +58,6 @@
"@opentelemetry/instrumentation-xml-http-request": "^0.45.1",
"@opentelemetry/sdk-trace-base": "^1.18.1",
"@opentelemetry/sdk-trace-web": "^1.18.1",
"@storybook/addons": "^7.6.13",
"@types/convert-units": "^2.3.9",
"@types/dompurify": "^3.0.2",
"@types/lodash": "^4.14.202",
Expand Down Expand Up @@ -119,17 +118,18 @@
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/plugin-transform-runtime": "^7.23.7",
"@babel/runtime": "^7.23.8",
"@chromatic-com/storybook": "^1.3.2",
"@emotion/jest": "^11.11.0",
"@storybook/addon-actions": "^7.6.13",
"@storybook/addon-essentials": "^7.6.13",
"@storybook/addon-interactions": "^7.6.13",
"@storybook/addon-links": "^7.6.13",
"@storybook/addon-styling": "^1.3.7",
"@storybook/preset-scss": "^1.0.3",
"@storybook/react": "^7.6.13",
"@storybook/react-webpack5": "^7.6.13",
"@storybook/storybook-deployer": "^2.8.16",
"@storybook/testing-library": "^0.2.2",
"@storybook/addon-essentials": "^8.0.8",
"@storybook/addon-interactions": "^8.0.8",
"@storybook/addon-links": "^8.0.8",
"@storybook/addon-onboarding": "^8.0.8",
"@storybook/addon-themes": "^8.0.8",
"@storybook/addon-webpack5-compiler-swc": "^1.0.2",
"@storybook/blocks": "^8.0.8",
"@storybook/react": "^8.0.8",
"@storybook/react-webpack5": "^8.0.8",
"@storybook/test": "^8.0.8",
"@svgr/cli": "^8.1.0",
"@svgr/webpack": "^8.1.0",
"@testing-library/jest-dom": "^6.1.4",
Expand All @@ -152,7 +152,7 @@
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-storybook": "^0.6.15",
"eslint-plugin-storybook": "^0.8.0",
"eslint-plugin-testing-library": "^6.0.2",
"fast-glob": "^3.3.2",
"fs-extra": "^11.1.1",
Expand All @@ -171,8 +171,7 @@
"rimraf": "^5.0.5",
"sass": "^1.69.5",
"sass-loader": "^13.3.2",
"storybook": "^7.6.13",
"storybook-addon-themes": "^6.1.0",
"storybook": "^8.0.8",
"style-loader": "^3.3.3",
"ts-jest": "^29.1.1",
"util": "^0.12.5",
Expand Down
23 changes: 23 additions & 0 deletions src/components/Atomic/CodeEditor/CodeEditor.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { render } from '@testing-library/react'
import CodeEditor from './CodeEditor'

describe('<CodeEditor>', () => {
it('renders without crashing', () => {
const { container, asFragment } = render(<CodeEditor value='' />)
expect(container).toBeInTheDocument()

expect(asFragment()).toMatchSnapshot()
})

it('displays placeholder text when value is empty', () => {
const { getByText, asFragment } = render(<CodeEditor placeholderText='Placeholder' value='' />)
expect(getByText('Placeholder')).toBeInTheDocument()

expect(asFragment()).toMatchSnapshot()
})

it('does not display placeholder text when value is not empty', () => {
const { queryByText } = render(<CodeEditor placeholderText='Placeholder' value='Some value' />)
expect(queryByText('Placeholder')).not.toBeInTheDocument()
})
})
3 changes: 2 additions & 1 deletion src/components/Atomic/CodeEditor/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ const CodeEditor: FC<Props> = (props) => {
extensions={extensions}
height={getSizeInPx(height!)}
onChange={(v) => handleChange(v)}
readOnly={disabled === true}
theme={theme}
value={value}
/>
{placeholderText && (
<div css={styles.placeholder}>
<div css={[styles.flex, !showPlaceholder && styles.noPlaceholder]}>
<IconFileUpload {...convertSize(50)} />
<p css={styles.placeholderText}>{placeholderText}</p>
{showPlaceholder && <p css={styles.placeholderText}>{placeholderText}</p>}
</div>
</div>
)}
Expand Down
Loading

0 comments on commit 5f4141f

Please sign in to comment.