How to embed CodePen in Next js 13.4
author

Jon Snow

05 October 2023

How to embed CodePen in Next js 13.4


There are two methods for embeding codepen in next js

  1. react-codepen-embed
  2. React Iframe (Recommended)

1. react-codepen-embed

Install

npm i react-codepen-embed

Example

import React from "react";
import Codepen from "react-codepen-embed";

const CodepenEmbedded = () => {
  return <Codepen hash="LYXLqpx" user="chrisgannon" />;
};

export default CodepenEmbedded;

Component Props

how to embed codepen in next js



2. React Iframe (Recommended)

Install

npm i react-iframe

Example

  1. Make EmbedCodePen Component
"use client";
import React from "react";
import Iframe from "react-iframe";
const EmbedCodePen = (props) => {
  const { author, slug } = props;
  // example https://codepen.io/chrisgannon/pen/LYXLqpx
  // here author is chrisgannon and slug is LYXLqpx
  return (
    <>
      <div className="preloaded">
        <Iframe
          url={`https://codepen.io/${author}/embed/${slug}?default-tab=result&editable=true&theme-id=dark`}
          width="100%"
          height="450px"
          loading="lazy"
          id="myId"
          className="myClassname"
          display="initial"
          position="relative"
          allowFullScreen={true}
        />
      </div>
    </>
  );
};

/* 
preloaded class css code:
.preloaded {
  background: linear-gradient(to right, #eee 8%, #e1e1e1 18%, #eee 33%);
  background-size: 200% 50px;
  animation: preloadAnimation 2s infinite;
}

@keyframes preloadAnimation {
  from {
    background-position: -280px 0;
  }

  to {
    background-position: 300px 0;
  }
}
*/

export default EmbedCodePen;
  1. Use EmbedCodePen Component
import EmbedCodePen from "./EmbedCodePen";

const App = () => {
  return (
    <>
      <EmbedCodePen author="chrisgannon" slug="LYXLqpx" />
    </>
  );
};

export default App;

Properties

how to embed codepen in next js



  1. Output

how to add codepen in nextjs

Check more https://www.npmjs.com/package/react-iframe

Thanks ❤️❤️


Share:  
https://www.democoding.in/blog...

Related Post

programming meme
Code Snippet

Codepen Ideas