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

Migrate from simplebar to overlaybars #9375

Merged
merged 24 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a25dcf5
CHANGE to use overlayscrollbars over simplebars
ndelangen Jan 9, 2020
ef8f22d
FIX tabs bordered + absolute
ndelangen Jan 9, 2020
cc5988e
FIX snapshots
ndelangen Jan 9, 2020
d723a6c
CLEANUP
ndelangen Jan 9, 2020
32520f8
CHANGE to css-in-js for overflowscrollbar styles
ndelangen Jan 10, 2020
afcaefc
REMOVE resize
ndelangen Jan 10, 2020
8e68fac
MOVE style file & FIX script
ndelangen Jan 11, 2020
138208a
FIX snapshots
ndelangen Jan 11, 2020
5c33a88
Merge branch 'next-6.0.0' into migrate-from-simplebar-to-overlaybars-2
ndelangen Jan 11, 2020
5929556
ADD jest-serializer-html
ndelangen Jan 11, 2020
09d95ae
decrease size of snapshots by snapping the html instead && FIX snapshots
ndelangen Jan 11, 2020
59fbfe3
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Jan 20, 2020
567c898
CLEANUP
ndelangen Jan 20, 2020
fd7c2f9
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Jan 27, 2020
e5bc525
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Jan 28, 2020
8ee2eca
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Jan 29, 2020
bc16989
FIX lockfile
ndelangen Jan 29, 2020
908c58e
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Feb 7, 2020
e3ec34b
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Feb 7, 2020
7a5521b
FIX
ndelangen Feb 7, 2020
21711ad
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Mar 10, 2020
7b24721
OPTIMIZE scrollareastyles
ndelangen Mar 10, 2020
c1e1043
FIX handle not draggable in sidebar
ndelangen Mar 10, 2020
6ec796b
Merge branch 'next' into migrate-from-simplebar-to-overlaybars-2
ndelangen Mar 10, 2020
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
71 changes: 68 additions & 3 deletions app/riot/src/client/preview/__snapshots__/render-riot.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`render a riot element can accept a constructor 1`] = `"<simpletest hacked=\\"true\\" test=\\"with a parameter\\"><div>HACKED : true ; simple test (with a parameter). Oh, by the way (value is mapped to riotValue)</div></simpletest>"`;
exports[`render a riot element can accept a constructor 1`] = `
<simpletest hacked="true"
test="with a parameter"
>
<div>
HACKED : true ; simple test (with a parameter). Oh, by the way (value is mapped to riotValue)
</div>
</simpletest>
`;

exports[`render a riot element can nest several tags 1`] = `"<matriochka><div><tag1><div>Inside tag1:<ul><li><tag2><div>Inside tag2:<ul><li><tag3><div>Inside tag3:<ul><li><tag4><div>Inside tag4:<ul><li><tag5><div>Inside tag5:<ul><li>Content</li></ul></div></tag5></li></ul></div></tag4></li></ul></div></tag3></li></ul></div></tag2></li></ul></div></tag1></div></matriochka>"`;
exports[`render a riot element can nest several tags 1`] = `
<matriochka>
<div>
<tag1>
<div>
Inside tag1:
<ul>
<li>
<tag2>
<div>
Inside tag2:
<ul>
<li>
<tag3>
<div>
Inside tag3:
<ul>
<li>
<tag4>
<div>
Inside tag4:
<ul>
<li>
<tag5>
<div>
Inside tag5:
<ul>
<li>
Content
</li>
</ul>
</div>
</tag5>
</li>
</ul>
</div>
</tag4>
</li>
</ul>
</div>
</tag3>
</li>
</ul>
</div>
</tag2>
</li>
</ul>
</div>
</tag1>
</div>
</matriochka>
`;

exports[`render a riot element can template some vars 1`] = `"<simpletest test=\\"with a parameter\\"><div>simple test (with a parameter). Oh, by the way (value is mapped to riotValue)</div></simpletest>"`;
exports[`render a riot element can template some vars 1`] = `
<simpletest test="with a parameter">
<div>
simple test (with a parameter). Oh, by the way (value is mapped to riotValue)
</div>
</simpletest>
`;
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = {
DOCS_MODE: false,
PREVIEW_URL: undefined,
},
snapshotSerializers: ['jest-emotion', 'enzyme-to-json/serializer'],
snapshotSerializers: ['jest-emotion', 'enzyme-to-json/serializer', 'jest-serializer-html'],
coverageDirectory: 'coverage',
setupFilesAfterEnv: ['./scripts/jest.init.js'],
coverageReporters: ['lcov'],
Expand Down
5 changes: 4 additions & 1 deletion lib/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
"dependencies": {
"@storybook/client-logger": "6.0.0-alpha.22",
"@storybook/theming": "6.0.0-alpha.22",
"@types/overlayscrollbars": "^1.9.0",
"@types/react-syntax-highlighter": "11.0.4",
"@types/react-textarea-autosize": "^4.3.3",
"core-js": "^3.0.1",
"global": "^4.3.2",
"lodash": "^4.17.15",
"markdown-to-jsx": "^6.9.1",
"memoizerific": "^1.11.3",
"overlayscrollbars": "^1.10.2",
"overlayscrollbars-react": "^0.2.1",
"polished": "^3.4.4",
"popper.js": "^1.14.7",
"react": "^16.8.3",
Expand All @@ -46,10 +49,10 @@
"react-popper-tooltip": "^2.8.3",
"react-syntax-highlighter": "^11.0.2",
"react-textarea-autosize": "^7.1.0",
"simplebar-react": "^1.0.0-alpha.6",
"ts-dedent": "^1.1.1"
},
"devDependencies": {
"css": "^2.2.4",
"enzyme": "^3.9.0",
"jest": "^25.1.0",
"jest-enzyme": "^7.0.2"
Expand Down
132 changes: 132 additions & 0 deletions lib/components/scripts/writeCssScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-param-reassign */

// This little script converts the overflowscrollbars CSS file into the css-in-js file
// it's normal you have to run prettier over the file after

const fs = require('fs');
const { parse } = require('css');
const { isNaN } = require('global');

const INPUT = require.resolve('overlayscrollbars/css/OverlayScrollbars.min.css');
const OUTPUT = `${__dirname}/../src/ScrollArea/ScrollAreaStyles.ts`;
const OPTIONS = { camelCase: true, numbers: true };

const read = file => {
return fs
.readFileSync(file)
.toString()
.replace(/(?:\r\n|\r|\n)/g, '');
};

const convert = (css, opts) => {
const ast = parse(css, { source: css });
const obj = cssToObject(opts)(ast.stylesheet.rules);
return obj;
};

const cssToObject = opts => (rules, result = {}) => {
rules.forEach(rule => {
if (rule.type === 'media') {
const key = `@media ${rule.media}`;
const decs = cssToObject(opts)(rule.rules);
result[key] = decs;
return;
}
if (rule.type === 'keyframes') {
result.__keyframes = Object.assign(result.__keyframes || {}, { [camel(rule.name)]: rule });
return;
}
if (rule.type === 'comment') {
return;
}

const key = rule.selectors.filter(s => !s.includes('.os-theme-none')).join(', ');

if (key.length) {
Object.assign(result, {
[key]: Object.assign(result[key] || {}, getDeclarations(rule.declarations, opts)),
});
}
});
return result;
};

const getDeclarations = (decs, opts = {}) => {
const result = decs
.filter(d => {
const filtered = d.type === 'comment' || d.property.match(/^(?:-webkit-|-ms-|-moz-)/);
return !filtered;
})
.map(d => ({
key: opts.camelCase ? camel(d.property) : d.property,
value: opts.numbers ? parsePx(d.value) : d.value,
}))
.reduce((a, b) => {
a[b.key] = b.value;
return a;
}, {});
return result;
};

const camel = str => str.replace(/(-[a-z])/g, x => x.toUpperCase()).replace(/-/g, '');

const parsePx = val => {
return /px$/.test(val) || val === '' || (val.match(/\d$/) && !isNaN(parseInt(val, 10)))
? parseFloat(val.replace(/px$/, ''))
: val;
};

const { __keyframes, ...styles } = convert(read(INPUT), OPTIONS);

const stringifiedKeyFrames = Object.values(__keyframes)
.map(k => {
return `const ${camel(k.name)} = keyframes\`${k.keyframes.reduce(
(acc, item) =>
`${acc}${k.position.source.substring(
item.position.start.column - 1,
item.position.end.column - 1
)}`,
''
)}\`;`;
})
.join('\n');

const stringifiedStyles = JSON.stringify(
Object.entries(styles).reduce((acc, [key, item]) => {
if (item.animationName && __keyframes[camel(item.animationName)]) {
item.animationName = camel(item.animationName);
}

if (item.backgroundImage && item.backgroundImage.match(/^url/)) {
item.backgroundImage =
'linear-gradient(135deg, rgba(0,0,0,0) 0%, rgba(0,0,0,0) 50%, rgba(0,0,0,0.4) 50%, rgba(0,0,0,0.4) 100%)';
}

acc[key] = item;
return acc;
}, {}),
null,
2
);

const stringifiedStylesWithReplacedKeyframes = Object.keys(__keyframes)
.reduce((acc, item) => {
// replace keyframes
return acc.replace(`"${item}"`, `\`\${${item}}\``);
}, stringifiedStyles)
.replace(/"([^\s]+)!important"/g, (f, p1) => {
// make "!important" rules work with TS
const v = parsePx(p1);
return `"${p1}!important" as any as ${JSON.stringify(v)}`;
});

const result = `
import { Theme, CSSObject, keyframes } from '@storybook/theming';

${stringifiedKeyFrames}

export const getScrollAreaStyles: (theme: Theme) => CSSObject = (theme: Theme) => (${stringifiedStylesWithReplacedKeyframes});
`;

fs.writeFileSync(OUTPUT, result);
7 changes: 2 additions & 5 deletions lib/components/src/ScrollArea/ScrollArea.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Storybook's implementation of SimpleBar https://github.com/Grsmto/simplebar
// Note: "SimpleBar can't be used on the <body>, <textarea> or <iframe> elements."

import React, { Fragment, FunctionComponent } from 'react';
import { styled, Global } from '@storybook/theming';

import SimpleBar from 'simplebar-react';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-react';
import { getScrollAreaStyles } from './ScrollAreaStyles';

export interface ScrollProps {
Expand All @@ -14,7 +11,7 @@ export interface ScrollProps {
}

const Scroll = styled(({ vertical, horizontal, ...rest }: ScrollProps) => (
<SimpleBar {...rest} scrollableNodeProps={{ tabIndex: 0 }} />
<OverlayScrollbarsComponent options={{ scrollbars: { autoHide: 'scroll' } }} {...rest} />
))<ScrollProps>(
({ vertical }) =>
!vertical
Expand Down
Loading