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

Always set theme color to background color ✅ #19391

Merged
merged 8 commits into from Nov 20, 2018
25 changes: 25 additions & 0 deletions extensions/amp-story/1.0/amp-story.js
Expand Up @@ -180,6 +180,11 @@ const TAG = 'amp-story';
const HIDE_ON_BOOKEND_SELECTOR =
'amp-story-page, .i-amphtml-story-system-layer';

/**
* The default light gray for chrome supported theme color.
* @const {string}
*/
const DEFAULT_THEME_COLOR = '#F1F3F4';

/**
* @implements {./media-pool.MediaPoolRoot}
Expand Down Expand Up @@ -348,6 +353,7 @@ export class AmpStory extends AMP.BaseElement {
this.initializeListeners_();
this.initializeListenersForDev_();

this.setThemeColor_();
this.storeService_.dispatch(Action.TOGGLE_UI, this.getUIType_());

this.navigationState_.observe(stateChangeEvent => {
Expand Down Expand Up @@ -418,6 +424,25 @@ export class AmpStory extends AMP.BaseElement {
});
}

/**
* @private
*/
setThemeColor_() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very unlikely, but publishers may provide a <meta name="theme-color" ... > when writing their story HTML. Should we try to check if there's one, so we don't override it?
We could check this by doing something like

if (this.win.document.querySelector('meta[name=theme-color]')) {
  return;
}

What do you think about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes a lot of sense! Will add in a check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that unlikely. E.g. the PWA audit in Lighthouse recommends it.

// Don't override the publisher's tag.
if (this.win.document.querySelector('meta[name=theme-color]')) {
return;
}
// The theme color should be copied from the story's primary accent color
// if possible, with the fall back being default light gray.
const meta = this.win.document.createElement('meta');
const ampStoryEl = this.win.document.getElementsByTagName('amp-story')[0];
const styles = computedStyle(this.win, ampStoryEl);
meta.name = 'theme-color';
meta.content = styles.getPropertyValue('--primary-color') ||
DEFAULT_THEME_COLOR;
this.win.document.getElementsByTagName('head')[0].appendChild(meta);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.win.document.head

}

/**
* @private
*/
Expand Down
10 changes: 10 additions & 0 deletions extensions/amp-story/1.0/test/test-amp-story.js
Expand Up @@ -398,6 +398,16 @@ describes.realWin('amp-story', {
});
});

it('should have a meta tag that sets the theme color', () => {
createPages(story.element, 2);
story.buildCallback();
return story.layoutCallback()
.then(() => {
const metaTag = win.document.querySelector('meta[name=theme-color]');
expect(metaTag).to.not.be.null;
});
});

describe('amp-story consent', () => {
it('should pause the story if there is a consent', () => {
sandbox.stub(Services, 'actionServiceForDoc')
Expand Down