Spanish Total.js
English
Documentation For AI agents
Contents

Your first component

Write a Greeting component, load it in UI Builder, change its settings and publish it.

.md GitHub
On this page

This page builds Greeting, a component that shows a short text in a coloured box. The user sets the text and the weight of the font in its settings.

Write the files

Create the folder public/components/greeting/ inside the folder of UI Builder, and put three files in it.

editor.html:

<style>
    .CLASS { padding: 15px 20px; border-radius: var(--radius); background-color: #EEF3FF; color: #2A4CB0; font-size: 16px; }
    .CLASS.CLASS_bold { font-weight: bold; }
</style>

<script>

    exports.id = 'greeting';
    exports.name = 'Greeting';
    exports.group = 'Demo';
    exports.author = 'Jane Doe';
    exports.icon = 'ti ti-comment';
    exports.version = '1';
    exports.config = { text: 'Hello, world!', bold: false };
    exports.render = 'auto';
    exports.settings = 'auto';

    exports.make = function(instance, config, element, cls) {

        instance.on('configure', function() {
            element.text(config.text);
            element.tclass(cls + '_bold', config.bold == true);
        });

        instance.emit('configure');
    };

</script>

<readme>
Shows a short greeting. Set the text and the weight of the font in the settings.
</readme>

render.html:

<style>
    .CLASS { padding: 15px 20px; border-radius: var(--radius); background-color: #EEF3FF; color: #2A4CB0; font-size: 16px; }
    .CLASS.CLASS_bold { font-weight: bold; }
</style>

<script>

    exports.id = 'greeting';

    exports.make = function(instance, config, element, cls) {
        element.text(config.text);
        element.tclass(cls + '_bold', config.bold == true);
    };

</script>

settings.html:

<div class="padding">
    <ui-component name="input" path="?.text" config="required:1;placeholder:Hello, world!" class="m">Text</ui-component>
    <ui-component name="input" path="?.bold" config="type:checkbox">Bold text</ui-component>
</div>

UI Builder serves its public folder, so the component is at http://localhost:8000/components/greeting/editor.html. The files can also be on any web server that allows requests from the address of UI Builder (CORS).

Load it

  1. Start a design from the Web app template.
  2. Select Import components at the top of the component list, and then Import custom.
  3. In URL address, enter http://localhost:8000/components/greeting/editor.html.
  4. Select IMPORT.

Greeting appears in the list on the left, in the group Demo. See Import a custom component.

Use it

  1. Drag Greeting onto the canvas. It shows Hello, world!, the default text from exports.config.
  2. Double-click it to open its settings.
  3. Type Welcome to Demo project in Text, select Bold text and select SUBMIT.

The settings of the Greeting component with the text Welcome to Demo project

The form shows Name and Read value from first. UI Builder adds these fields to every component. Your fields from settings.html follow.

Publish it

Select Publish and Download HTML. The published page loads render.html of your component and shows the greeting. See Publish a design.

What the code does

  • exports.id, exports.name, exports.group, exports.icon and the other properties describe the component. See Component declaration.
  • exports.config holds the default settings of a new instance.
  • UI Builder calls exports.make once for each instance. It receives the instance, its config, its element (a jQuery object) and cls, the class name of the component.
  • In the editor, instance.on('configure', ...) runs after the user submits the settings. The code calls it once with instance.emit('configure') to draw the first look.
  • In the published design, the settings do not change, so render.html draws the text once.

Change the component

Edit the files and select Re-load components (circular arrows) in the toolbar. The canvas is built again with the new code.

Made in Europe