Skip to content

Instantly share code, notes, and snippets.

@nleush
Last active September 25, 2021 03:45
Show Gist options
  • Save nleush/01fabc8d1287e64d68515b554be7057c to your computer and use it in GitHub Desktop.
Save nleush/01fabc8d1287e64d68515b554be7057c to your computer and use it in GitHub Desktop.
// Include Iframely embed.js in your page using preferred method. E.g.:
// <script src="//cdn.iframe.ly/embed.js?api_key=[your key here]"></script>
// Add following methods to your component.
componentDidMount: function() {
// Builds widget after embed code rendered.
window.iframely && iframely.load();
},
getIframelyHtml: function() {
// Use embed code from API.
return {__html: this.iframelyEmbedHtmlCode};
// Alternatively you can use widget generator without embed code from API.
// return {__html: '<a href="' + this.url + '" data-iframely-url>' + this.title + '</a>'};
}
render: function () {
// Render raw widget html using 'dangerouslySetInnerHTML' attribute.
return <div dangerouslySetInnerHTML={this.getIframelyHtml()} />
}
@taylorhlabs
Copy link

taylorhlabs commented Sep 25, 2021

@tlester - Here is a code snippet that is working with a functional component. Note I'm using Next.js so you'll need to inject the embed.js script another way if you're not using Next.js.

import { useEffect, useState } from 'react';
import Script from 'next/script';
import axios from 'axios';

export default function Iframely() {
  const [html, setHtml] = useState(null);

  useEffect(() => {
    async function getHtml() {
      const url = encodeURIComponent(
        'https://gist.github.com/nleush/7e7775c9709eb3bdb6e6'
      );
      const apiUrl = `https://iframe.ly/api/iframely?url=${url}&api_key={your_api_key}&iframe=1&omit_script=1`;
      const res = await axios(apiUrl);
      console.log(res.data);
      const html = res.data.html;
      setHtml(html);
    }
    getHtml();
  }, []);

  return (
    <>
      <Script src="//cdn.iframe.ly/embed.js" strategy="beforeInteractive" />
      <div dangerouslySetInnerHTML={{ __html: html }} />
    </>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment