DEV Community

Cover image for Building a CodePen Alternative with VS Code - Monaco Editor Dark Theme
Sh Raj
Sh Raj

Posted on • Updated on

Building a CodePen Alternative with VS Code - Monaco Editor Dark Theme

Title: Step-by-Step Guide to Building a CodePen Alternative with Monaco Editor and VS Code Dark Theme

See Demo :- https://codexd.sh20raj.repl.co/

Introduction:
CodePen is a popular online code editor that provides developers with a collaborative playground to write HTML, CSS, and JavaScript code. If you're looking to create a similar platform tailored to your needs, building a CodePen alternative can be an exciting and rewarding project. In this step-by-step guide, we will walk you through creating a basic CodePen alternative—a web-based code editor with real-time result preview and additional features, all powered by Monaco Editor and featuring the stylish VS Code Dark theme.

Prerequisites:
Before we get started, make sure you have a basic understanding of HTML, CSS, and JavaScript. Familiarity with web development tools and libraries like Monaco Editor will be helpful for this project.

Step 1: Set Up the Project Structure
Begin by creating a new directory for your project. Inside this directory, create an index.html file and a folder named css to store your project's CSS files. Additionally, create a folder named js to hold the JavaScript files.

Step 2: Include the Required Libraries
In the index.html file, include the necessary libraries for your CodePen alternative. For this example, we'll use Monaco Editor for code editing and Bootstrap for styling:

<!DOCTYPE html>
<html>
<head>
  <title>My CodePen Alternative with Monaco Editor</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.27.0/min/vs/loader.js"></script>
  <style>
    /* Your custom CSS styles here */
  </style>
</head>
<body>
  <!-- Your content goes here -->
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Editor Container
Next, create a container for the code editor. Add a div element with the ID editor-container where the Monaco Editor will be placed:

<body>
  <div class="container mt-4">
    <h1 class="mb-4">My CodePen Alternative with Monaco Editor</h1>
    <div id="editor-container"></div>
  </div>
</body>
Enter fullscreen mode Exit fullscreen mode

Step 4: Initialize Monaco Editor
Now, it's time to initialize the Monaco Editor and set its initial value. Add the following JavaScript code just before the closing </body> tag in the index.html file:

<body>
  <!-- Previous content -->

  <script>
    require.config({ paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.27.0/min/vs' } });

    require(['vs/editor/editor.main'], function () {
      monaco.editor.defineTheme('vs-dark', {
        base: 'vs-dark',
        inherit: true,
        rules: [],
        colors: {}
      });

      var editor = monaco.editor.create(document.getElementById('editor-container'), {
        value: '<!-- Write your code here -->',
        language: 'html',
        theme: 'vs-dark'
      });
    });
  </script>
</body>
Enter fullscreen mode Exit fullscreen mode

Step 5: Add a Result Preview
To display the result of the user's code, add an iframe element below the editor container. This iframe will act as the result preview area:

<body>
  <div class="container mt-4">
    <h1 class="mb-4">My CodePen Alternative with Monaco Editor</h1>
    <div id="editor-container"></div>
    <iframe id="result-preview" class="mt-4" frameborder="0"></iframe>
  </div>

  <script>
    // Previous JavaScript code
  </script>
</body>
Enter fullscreen mode Exit fullscreen mode

Step 6: Update Result Preview on Code Change
We want the result preview to update in real-time as the user types. To achieve this, listen for changes in the editor and update the iframe source accordingly:

<body>
  <div class="container mt-4">
    <!-- Previous content -->
  </div>

  <script>
    // Previous JavaScript code

    // Listen for changes in the editor and update the result-preview accordingly
    editor.onDidChangeModelContent(function () {
      updateResultPreview();
    });

    function updateResultPreview() {
      var code = editor.getValue();
      var iframe = document.getElementById('result-preview');
      var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
      iframeDocument.open();
      iframeDocument.write(code);
      iframeDocument.close();
    }
  </script>
</body>
Enter fullscreen mode Exit fullscreen mode

Step 7: Customize and Enhance
Now that you have the core functionality of your CodePen alternative, you can customize and enhance it further. Add buttons for running the code, saving projects, and implementing additional features like adding JS/CSS resources, sharing, and collaboration.

Conclusion:
Congratulations! You've successfully created a basic CodePen alternative—a web-based code editor with real-time result preview, powered by Monaco Editor and featuring the stylish VS Code Dark theme. From here, you can continue to build upon the foundation by adding more features and customizing the experience to suit your needs and preferences.

Building a CodePen alternative is an exciting journey, and the possibilities are endless. Continue exploring and refining your platform, and you may create a valuable tool that caters to developers worldwide.


Top comments (0)