Custom Elements

Create your own element types for specialized inputs beyond the built-in types.

circle-exclamation

Overview

Custom elements allow you to:

  • Create specialized form inputs

  • Build complex UI components

  • Implement custom validation

  • Add unique interactions


Creating a Custom Element

Step 1: Define the Render Function

Custom elements use a render function that returns an HTML string.

const colorPickerRenderer = (element, value, uniqueId) => {
  const currentValue = value || element.default || '#000000';
  
  return `
    <div class="settings-input-group" data-element-id="${element.id}">
      <label for="${uniqueId}">
        ${element.label}
        ${element.required ? '<span style="color: var(--col-danger)"> *</span>' : ''}
      </label>
      
      ${element.description ? `<p class="settings-description">${element.description}</p>` : ''}
      
      <div style="display: flex; align-items: center; gap: 10px;">
        <input
          type="color"
          id="${uniqueId}-color"
          value="${currentValue}"
          onchange="this.nextElementSibling.value = this.value; this.dispatchEvent(new CustomEvent('setting-changed', {detail: {id: '${element.id}', value: this.value}, bubbles: true}));"
          style="width: 60px; height: 40px; cursor: pointer;"
        />
        <input
          type="text"
          id="${uniqueId}"
          value="${currentValue}"
          placeholder="#000000"
          oninput="this.previousElementSibling.value = this.value; this.dispatchEvent(new CustomEvent('setting-changed', {detail: {id: '${element.id}', value: this.value}, bubbles: true}));"
          style="flex: 1; padding: 8px 12px; background-color: var(--col-input-default); border: 1px solid var(--col-ouliner-default); border-radius: var(--radius-sm); color: var(--col-txt-primary);"
        />
      </div>
      
      ${element.hint ? `<small style="color: var(--col-txt-secondary); font-size: 12px;">${element.hint}</small>` : ''}
    </div>
  `;
};

Step 2: Register the Element Type

Step 3: Use in Settings


Render Function Reference

Parameters

Parameter
Type
Description

element

Object

The element configuration object with id, label, type, default, etc.

value

Any

Current value of the element

uniqueId

String

Unique ID for DOM elements (use for input IDs)

Return Value

Must return a string containing HTML that will be inserted into the DOM.


Event Handling

To update values, dispatch a custom event from your input:

Example with Event


Examples

Simple Rating Element

File Size Selector

Tag Input


CSS Variables

Use these CSS variables for consistent theming:

Example with Variables


Best Practices

1. Always Use uniqueId for Element IDs

2. Dispatch Events on Change

3. Escape Values Properly

4. Use CSS Variables

5. Provide Fallback Values


Troubleshooting

Element Not Rendering

Problem: Custom element doesn't appear.

Solutions:

  1. Check that element is registered before use

  2. Verify render function returns a string

  3. Check for syntax errors in HTML

Values Not Saving

Problem: Changes don't persist.

Solutions:

  1. Ensure setting-changed event is dispatched

  2. Check event bubbles: bubbles: true

  3. Verify element ID matches in event detail

Styling Issues

Problem: Element doesn't match theme.

Solutions:

  1. Use CSS variables instead of hardcoded colors

  2. Wrap in settings-input-group div

  3. Test in both light and dark modes


Next Steps

  • See more examples →

  • Learn about best practices →

  • Read about reading settings →

Last updated