Skip to content

Commit

Permalink
✨ CSS support in Storybook and amp-image-slider example (ampproject…
Browse files Browse the repository at this point in the history
…#27800)

* Added css support to storybook + amp-image-slider example

* Remove unncessary source maps

* Fixed linter issues
  • Loading branch information
wassgha authored and samouri committed Apr 21, 2020
1 parent 367ef6f commit 81512c8
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
15 changes: 15 additions & 0 deletions build-system/tasks/storybook/amp-env/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@

import * as Preact from '../../../../src/preact';
import {makeDecorator} from '@storybook/addons';
import flush from 'styled-jsx/server';
import render from 'preact-render-to-string/jsx';

export default makeDecorator({
name: 'withAmpDecorator',
parameterName: 'amp',
wrapper: (getStory, context) => {
const contents = render(getStory(context));
const styleElements = flush();
const styles = render(
<style
amp-custom=""
dangerouslySetInnerHTML={{
__html: styleElements
.map((style) => style.props.dangerouslySetInnerHTML.__html)
.join('')
.replace(/\/\*# sourceMappingURL=.*\*\//g, '')
.replace(/\/\*@ sourceURL=.*?\*\//g, ''),
}}
/>
);

const ampHtml = `
<!doctype html>
Expand All @@ -41,6 +55,7 @@ export default makeDecorator({
ext.version || 0.1
}.js"></script>`
)}
${styles}
</head>
<body>
${contents}
Expand Down
3 changes: 2 additions & 1 deletion build-system/tasks/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"babel-loader": "8.0.6",
"core-js": "3.6.4",
"preact": "10.3.4",
"preact-render-to-string": "5.1.4"
"preact-render-to-string": "5.1.4",
"styled-jsx": "3.2.5"
}
}
47 changes: 45 additions & 2 deletions build-system/tasks/storybook/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,15 @@
globals "^11.1.0"
lodash "^4.17.13"

"@babel/types@7.8.3":
version "7.8.3"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.8.3.tgz#5a383dffa5416db1b73dedffd311ffd0788fb31c"
integrity sha512-jBD+G8+LWpMBBWvVcdr4QysjUE4mU/syrhN17o1u3gx0/WzJB1kwiVZAXRtWbsIPOwW8pF/YJV5+nmetPzepXg==
dependencies:
esutils "^2.0.2"
lodash "^4.17.13"
to-fast-properties "^2.0.0"

"@babel/types@^7.4.4", "@babel/types@^7.8.3", "@babel/types@^7.8.6", "@babel/types@^7.9.0":
version "7.9.0"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.9.0.tgz#00b064c3df83ad32b2dbf5ff07312b15c7f1efb5"
Expand Down Expand Up @@ -2114,7 +2123,7 @@ babel-plugin-minify-type-constructors@^0.4.3:
dependencies:
babel-helper-is-void-0 "^0.4.3"

babel-plugin-syntax-jsx@^6.18.0:
babel-plugin-syntax-jsx@6.18.0, babel-plugin-syntax-jsx@^6.18.0:
version "6.18.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
integrity sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY=
Expand Down Expand Up @@ -2823,7 +2832,7 @@ content-type@~1.0.4:
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b"
integrity sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==

convert-source-map@^1.5.0, convert-source-map@^1.7.0:
convert-source-map@1.7.0, convert-source-map@^1.5.0, convert-source-map@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.7.0.tgz#17a2cb882d7f77d3490585e2ce6c524424a3a442"
integrity sha512-4FJkXzKXEDB1snCFZlLP4gpC3JILicCpGbzG9f9G7tGqGCzETQ2hWPrcinA9oU4wtf2biUaEH5065UnMeR33oA==
Expand Down Expand Up @@ -6896,6 +6905,11 @@ source-map-url@^0.4.0:
resolved "https://registry.yarnpkg.com/source-map-url/-/source-map-url-0.4.0.tgz#3e935d7ddd73631b97659956d55128e87b5084a3"
integrity sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=

source-map@0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

source-map@^0.5.0, source-map@^0.5.6, source-map@^0.5.7:
version "0.5.7"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
Expand Down Expand Up @@ -6993,6 +7007,11 @@ stream-shift@^1.0.0:
resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.1.tgz#d7088281559ab2778424279b0877da3c392d5a3d"
integrity sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==

string-hash@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/string-hash/-/string-hash-1.1.3.tgz#e8aafc0ac1855b4666929ed7dd1275df5d6c811b"
integrity sha1-6Kr8CsGFW0Zmkp7X3RJ1311sgRs=

string-width@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"
Expand Down Expand Up @@ -7140,6 +7159,30 @@ style-loader@^1.0.0:
loader-utils "^1.2.3"
schema-utils "^2.6.4"

styled-jsx@3.2.5:
version "3.2.5"
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.2.5.tgz#0172a3e13a0d6d8bf09167dcaf32cf7102d932ca"
integrity sha512-prEahkYwQHomUljJzXzrFnBmQrSMtWOBbXn8QeEkpfFkqMZQGshxzzp4H8ebBIsbVlHF/3+GSXMnmK/fp7qVYQ==
dependencies:
"@babel/types" "7.8.3"
babel-plugin-syntax-jsx "6.18.0"
convert-source-map "1.7.0"
loader-utils "1.2.3"
source-map "0.7.3"
string-hash "1.1.3"
stylis "3.5.4"
stylis-rule-sheet "0.0.10"

stylis-rule-sheet@0.0.10:
version "0.0.10"
resolved "https://registry.yarnpkg.com/stylis-rule-sheet/-/stylis-rule-sheet-0.0.10.tgz#44e64a2b076643f4b52e5ff71efc04d8c3c4a430"
integrity sha512-nTbZoaqoBnmK+ptANthb10ZRZOGC+EmTLLUxeYIuHNkEKcmKgXX1XWKkUBT2Ac4es3NybooPe0SmvKdhKJZAuw==

stylis@3.5.4:
version "3.5.4"
resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.5.4.tgz#f665f25f5e299cf3d64654ab949a57c768b73fbe"
integrity sha512-8/3pSmthWM7lsPBKv7NXkzn2Uc9W7NotcwGNpJaa3k7WMM1XDCA4MgT5k/8BIexd5ydZdboXtU90XH9Ec4Bv/Q==

supports-color@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
Expand Down
84 changes: 84 additions & 0 deletions extensions/amp-image-slider/0.1/storybook/Basic.amp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Preact from '../../../../src/preact';
import {storiesOf} from '@storybook/preact';
import {text, withKnobs} from '@storybook/addon-knobs';
import {withA11y} from '@storybook/addon-a11y';
import withAmp from '../../../../build-system/tasks/storybook/amp-env/decorator.js';

// eslint-disable-next-line
storiesOf('Image Slider', module)
.addDecorator(withKnobs)
.addDecorator(withA11y)
.addDecorator(withAmp)
.addParameters({extensions: [{name: 'amp-image-slider', version: 0.1}]})
.add('default', () => {
const first = text(
'First image',
'https://amp.dev/static/samples/img/canoe_900x600.jpg'
);
const second = text(
'Second image',
'https://amp.dev/static/samples/img/canoe_900x600_blur.jpg'
);

return (
<amp-image-slider width="600" height="300" layout="fixed">
<amp-img src={first} alt={'First image'} layout="fill"></amp-img>
<amp-img src={second} alt={'Second iamge'} layout="fill"></amp-img>
</amp-image-slider>
);
})
.add('custom-hints', () => {
const first = text(
'First image',
'https://amp.dev/static/samples/img/canoe_900x600.jpg'
);
const second = text(
'Second image',
'https://amp.dev/static/samples/img/canoe_900x600_blur.jpg'
);

return (
<amp-image-slider width="600" height="300" layout="fixed">
<amp-img src={first} alt={'First image'} layout="fill"></amp-img>
<amp-img src={second} alt={'Second image'} layout="fill"></amp-img>
<style jsx global>
{`
.amp-image-slider-hint-right {
width: 10px;
height: 20px;
background-size: 10px 20px;
margin-left: 10px;
background-image: url("data:image/svg+xml;charset=utf-8,%3csvg xmlns='http://www.w3.org/2000/svg' width='10' height='20' viewBox='0 0 10 20'%3e%3cpolygon points='0,0 10,10 0,20' style='fill:white;stroke:black;stroke-width:1' /%3e%3c/svg%3e");
}
`}
</style>
<style jsx global>
{`
.amp-image-slider-hint-left {
width: 10px;
height: 20px;
background-size: 10px 20px;
margin-right: 10px;
background-image: url("data:image/svg+xml;charset=utf-8,%3csvg xmlns='http://www.w3.org/2000/svg' width='10' height='20' viewBox='0 0 10 20'%3e%3cpolygon points='10,0 0,10 10,20' style='fill:white;stroke:black;stroke-width:1' /%3e%3c/svg%3e");
}
`}
</style>
</amp-image-slider>
);
});

0 comments on commit 81512c8

Please sign in to comment.