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

✨ Proof of concept React BaseElement #24153

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Store component to get state
  • Loading branch information
jridgewell committed Aug 24, 2019
commit 6fa7dc3444c87976144d792f259b0393a872931d
33 changes: 15 additions & 18 deletions extensions/amp-react-img/0.1/amp-react-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,16 @@ AMP.extension('amp-react-img', '0.1', AMP => {
*/
constructor(props) {
super(props);

this.state = {prerender: true};
Copy link
Contributor

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.

Copy link
Contributor Author

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.

}

/**
* @return {*}
*/
render() {
const props = pick(this.props, ATTRIBUTES_TO_PROPAGATE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these not camel-cased?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a local binding at the top of the array.

Copy link
Contributor

Choose a reason for hiding this comment

The 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"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Playing around, it appears to work either way... I'm not sure. aria-label should definitely be lowercase, though: https://reactjs.org/docs/dom-elements.html.

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
}
Expand All @@ -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'];
Expand Down Expand Up @@ -112,6 +111,9 @@ AMP.extension('amp-react-img', '0.1', AMP => {
/** @param {!AmpElement} element */
constructor(element) {
super(element);

/** @private {?Component} */
this.el_ = null;
}

/**
Expand All @@ -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;
}
};
Expand Down