Skip to content

Commit

Permalink
feat(dx): add Storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
irudoy committed Apr 21, 2020
1 parent 52432b9 commit 48590b7
Show file tree
Hide file tree
Showing 8 changed files with 7,229 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
insert_final_newline = true
47 changes: 45 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,48 @@
"react/jsx-filename-extension": "off",
"react/forbid-prop-types": "off",
"jsx-a11y/control-has-associated-label": "off"
}
}
},
"overrides": [
{
"files": ["{.storybook,stories}/**/*"],
"rules": {
"import/no-extraneous-dependencies": "off",
"react/prop-types": "off",
"jsx-a11y/href-no-hash": "off",
"jsx-a11y/accessible-emoji": "off",
"jsx-a11y/alt-text": "off",
"jsx-a11y/anchor-has-content": "off",
"jsx-a11y/anchor-is-valid": "off",
"jsx-a11y/aria-activedescendant-has-tabindex": "off",
"jsx-a11y/aria-props": "off",
"jsx-a11y/aria-proptypes": "off",
"jsx-a11y/aria-role": "off",
"jsx-a11y/aria-unsupported-elements": "off",
"jsx-a11y/click-events-have-key-events": "off",
"jsx-a11y/heading-has-content": "off",
"jsx-a11y/html-has-lang": "off",
"jsx-a11y/iframe-has-title": "off",
"jsx-a11y/img-redundant-alt": "off",
"jsx-a11y/interactive-supports-focus": "off",
"jsx-a11y/label-has-for": "off",
"jsx-a11y/label-has-associated-control": "off",
"jsx-a11y/media-has-caption": "off",
"jsx-a11y/mouse-events-have-key-events": "off",
"jsx-a11y/no-access-key": "off",
"jsx-a11y/no-autofocus": "off",
"jsx-a11y/no-distracting-elements": "off",
"jsx-a11y/no-interactive-element-to-noninteractive-role": "off",
"jsx-a11y/no-noninteractive-element-interactions": "off",
"jsx-a11y/no-noninteractive-element-to-interactive-role": "off",
"jsx-a11y/no-noninteractive-tabindex": "off",
"jsx-a11y/no-onchange": "off",
"jsx-a11y/no-redundant-roles": "off",
"jsx-a11y/no-static-element-interactions": "off",
"jsx-a11y/role-has-required-aria-props": "off",
"jsx-a11y/role-supports-aria-props": "off",
"jsx-a11y/scope": "off",
"jsx-a11y/tabindex-no-positive": "off"
}
}
]
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
storybook-static
23 changes: 23 additions & 0 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const pathlib = require('path')

const resolveStories = (path) => pathlib.resolve(__dirname, '../stories', path)

module.exports = {
stories: [
resolveStories('**/*.stories.ts'),
resolveStories('**/*.stories.tsx'),
],
addons: ['@storybook/addon-knobs/register', '@storybook/addon-storysource'],
webpackFinal: async (config) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('ts-loader'),
},
],
})
config.resolve.extensions.push('.ts', '.tsx')
return config
},
}
22 changes: 19 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@
"start": "yarn build --watch",
"prepublish": "yarn build",
"lint": "eslint",
"prettify": "prettier"
"prettify": "prettier",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@storybook/addon-knobs": "6.0.0-alpha.28",
"@storybook/addon-storysource": "6.0.0-alpha.28",
"@storybook/addons": "6.0.0-alpha.28",
"@storybook/react": "6.0.0-alpha.28",
"@storybook/source-loader": "6.0.0-alpha.28",
"@types/photoswipe": "^4.1.0",
"@types/react": "^16.9.34",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"babel-loader": "^8.1.0",
"eslint": "6.8.0",
"eslint-config-airbnb": "18.1.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.11.0",
"eslint-plugin-react-hooks": "^2.5.0",
"husky": "^4.2.5",
"lint-staged": "^10.1.6",
"photoswipe": "^4.1.3",
"prettier": "^2.0.4",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"rimraf": "^3.0.2",
"typescript": "^3.8.3"
"ts-loader": "^7.0.1",
"typescript": "^3.8.3",
"webpack": "^4.43.0"
},
"peerDependencies": {
"photoswipe": ">= 4.1.0",
Expand All @@ -49,5 +64,6 @@
"hooks": {
"pre-commit": "yarn build && lint-staged"
}
}
},
"dependencies": {}
}
92 changes: 92 additions & 0 deletions stories/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React, { useState, useCallback, FC } from 'react'
import { withKnobs, button } from '@storybook/addon-knobs'
import { Gallery, Item, PhotoswipeLayoutDefault } from '../src'
import 'photoswipe/dist/photoswipe.css'
import 'photoswipe/dist/default-skin/default-skin.css'

export default { title: 'Gallery', decorators: [withKnobs] }

const N = 3

function shuffle(array: any[]) {
let currentIndex = array.length
let temp: any
let randomIndex: number
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
temp = array[currentIndex]
// eslint-disable-next-line no-param-reassign
array[currentIndex] = array[randomIndex]
// eslint-disable-next-line no-param-reassign
array[randomIndex] = temp
}

return array
}

interface ImageItem {
large: string
thumb: string
w: number
h: number
title: string
}

const createItem = (index: number): ImageItem => ({
large: `https://placekitten.com/1024/768?image=${index}`,
thumb: `https://placekitten.com/160/120?image=${index}`,
w: 1024,
h: 768,
title: `kitty #${index}`,
})

const items = Array.from({ length: N }, (_, i) => createItem(i + 1))

const ImageItem: FC<ImageItem> = ({ large, thumb, w, h, title }) => {
const [fullTitle, setFullTitle] = useState(title)
return (
<Item width={w} height={h} full={large} thumb={thumb} title={fullTitle}>
{({ open, thumbRef }) => (
<div style={{ display: 'inline-block', margin: 5 }}>
<img
onClick={open}
src={thumb}
ref={thumbRef}
style={{ display: 'block', cursor: 'pointer', marginBottom: 5 }}
/>
<input
type="text"
value={fullTitle}
onChange={(e) => setFullTitle(e.target.value)}
style={{ width: '100%', boxSizing: 'border-box' }}
/>
</div>
)}
</Item>
)
}

export const playground = () => {
const [photos, setPhotos] = useState(items)
button('add', () => setPhotos([createItem(photos.length + 1), ...photos]))
button('remove', () => setPhotos(photos.slice(1)))
button('swap first two', () =>
setPhotos([photos[1], photos[0], ...photos.slice(2)]),
)
button('swap last two', () =>
setPhotos([
...photos.slice(0, photos.length - 2),
photos[photos.length - 1],
photos[photos.length - 2],
]),
)
button('shuffle', () => setPhotos([...shuffle(photos)]))
return (
<Gallery layout={PhotoswipeLayoutDefault}>
{photos.map((props) => (
<ImageItem {...props} key={props.thumb} />
))}
</Gallery>
)
}
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
"jsx": "react",
"declaration": true,
"strict": true,
"skipLibCheck": true,
"sourceMap": true,
"allowJs": false,
"target": "ES5",
"lib": ["DOM", "ES5", "ES2015.Collection", "ES2015.Iterable"]
"lib": ["DOM", "ES5", "ES2015.Collection", "ES2015.Iterable"],
"rootDirs": ["src", "stories"]
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
Loading

0 comments on commit 48590b7

Please sign in to comment.