Skip to content

Commit

Permalink
✨ 🖍[amp story shopping] Shopping tag CSS (#37026)
Browse files Browse the repository at this point in the history
* Shopping tag.

* Delete code.

* Update templates

* Remove unrelated data.

* Update comment

* compress image

* CSS

* lint

* lint

* lint

* lint

* default style

* dep check

* Variable naming
  • Loading branch information
processprocess committed Dec 1, 2021
1 parent fa431d8 commit 833f52e
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 25 deletions.
1 change: 1 addition & 0 deletions build-system/test-configs/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ exports.rules = [
// Story Shopping
'extensions/amp-story-shopping/0.1/amp-story-shopping-config.js->extensions/amp-story/1.0/amp-story-store-service.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js->extensions/amp-story/1.0/amp-story-store-service.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js->extensions/amp-story/1.0/utils.js',

// Interactive components that depend on story functionality.
'extensions/amp-story-interactive/0.1/amp-story-interactive-abstract.js->extensions/amp-story/1.0/amp-story-store-service.js',
Expand Down
50 changes: 44 additions & 6 deletions examples/amp-story/amp-story-shopping.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-story" src="https://cdn.ampproject.org/v0/amp-story-1.0.js"></script>
<script async custom-element="amp-story-shopping" src="https://cdn.ampproject.org/v0/amp-story-shopping-0.1.js"></script>
<style amp-custom>
[data-tag-id="hat"] {
top: 33%;
left: 7%;
}
[data-tag-id="sunglasses"] {
top: 28%;
left: 43%;
}
[data-tag-id="backpack"] {
top: 33%;
left: 80%;
}
[data-tag-id=city-pop] {
top: 33%;
left: 7%;
}
[data-tag-id=k-pop] {
top: 28%;
left: 43%;
}
[data-tag-id=eurodance] {
top: 33%;
left: 80%;
}
</style>
</head>
<body>
<amp-story
Expand All @@ -26,20 +52,26 @@
"items" : [
{
"product-tag-id": "hat",
"product-title": "Hootenanny Hat"
"product-title": "Hootenanny Hat",
"product-price": "799"
},
{
"product-tag-id": "sunglasses",
"product-title": "Spectacular Spectacles"
"product-title": "Spectacular Spectacles",
"product-price": "400"
},
{
"product-tag-id": "backpack",
"product-title": "Beastly Backpack"
"product-title": "Beastly Backpack",
"product-price": "1000"
}
]
}
</script>
</amp-story-shopping-config>
<amp-story-grid-layer template="fill">
<amp-img layout="fill" src="/examples/visual-tests/amp-story/img/shopping/bg.png"></amp-img>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag data-tag-id="hat" ></amp-story-shopping-tag>
<amp-story-shopping-tag data-tag-id="sunglasses" ></amp-story-shopping-tag>
Expand All @@ -58,20 +90,26 @@
"items" : [
{
"product-tag-id": "city-pop",
"product-title": "Plastic Love"
"product-title": "Plastic Love",
"product-price": "19"
},
{
"product-tag-id": "k-pop",
"product-title": "Gangnam Style"
"product-title": "Gangnam Style",
"product-price": "10"
},
{
"product-tag-id": "eurodance",
"product-title": "Crystal King Battle"
"product-title": "Crystal King Battle",
"product-price": "300"
}
]
}
</script>
</amp-story-shopping-config>
<amp-story-grid-layer template="fill">
<amp-img layout="fill" src="/examples/visual-tests/amp-story/img/shopping/bg.png"></amp-img>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag data-tag-id="city-pop" ></amp-story-shopping-tag>
<amp-story-shopping-tag data-tag-id="k-pop" ></amp-story-shopping-tag>
Expand Down
9 changes: 6 additions & 3 deletions examples/amp-story/shopping/remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
"items": [
{
"product-tag-id": "city-pop",
"product-title": "Plastic Love"
"product-title": "Plastic Love",
"product-price": "19"
},
{
"product-tag-id": "k-pop",
"product-title": "Gangnam Style"
"product-title": "Gangnam Style",
"product-price": "10"
},
{
"product-tag-id": "eurodance",
"product-title": "Crystal King Battle"
"product-title": "Crystal King Battle",
"product-price": "300"
}
]
}
40 changes: 39 additions & 1 deletion examples/visual-tests/amp-story/amp-story-shopping.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,37 @@
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-story" src="https://cdn.ampproject.org/v0/amp-story-1.0.js"></script>
<script async custom-element="amp-story-shopping" src="https://cdn.ampproject.org/v0/amp-story-shopping-0.1.js"></script>
<style amp-custom>
[data-tag-id="hat"] {
top: 33%;
left: 7%;
}
[data-tag-id="sunglasses"] {
top: 28%;
left: 43%;
}
[data-tag-id="backpack"] {
top: 33%;
left: 80%;
}
[data-tag-id=city-pop] {
top: 33%;
left: 7%;
}
[data-tag-id=k-pop] {
top: 28%;
left: 43%;
}
[data-tag-id=eurodance] {
top: 33%;
left: 80%;
}
</style>
</head>
<body>
<amp-story
standalone
title="New Story Shopping Component Visual Diff Test"
title="Story Shopping Component Visual Diff Test"
publisher="AMP Story Shopping"
publisher-logo-src="example.com/logo.png"
poster-portrait-src="example.com/poster.jpg">
Expand All @@ -27,19 +53,25 @@
{
"product-tag-id": "hat",
"product-title": "Hootenanny Hat",
"product-price": "799"
},
{
"product-tag-id": "sunglasses",
"product-title": "Spectacular Spectacles",
"product-price": "400"
},
{
"product-tag-id": "backpack",
"product-title": "Beastly Backpack",
"product-price": "1000"
}
]
}
</script>
</amp-story-shopping-config>
<amp-story-grid-layer template="fill">
<amp-img layout="fill" src="/examples/visual-tests/amp-story/img/shopping/bg.png"></amp-img>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag data-tag-id="hat" ></amp-story-shopping-tag>
<amp-story-shopping-tag data-tag-id="sunglasses" ></amp-story-shopping-tag>
Expand All @@ -59,19 +91,25 @@
{
"product-tag-id": "city-pop",
"product-title": "Plastic Love",
"product-price": "799"
},
{
"product-tag-id": "k-pop",
"product-title": "Gangnam Style",
"product-price": "400"
},
{
"product-tag-id": "eurodance",
"product-title": "Crystal King Battle",
"product-price": "1000"
}
]
}
</script>
</amp-story-shopping-config>
<amp-story-grid-layer template="fill">
<amp-img layout="fill" src="/examples/visual-tests/amp-story/img/shopping/bg.png"></amp-img>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag data-tag-id="city-pop" ></amp-story-shopping-tag>
<amp-story-shopping-tag data-tag-id="k-pop" ></amp-story-shopping-tag>
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 76 additions & 5 deletions extensions/amp-story-shopping/0.1/amp-story-shopping-tag.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
amp-story-shopping-tag {
/**
* This is placeholder for the shopping tag until syling is implemented.
* Having a min-height enables the layoutCallback to be called.
*/
min-height: 20px;
font-family: 'Poppins', sans-serif !important;
font-weight: 700 !important;
cursor: pointer !important;
width: auto !important;
color: white !important;
position: absolute !important;
/* Height and width ensures layoutCallback is called. */
min-height: 1px !important;
min-width: 1px !important;

--i-amphtml-shopping-tag-bg-color: rgba(0, 0, 0, 0.3);
--i-amphtml-shopping-tag-backdrop-filter: blur(20px);
}

.amp-story-shopping-tag-inner {
height: 36px !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
}

.amp-story-shopping-tag-dot {
width: 16px !important;
height: 16px !important;
border-radius: 100% !important;
background-color: var(--i-amphtml-shopping-tag-bg-color) !important;
position: relative !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
margin-inline-end: 4px !important;
backdrop-filter: var(--i-amphtml-shopping-tag-backdrop-filter) !important;
}

.amp-story-shopping-tag-dot:before {
border-radius: 100% !important;
content: '' !important;
width: 8px !important;
height: 8px !important;
background: white !important;
box-shadow: 0px 2px 8px var(--i-amphtml-shopping-tag-bg-color) !important;
}

.amp-story-shopping-tag-pill {
display: flex !important;
align-items: center !important;
border-radius: 18px !important;
height: 100% !important;
background-color: var(--i-amphtml-shopping-tag-bg-color) !important;
padding: 0 6px !important;
backdrop-filter: var(--i-amphtml-shopping-tag-backdrop-filter) !important;
}

.amp-story-shopping-tag-pill-image {
width: 24px !important;
height: 24px !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
background-color: rgba(0, 0, 0, 0.6) !important;
border-radius: 100% !important;
background-image: url('data:image/svg+xml;charset=utf-8,<svg width="12" height="12" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M11.49 5.755 6.24.505a1.16 1.16 0 0 0-.823-.338H1.334A1.17 1.17 0 0 0 .167 1.333v4.084c0 .32.128.612.344.828l5.25 5.25c.21.21.502.338.823.338.32 0 .612-.128.822-.344l4.083-4.083a1.14 1.14 0 0 0 .345-.823c0-.32-.134-.618-.345-.828ZM2.208 3.083a.874.874 0 1 1 0-1.75.874.874 0 1 1 0 1.75Z" fill="#fff"/></svg>') !important;
background-repeat: no-repeat !important;
background-position: center !important;
}

.amp-story-shopping-tag-pill-text {
font-size: 14px !important;
padding: 0 6px 0 4px !important;
}

.amp-story-shopping-tag-pill-text::before {
content: '$' !important;
font-weight: 400 !important;
padding-inline-end: 3px !important;
font-size: 10px !important;
}
59 changes: 50 additions & 9 deletions extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
import * as Preact from '#core/dom/jsx';
import {Layout_Enum} from '#core/dom/layout';

import {Services} from '#service';

import {devAssert} from '#utils/log';

import {CSS as shoppingTagCSS} from '../../../build/amp-story-shopping-tag-0.1.css';
import {
ShoppingConfigDataDef,
StateProperty,
} from '../../amp-story/1.0/amp-story-store-service';
import {createShadowRootWithStyle} from '../../amp-story/1.0/utils';

/** @const {!Array<!Object>} fontFaces with urls from https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&amp;display=swap */
const FONTS_TO_LOAD = [
{
family: 'Poppins',
weight: '400',
src: "url(https://fonts.gstatic.com/s/poppins/v9/pxiEyp8kv8JHgFVrJJfecnFHGPc.woff2) format('woff2')",
},
{
family: 'Poppins',
weight: '700',
src: "url(https://fonts.gstatic.com/s/poppins/v9/pxiByp8kv8JHgFVrLCz7Z1xlFd2JQEk.woff2) format('woff2')",
},
];

const renderShoppingTagTemplate = (tagData) => (
<div class="amp-story-shopping-tag-inner">
<span class="amp-story-shopping-tag-dot"></span>
<span class="amp-story-shopping-tag-pill">
<span class="amp-story-shopping-tag-pill-image"></span>
<span class="amp-story-shopping-tag-pill-text">
{tagData['product-price']}
</span>
</span>
</div>
);

/**
* @typedef {{
Expand All @@ -26,19 +53,18 @@ export class AmpStoryShoppingTag extends AMP.BaseElement {

/** @override */
buildCallback() {
this.loadFonts_();
this.element.setAttribute('role', 'button');
return Services.storyStoreServiceForOrNull(this.win).then(
(storeService) => {
devAssert(storeService, 'Could not retrieve AmpStoryStoreService');
this.storeService_ = storeService;
}
(storeService) => (this.storeService_ = storeService)
);
}

/** @override */
layoutCallback() {
this.storeService_.subscribe(
StateProperty.SHOPPING_DATA,
(shoppingData) => this.updateShoppingTag_(shoppingData),
(shoppingData) => this.createAndAppendInnerShoppingTagEl_(shoppingData),
true /** callToInitialize */
);
}
Expand All @@ -52,13 +78,28 @@ export class AmpStoryShoppingTag extends AMP.BaseElement {
* @param {!ShoppingDataDef} shoppingData
* @private
*/
updateShoppingTag_(shoppingData) {
createAndAppendInnerShoppingTagEl_(shoppingData) {
const tagData = shoppingData[this.element.getAttribute('data-tag-id')];
if (!tagData) {
return;
}
this.mutateElement(() => {
this.element.textContent = tagData['product-title'];
createShadowRootWithStyle(
this.element,
renderShoppingTagTemplate(tagData),
shoppingTagCSS
);
});
}

/** @private */
loadFonts_() {
if (this.win.document.fonts && FontFace) {
FONTS_TO_LOAD.forEach(({family, src, style = 'normal', weight}) =>
new FontFace(family, src, {weight, style})
.load()
.then((font) => this.win.document.fonts.add(font))
);
}
}
}

0 comments on commit 833f52e

Please sign in to comment.