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

amp-sticky-ad: Force background-color alpha to be 1 #5819

Merged
merged 9 commits into from Oct 27, 2016

Conversation

zhouyx
Copy link
Contributor

@zhouyx zhouyx commented Oct 25, 2016

I force the background-color alpha to be 1 in this PR. Didn't add opacity: 1 !important though. opacity:0 will also apply opacity to amp-ad, I assume no one is using that.

cc @jasti

@@ -23,7 +23,7 @@ amp-sticky-ad {
z-index: 11;
max-height: 100px !important;
box-sizing: border-box;
background-color: rgba(0, 0, 0, 0.5);
background-color: #fff;
Copy link
Contributor

Choose a reason for hiding this comment

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

as discussed remove this


/** @private @const {string} */
const TAG = 'amp-sticky-ad-v1';
Copy link
Contributor

Choose a reason for hiding this comment

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

name this UX_EXPERIMENT, unlike other cases we are not sharing tag name and experiment name here.

Also maybe a more descriptive experiment name? amp-sticky-ad-better-ux ?

Also cleanup issue for the experiment with a comment pointing to issue #


/**
* To check for background-color alpha and force it to be 1.
* Whoever call this needs to make sure it's in a vsync.
Copy link
Contributor

Choose a reason for hiding this comment

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

*calls

if (!isExperimentOn(this.win, TAG)) {
return;
}
const background = this.win.getComputedStyle(this.element)
Copy link
Contributor

Choose a reason for hiding this comment

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

as discussed, force opacity here with a TODO to move it to CSS

if (!isExperimentOn(this.win, TAG)) {
return;
}
const background = this.win.getComputedStyle(this.element)
Copy link
Contributor

Choose a reason for hiding this comment

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

move this logic to style.js with some unit tests. Maybe something like removeAlphaFromColor(cssColorValue)

}
const background = this.win.getComputedStyle(this.element)
.getPropertyValue('background-color');
if (!startsWith(background, 'rgba')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

there is also hsla

if (!startsWith(background, 'rgba')) {
return;
}
user().warn('AMP-STICKY-AD', 'Do not allow container to be transparent');
Copy link
Contributor

Choose a reason for hiding this comment

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

just having grba or hsla does not mean it is transparent, we need to check if alpha value is < 1.

Copy link
Contributor Author

@zhouyx zhouyx Oct 26, 2016

Choose a reason for hiding this comment

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

I found that when rgba has an alpha value equal or greater than 1. getComputedStyle background-color will always return rgb instead. I tested this with Chrome and Safari, haven't tested with hsla, but I assume it would be the same.

}

// TODO(@zhouyx): Move the opacity style to CSS after remove experiments
// Not using setStyles because we will remove this line later.
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm? It's never removed.

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 will remove once we change our ux and decide to remove experimental protection.

return;
}

user().warn('AMP-STICKY-AD', 'Do not allow container to be transparent');
Copy link
Contributor

Choose a reason for hiding this comment

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

#error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, I thought about using warn or error. #error feels too strong to me

* @param{string} cssColorValue
* @return {?string}
*/
export function removeAlphaFromBackgroundColor(cssColorValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

removeAlphaFromColor

* Return the new "background-color" property without alpha.
* Note caller needs to make sure the input cssColorValue is a valid
* CSS "background-color" value.
* @param{string} cssColorValue
Copy link
Contributor

Choose a reason for hiding this comment

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

spacing.

* @param{string} cssColorValue
* @return {?string}
*/
export function removeAlphaFromBackgroundColor(cssColorValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be simplified down to:

return cssColor.replace(/\(([^,]+),([^,]+),([^,)]+),[^)]+\)/g, '($1,$2,$3, 1)');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😨 OMG regular expression!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great suggestion! applied.

@zhouyx
Copy link
Contributor Author

zhouyx commented Oct 26, 2016

@jridgewell @aghassemi PTAL

Copy link
Contributor

@aghassemi aghassemi left a comment

Choose a reason for hiding this comment

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

LGTM, small comments

}

// TODO(@zhouyx): Move the opacity style to CSS after remove experiments
// Not using setStyles because we will remove this line later.
Copy link
Contributor

Choose a reason for hiding this comment

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

but you are using setStyle :)

return;
}

user().warn('AMP-STICKY-AD', 'Do not allow container to be transparent');
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: semitransparent would probably be more accurate than transparent

@@ -196,3 +195,16 @@ export function translate(x, opt_y) {
export function scale(value) {
return `scale(${value})`;
}

/**
* Remove alpha value from a "background-color" CSS property.
Copy link
Contributor

Choose a reason for hiding this comment

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

remove references to "background-color" in comments

expect(st.removeAlphaFromColor('rgb(1, 1, 1)')).to.equal(
'rgb(1, 1, 1)');
expect(st.removeAlphaFromColor('hsl(1, 1, 1)')).to.equal(
'hsl(1, 1, 1)');
Copy link
Contributor

Choose a reason for hiding this comment

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

add tests for percent-based RGBA as well:
hsla(120, 100%, 25%, 0.3)

and maybe some negative cases like:

'#333'
'transparent'
''
'white'
'totallyInvalid'

*/
export function removeAlphaFromColor(cssColor) {
return cssColor.replace(
/\(([^,]+),([^,]+),([^,)]+),[^)]+\)/g, '($1,$2,$3, 1)');
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

// TODO(@zhouyx): Move the opacity style to CSS after remove experiments
// Note: Use setStyle because we will remove this line later.
setStyle(this.element, 'opacity', '1 !important');
setStyle(this.element, 'background-image', 'none');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

add the background-image: none here as well

it('should not allow container to be set to transparent', () => {
toggleExperiment(window, 'amp-sticky-ad-better-ux', true);
return getAmpStickyAd({
'style': 'background-color: transparent !important',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

new test

@@ -196,3 +195,15 @@ export function translate(x, opt_y) {
export function scale(value) {
return `scale(${value})`;
}

/**
* Remove alpha value from a rgba color value.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

rephrase the comment

'rgba(1, 1, 1, 1)');
expect(st.removeAlphaFromColor('rgb(1, 1, 1)')).to.equal(
'rgb(1, 1, 1)');
expect(st.removeAlphaFromColor('rgba(0, 0, 0,-0.5)')).to.equal(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove hsla test since we assume it's not a valid input.

@zhouyx zhouyx merged commit d05a12e into ampproject:master Oct 27, 2016
@zhouyx zhouyx deleted the amp-sticky-improve2 branch October 27, 2016 18:10
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
* check background-color alpha

* s

* update JSdoc

* address comment 1

* refactor func to style.js

* fix test

* apply regular expression

* address comment

* add new test
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
* check background-color alpha

* s

* update JSdoc

* address comment 1

* refactor func to style.js

* fix test

* apply regular expression

* address comment

* add new test
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

3 participants