|
| 1 | +/** |
| 2 | + * Copyright 2020 The AMP HTML Authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as Preact from '../../../src/preact'; |
| 18 | +import {ContainWrapper} from '../../../src/preact/component'; |
| 19 | +import {dict} from '../../../src/utils/object'; |
| 20 | +import {getData} from '../../../src/event-helper'; |
| 21 | +import {parseJson} from '../../../src/json'; |
| 22 | +import {useLayoutEffect, useRef, useState} from '../../../src/preact'; |
| 23 | + |
| 24 | +const NO_HEIGHT_STYLE = dict(); |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {!InstagramPropsDef} props |
| 28 | + * @return {PreactDef.Renderable} |
| 29 | + */ |
| 30 | +export function Instagram({ |
| 31 | + shortcode, |
| 32 | + captioned, |
| 33 | + title, |
| 34 | + requestResize, |
| 35 | + loading = 'lazy', |
| 36 | + ...rest |
| 37 | +}) { |
| 38 | + const iframeRef = useRef(null); |
| 39 | + const [heightStyle, setHeightStyle] = useState(NO_HEIGHT_STYLE); |
| 40 | + const [opacity, setOpacity] = useState(0); |
| 41 | + |
| 42 | + useLayoutEffect(() => { |
| 43 | + const messageHandler = (event) => { |
| 44 | + if ( |
| 45 | + event.origin != 'https://www.instagram.com' || |
| 46 | + event.source != iframeRef.current.contentWindow |
| 47 | + ) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const data = parseJson(getData(event)); |
| 52 | + |
| 53 | + if (data['type'] == 'MEASURE' && data['details']) { |
| 54 | + const height = data['details']['height']; |
| 55 | + if (requestResize) { |
| 56 | + requestResize(height); |
| 57 | + } else { |
| 58 | + setHeightStyle(dict({'height': height})); |
| 59 | + } |
| 60 | + setOpacity(1); |
| 61 | + } |
| 62 | + }; |
| 63 | + const {defaultView} = iframeRef.current.ownerDocument; |
| 64 | + |
| 65 | + defaultView.addEventListener('message', messageHandler); |
| 66 | + |
| 67 | + return () => { |
| 68 | + defaultView.removeEventListener('message', messageHandler); |
| 69 | + }; |
| 70 | + }, [requestResize, heightStyle]); |
| 71 | + |
| 72 | + return ( |
| 73 | + <ContainWrapper {...rest} wrapperStyle={heightStyle} layout size paint> |
| 74 | + <iframe |
| 75 | + ref={iframeRef} |
| 76 | + src={ |
| 77 | + 'https://www.instagram.com/p/' + |
| 78 | + encodeURIComponent(shortcode) + |
| 79 | + '/embed/' + |
| 80 | + (captioned ? 'captioned/' : '') + |
| 81 | + '?cr=1&v=12' |
| 82 | + } |
| 83 | + scrolling="no" |
| 84 | + frameborder="0" |
| 85 | + allowtransparency |
| 86 | + title={title} |
| 87 | + loading={loading} |
| 88 | + style={{ |
| 89 | + width: '100%', |
| 90 | + height: '100%', |
| 91 | + opacity, |
| 92 | + contentVisibility: 'auto', |
| 93 | + }} |
| 94 | + /> |
| 95 | + </ContainWrapper> |
| 96 | + ); |
| 97 | +} |
0 commit comments