The "Online Code Editor" project is a web application that allows users to write, edit, and preview HTML, CSS, and JavaScript code in real-time. It provides a user-friendly interface with three separate code editors for HTML, CSS, and JS, and a live preview pane to view the output.
Use the live Project: https://himanshu4776.github.io/Web-IDE/
- React: The frontend of the application is built using React, a popular JavaScript library for building user interfaces.
- HTML, CSS, and JavaScript: The project utilizes these three fundamental web technologies to enable code editing and rendering.
- LocalStorage: The project uses LocalStorage to store the user's code so that it is preserved across sessions.
- useEffect Hook: React's useEffect hook is used to manage side effects, such as updating the preview pane when the code changes.
- Iframe: An iframe element is used to display the live preview of the rendered HTML, CSS, and JavaScript code.
The project consists of the following main files and directories:
-
App.js
: This file contains the main component of the application,App()
. It manages the state of the HTML, CSS, and JS code using React hooks (useState
,useEffect
) and coordinates the rendering of the code editors and the preview pane. -
App.css
: This file contains the CSS styles used for the layout and appearance of the application. -
components/Editor.js
: This directory contains theEditor
component, which represents an individual code editor. It receives props for the language (HTML, CSS, or JS), the display name, the initial code value, and a callback function to update the code when changes occur. -
hooks/useLocalStorage.js
: This directory contains theuseLocalStorage
custom hook. It provides a convenient way to store and retrieve data from the browser's LocalStorage. -
constants.js
: This file defines the initial starter code for HTML and CSS editors, which are used when the application starts.
-
On application startup, the
App()
component uses theuseLocalStorage
hook to retrieve the previously saved HTML and CSS code from LocalStorage. If no code is found, it initializes the HTML and CSS code using the constants defined inconstants.js
. -
The three code editors (
Editor
components) for HTML, CSS, and JS are displayed side by side in the top pane of the application. Users can edit the code in each editor, and the changes are immediately reflected in the live preview pane. -
Whenever the user modifies the code in any of the editors, the
onChange
callback passed to theEditor
component is triggered. The callback updates the state of the corresponding code (HTML, CSS, or JS) in theApp()
component. -
To provide a seamless preview experience, the
App()
component uses theuseEffect
hook to update thesrcDoc
state variable whenever there are changes to the HTML, CSS, or JS code. ThesrcDoc
contains the rendered code, which is then displayed in the iframe. -
The iframe is sandboxed with the
sandbox="allow-scripts"
attribute to prevent any malicious scripts from being executed. It provides a secure environment to preview the code.
- Clone the repository to your local machine.
- Run
npm install
to install the required dependencies. - Run
npm start
to start the development server. - The application will open in your default web browser, allowing you to use the online code editor.
- User Authentication: Implement user authentication to allow users to save and share their code snippets.
- Theme Customization: Add the ability for users to customize the editor's theme and appearance.
- Additional Language Support: Extend the editor to support other programming languages and frameworks.
The "Online Code Editor" project is a simple yet powerful tool for developers to experiment, test, and visualize their code in real-time. It provides a seamless editing experience and an instant preview of the code output. With its extensible architecture, the project serves as a foundation for adding more features and functionalities in the future.