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

Conversation

bramanudom
Copy link
Contributor

Closes #19203

Added theme-color meta-tags based on the primary accent color of the story. Should the primary accent color not exist, the default theme-color is set to light gray with black text (which is the default for lighter theme colors).

@bramanudom
Copy link
Contributor Author

@newmuis PTAL

Copy link
Contributor

@newmuis newmuis left a comment

Choose a reason for hiding this comment

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

Awesome! Just a few small changes

let meta = document.createElement('meta');
const styles = getComputedStyle(document.body);
meta.name = "theme-color";
meta.content = styles.getPropertyValue('primary-color') || '#F1F3F4';
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can you extract the #F1F3F4 to a constant (e.g. DEFAULT_THEME_COLOR) declared at the top of this file?

// if possible, with the fall back being default light gray.
let meta = document.createElement('meta');
const styles = getComputedStyle(document.body);
meta.name = "theme-color";
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Use single quotes for string values

setThemeColor_(){
// The theme color should be copied from the story's primary accent color
// if possible, with the fall back being default light gray.
let meta = document.createElement('meta');
Copy link
Contributor

Choose a reason for hiding this comment

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

So, AMP is very weird about the way that we use document. If possible, you want to get it from the window object (i.e. this.win.document), because IIRC in some cases, there could be more than one window, and/or more than one document.

Copy link
Contributor

@gmajoulet gmajoulet left a comment

Choose a reason for hiding this comment

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

Great!!

meta.name = 'theme-color';
meta.content = styles.getPropertyValue('primary-color') ||
DEFAULT_THEME_COLOR;
document.getElementsByTagName('head')[0].appendChild(meta);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you want to apply what you used to get the document (this.win.document) to the other document statements :)

/**
* @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.

story.buildCallback();
const metaTags = story.element.getElementsByTagName('meta');
const metaTagNames = Array.from(metTagNames).map(el => el.name);
expect(metaTagNames.contains('theme-color')).to.be.true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: you could avoid looping through an array by selecting your meta tag directly. Something like
const metaTag = win.document.querSelector('meta[name=theme-color]');

const styles = computedStyle(this.win.document.body);
meta.name = 'theme-color';
meta.content = styles.getPropertyValue('primary-color') ||
DEFAULT_THEME_COLOR;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think the style is to indent hanging lines +4 spaces from the previous line. So:

meta.content = styles.getPropertyValue('primary-color') ||
    DEFAULT_THEME_COLOR;

@gmajoulet gmajoulet merged commit ef1c595 into ampproject:master Nov 20, 2018
@bramanudom bramanudom deleted the theme_color branch November 21, 2018 16:14
/**
* @private
*/
setThemeColor_() {
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.

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

Enriqe pushed a commit to Enriqe/amphtml that referenced this pull request Nov 28, 2018
* Added theme-color based on accent color for ampstory

* Added theme-color based on accent color for ampstory

* Updated JSDoc for method

* fixed lint errors and test errors

* fixed suggestions from code review

* use computed style helper instead of getComputedStyle

* fixed tests, implemented code review suggestions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants