forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstagram.js
More file actions
89 lines (81 loc) · 2.5 KB
/
instagram.js
File metadata and controls
89 lines (81 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as Preact from '../../../src/preact';
import {ContainWrapper} from '../../../src/preact/component';
import {getData} from '../../../src/event-helper';
import {useMountEffect} from '../../../src/preact/utils';
import {useRef, useState} from '../../../src/preact';
/**
* @param {!InstagramProps} props
* @return {PreactDef.Renderable}
*/
export function Instagram({shortcode, captioned, style, alt, resize}) {
const iframeRef = useRef(null);
const [heightStyle, setHeightStyle] = useState(null);
const [opacity, setOpacity] = useState(0);
/**
* Upon component mount, the Instagram post's height is received and applied.
*/
useMountEffect(() => {
/**
* @param {Event} event
*/
function handleMessage(event) {
if (
event.origin != 'https://www.instagram.com' ||
event.source != iframeRef.current.contentWindow
) {
return;
}
const data = JSON.parse(getData(event));
if (data['type'] == 'MEASURE') {
if (typeof resize === 'function') {
resize(data['details']['height']);
} else {
setHeightStyle({'height': data['details']['height']});
}
setOpacity(1);
}
}
window.addEventListener('message', handleMessage);
return () => {
window.removeEventListener('message', handleMessage);
};
});
return (
<ContainWrapper style={{...style, ...heightStyle}} layout size paint>
<iframe
ref={iframeRef}
src={
'https://www.instagram.com/p/' +
(encodeURIComponent(shortcode) || 'error') +
'/embed/' +
(captioned ? 'captioned/' : '') +
'?cr=1&v=12'
}
scrolling="no"
frameborder="0"
allowtransparency
title={alt}
style={{
width: '100%',
height: '100%',
opacity,
}}
/>
</ContainWrapper>
);
}