Component used to present a block of code in a Toolkit element

The code block is parsed with Prettier before it is rendered.


import React, { useEffect, useRef } from 'react'; 
import Reactium, { useHookComponent } from 'reactium-core/sdk';

const SomeCode = `
    // Here's some code:
    console.log('Hello Whomans'); 

const MyCodeElement = () => {
    // Reference placeholder
    const codeRef = useRef(); 
    // Get the Code component from Reactium.Component registry
    const { Code } = useHookComponent('RTK');
    // Change handler 
    const changed = e => {
    // Listen to change event as a side-effect
    useEffect(() => {
        if (!codeRef.current) return; 
        codeRef.current.addEventListener('change', changed); 
        return () => {
            codeRef.current.removeEventListener('change', changed);
    }, [codeRef.current]);
    // Render
    return <Code value={SomeCode} ref={codeRef} onChange={changed} />;


The Code component is an implementation of Codemirror and most of the properties are passed through to the Codemirror instance. If you want more control over the Code output you can directly apply configuration to the Codemirror instance via the editor property.



Triggered when the code value is changed.


You can theme the Code block by supplying the following scss variables before the Toolkit styles are imported:


Last updated