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
Conversation
@@ -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; |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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')) { |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#error
?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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)');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😨 OMG regular expression!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great suggestion! applied.
@jridgewell @aghassemi PTAL |
There was a problem hiding this 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. |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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)'); |
There was a problem hiding this comment.
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)'); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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', |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
* 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
* 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
I force the
background-color
alpha to be 1 in this PR. Didn't addopacity: 1 !important
though.opacity:0
will also apply opacity toamp-ad
, I assume no one is using that.cc @jasti