InstallationComponents

CodeNexus

The main component that provides a full-featured code editor with live preview. It supports HTML, CSS, and JavaScript editing with real-time preview and many advanced features.

Basic Usage

<CodeNexus
  html="<h1>Hello World</h1>"
  css="h1 { color: blue; }"
  javascript="console.log('Hello from Code Nexus');"
/>

Properties

General

PropertyDescriptionTypeDefault
htmlHTML contentstringEmpty string
cssCSS contentstringEmpty string
javascriptJavaScript contentstringEmpty string
debounceTimeTime to debounce updates to the previewnumber300

Appearance

PropertyDescriptionTypeDefault
hideEditorsHides the live editor containersbooleanfalse
tabbedSwitches from split view to a tabbed viewbooleanfalse
themeTheme configuration{ colors, dark }Dark theme

Templates

PropertyDescriptionTypeDefault
enableTemplatesAbility to load example/starter templatesbooleanfalse
templatesCustom starter templates for quick loading{ name, html, css, javascript }[][]

Formatting

PropertyDescriptionTypeDefault
enableFormattingEnables code formatting functionalitybooleanfalse
formattingOptionsOptions for code formattingFormattingOptions{}
showFormattingButtonsShow formatting buttons in the UIbooleantrue

Events

contentChange

Event emitted when any editor content changes

{ html: string; css: string; javascript: string }

Example:

<CodeNexus
  onContentChange={(event) => {
    const { html, css, javascript } = event.detail;
    console.log('Content changed:', { html, css, javascript });
  }}
/>

Methods

loadTemplate(template)

Loads a template into the editor

Parameters:

template: { html: string; css: string; javascript: string }

Returns:

Promise<void>

Example:

// Get a reference to the component
const codeNexus = document.querySelector('code-nexus');

// Load a template
codeNexus.loadTemplate({
  html: '<h1>Hello World</h1>',
  css: 'h1 { color: blue; }',
  javascript: 'console.log("Hello");'
}).then(() => {
  console.log('Template loaded successfully');
});

formatAll()

Format all code sections

Returns:

Promise<void>

Example:

// Get a reference to the component
const codeNexus = document.querySelector('code-nexus');

// Format all code
codeNexus.formatAll().then(() => {
  console.log('Code formatted successfully');
});

Advanced Examples

Templates

You can provide templates for users to quickly load and experiment with different code examples:

<CodeNexus
  enableTemplates={true}
  templates={[
    {
      name: "Hello World",
      html: "<h1>Hello</h1>",
      css: "h1 { color: blue }",
      javascript: "console.log('Hello');"
    },
    {
      name: "Counter",
      html: "<button id='counter'>Count: 0</button>",
      css: "button { padding: 10px; }",
      javascript: "let count = 0; document.getElementById('counter').addEventListener('click', () => { count++; document.getElementById('counter').textContent = 'Count: ' + count; });"
    }
  ]}
/>

Code Formatting

Enable code formatting to allow users to format their code with a single click:

<CodeNexus 
  enableFormatting={true}
  formattingOptions={{ printWidth: 100, tabWidth: 2, singleQuote: true }}
/>

React Integration

Use the React wrapper for easier integration in React applications:

import { CodeNexus } from "code-nexus-react";

function App() {
  const [html, setHtml] = useState("<h1>Hello World</h1>");
  const [css, setCss] = useState("h1 { color: blue; }");
  const [js, setJs] = useState("console.log('Hello');");

  const handleContentChange = (event) => {
    const { html, css, javascript } = event.detail;
    setHtml(html);
    setCss(css);
    setJs(javascript);
  };

  return (
    <CodeNexus
      html={html}
      css={css}
      javascript={js}
      onContentChange={handleContentChange}
    />
  );
}

Editable Interactive Example

This example is fully editable. You can modify the HTML, CSS, and JavaScript to see your changes in real-time. The editor supports syntax highlighting, code formatting, and provides an instant preview of your code.

Editable Example - Try modifying the code below