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

🐛 Fix amp-twitter component resizing issue #37740

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions extensions/amp-twitter/1.0/base-element.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import {PreactBaseElement} from '#preact/base-element';
import {createParseAttrsWithPrefix} from '#preact/parse-props';

import {BentoTwitter} from './component';
import {CSS} from './component.jss';

export class BaseElement extends PreactBaseElement {}

@@ -25,3 +26,6 @@ BaseElement['usesShadowDom'] = true;

/** @override */
BaseElement['loadable'] = true;

/** @override */
BaseElement['shadowCss'] = CSS;
18 changes: 16 additions & 2 deletions extensions/amp-twitter/1.0/component.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import objstr from 'obj-str';

import {MessageType_Enum, deserializeMessage} from '#core/3p-frame-messaging';

import * as Preact from '#preact';
import {useCallback, useMemo, useState} from '#preact';
import {useCallback, useMemo, useRef, useState} from '#preact';
import {forwardRef} from '#preact/compat';
import {useValueRef} from '#preact/component';
import {ProxyIframeEmbed} from '#preact/component/3p-frame';

import {useStyles} from './component.jss';

/** @const {string} */
const TYPE = 'twitter';
const FULL_HEIGHT = '100%';
@@ -40,6 +44,9 @@ function BentoTwitterWithRef(
const [height, setHeight] = useState(null);
const onLoadRef = useValueRef(onLoad);
const onErrorRef = useValueRef(onError);
const iframeRef = useRef(ref);
const classes = useStyles();
const [isLoading, setIsLoading] = useState(true);

const messageHandler = useCallback(
(event) => {
@@ -48,6 +55,10 @@ function BentoTwitterWithRef(
const height = data['height'];
if (requestResize) {
requestResize(height);

// Remove the load wrapper styling from embed after the resize is complete.
setIsLoading(false);

setHeight(FULL_HEIGHT);
} else {
setHeight(height);
@@ -90,14 +101,17 @@ function BentoTwitterWithRef(
return (
<ProxyIframeEmbed
allowfullscreen
ref={ref}
ref={iframeRef}
title={title}
{...rest}
// non-overridable props
matchesMessagingOrigin={MATCHES_MESSAGING_ORIGIN}
messageHandler={messageHandler}
options={options}
type={TYPE}
class={objstr({
[classes.loadWrapper]: isLoading,
})}
style={height ? {...style, height} : style}
/>
);
22 changes: 22 additions & 0 deletions extensions/amp-twitter/1.0/component.jss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {createUseStyles} from 'react-jss';

// This style is added as part of a workaround to fix the component resizing issue because
// the resizing happens only when the embed comes into viewport and most of the time the
// attemptChangeHeight request is gets rejected.
const loadWrapper = {
position: 'fixed !important',
opacity: '0',
top: '0',
bottom: '0',
left: '0',
right: '0',
pointerEvents: 'none',
};

const JSS = {
loadWrapper,
};

// useStyles gets replaced for AMP builds via `babel-plugin-transform-jss`.
// eslint-disable-next-line local/no-export-side-effect
export const useStyles = createUseStyles(JSS);
4 changes: 0 additions & 4 deletions extensions/amp-twitter/1.0/test/test-component.js
Original file line number Diff line number Diff line change
@@ -111,13 +111,9 @@ describes.sandboxed('Twitter preact component v1.0', {}, (env) => {
/>
);

let api = ref.current;
expect(api.readyState).to.equal('loading');
expect(onReadyState).to.not.be.called;

await wrapper.find('iframe').invoke('onLoad')();
api = ref.current;
expect(api.readyState).to.equal('complete');
expect(onReadyState).to.be.calledOnce.calledWith('complete');
});