Skip to content

Instantly share code, notes, and snippets.

@wmbutler
Created December 24, 2021 01:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmbutler/d0da77b80b5bc5c036ba9d14947263af to your computer and use it in GitHub Desktop.
Save wmbutler/d0da77b80b5bc5c036ba9d14947263af to your computer and use it in GitHub Desktop.
Adding a react-youtube component to Plasmic.app

Plasmic react-youtube

I'm posting these notes because it wasn't very clear how to do this the first time I set it up. If you are using Plasmic to build a website or create react components, this will help you understand how to import existing React components. There are a few that the Plasmic Team has created wrappers for. I used this method to install it, but there were several manual steps that weren't all that obvious.

This assumes you already have created a github repo for your plasmic site and that you are following the NextJS path.

  1. On the root level, add this file plasmic-init.ts. Be sure to include your id and token. I've redacted them in this example:
import { initPlasmicLoader } from "@plasmicapp/loader-nextjs";
import {registerYouTube} from "@plasmicpkgs/react-youtube"; // This imports the Youtube module

export const PLASMIC = initPlasmicLoader({
  projects: [
    {
      id: "REDACTED",  // ID of a project you are using
      token: "REDACTED"  // API token for that project
    }
  ],
  // Fetches the latest revisions, whether or not they were unpublished!
  // Disable for production to ensure you render only published changes.
  preview: true,
});
registerYouTube(PLASMIC);  // This registers the Youtube App
  1. Make sure that yarn is installed on your system and working properly
sudo npm install -g yarn
yarn
  1. Install react-youtube. Go ahead and make sure plasmic loader is installed as well.
yarn install @plasmicapp/loader-nextjs
yarn install @plasmicpkgs/react-youtube
  1. Create this file inside the pages directory and name it plasmic-host.tsx. This is a weird little slight of hand that wraps the plasmic GUI into your machine running the site instead of plasmic's server. This means that it can incorporate whatever modules you add locally. In our case it's the react-youtube module.
import * as React from 'react';
import { PlasmicCanvasHost } from '@plasmicapp/loader-nextjs';
import Head from 'next/head';
// Some Next.js app configurations will cause a plain `import '../plasmic-init'` to be ignored.
import { PLASMIC } from '../plasmic-init';

export default function Host() {
  return (
    PLASMIC && (
      <div>
        <Head>
          <script
            dangerouslySetInnerHTML={{
              __html: `
          !function(){const n=window,i="__REACT_DEVTOOLS_GLOBAL_HOOK__",o="__PlasmicPreambleVersion",t=function(){}
  if(void 0!==n){if(n.parent!==n)try{n[i]=n.parent[i]}catch(e){}if(!n[i]){const r=new Map
  n[i]={supportsFiber:!0,renderers:r,inject:function(n){r.set(r.size+1,n)},onCommitFiberRoot:t,onCommitFiberUnmount:t}}n[i][o]||(n[i][o]
="1")}}()`
            }}
          ></script>
        </Head>
        <PlasmicCanvasHost />
      </div>
    )
  );
}
  1. Now from the plasmic studio app, https://studio.plasmic.app/projects you need to Configure the Project and set the protocol to http and the URL to localhost:3000/plasmic-host.

  2. Now, start up your project locally with yarn dev

  3. Now, open your Project at plasmic.app. You will see at the bottom, a category called Code Components with the Youtube App. You can drop it into any area of the project you prefer.

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