-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
✨ Proof of concept React BaseElement #24153
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,18 +61,16 @@ AMP.extension('amp-react-img', '0.1', AMP => { | |
*/ | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = {prerender: true}; | ||
} | ||
|
||
/** | ||
* @return {*} | ||
*/ | ||
render() { | ||
const props = pick(this.props, ATTRIBUTES_TO_PROPAGATE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these not camel-cased? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a local binding at the top of the array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant: are the attributes->props camel-cased? I.e. should it be "aria-label" or "ariaLabel"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Playing around, it appears to work either way... I'm not sure. |
||
const { | ||
id, | ||
'i-amphtml-ssr': ssr, | ||
'i-amphtml-prerender': prerender, | ||
} = this.props; | ||
const {id, 'i-amphtml-ssr': ssr} = this.props; | ||
if (ssr) { | ||
// TODO: Figure out SSR children | ||
} | ||
|
@@ -83,6 +81,7 @@ AMP.extension('amp-react-img', '0.1', AMP => { | |
props['decoding'] = 'async'; | ||
|
||
// amp-img is always allowed to render, but we want to make this interesting... | ||
const {prerender} = this.state; | ||
if (prerender) { | ||
delete props['src']; | ||
delete props['srcset']; | ||
|
@@ -112,6 +111,9 @@ AMP.extension('amp-react-img', '0.1', AMP => { | |
/** @param {!AmpElement} element */ | ||
constructor(element) { | ||
super(element); | ||
|
||
/** @private {?Component} */ | ||
this.el_ = null; | ||
} | ||
|
||
/** | ||
|
@@ -129,34 +131,29 @@ AMP.extension('amp-react-img', '0.1', AMP => { | |
|
||
/** @override */ | ||
buildCallback() { | ||
const el = react.createElement( | ||
Component, | ||
this.attributes_(/* prerender */ true) | ||
); | ||
render(el, this.element); | ||
const el = react.createElement(Component, this.attributes_()); | ||
this.el_ = render(el, this.element); | ||
jridgewell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** @override */ | ||
layoutCallback() { | ||
const el = react.createElement( | ||
Component, | ||
this.attributes_(/* prerender */ false) | ||
); | ||
render(el, this.element); | ||
const el = devAssert(this.el_); | ||
el.setState({prerender: false}); | ||
|
||
const rerender = react.createElement(Component, this.attributes_()); | ||
jridgewell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
render(rerender, this.element); | ||
jridgewell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @param {boolean} prerender | ||
* @return {!Object<string, string>} | ||
*/ | ||
attributes_(prerender) { | ||
attributes_() { | ||
const out = {}; | ||
const {attributes} = this.element; | ||
for (let i = 0, l = attributes.length; i < l; i++) { | ||
const attr = attributes[i]; | ||
out[attr.name] = attr.value; | ||
} | ||
out['i-amphtml-prerender'] = prerender; | ||
return out; | ||
} | ||
}; | ||
|
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.
Definitely subject to bikeshedding, but imho, this should be something like "loading=auto|manual", since there might be other reasons to block rendering, besides prerendering.
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.
Yup, I'm not tied to
prerender
at all, it just the first name I thought of.