> For the complete documentation index, see [llms.txt](https://docs.realmacsoftware.com/elements-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.realmacsoftware.com/elements-docs/elements-language/component/hooks.js/available-functions/rw.setrootelement.md).

# rw\.setRootElement

The `rw.setRootElement()` function configures the root HTML element that wraps your component. This allows you to control the element tag, CSS classes, and HTML attributes.

## Syntax

```javascript
rw.setRootElement({
    as: 'tagName',      // HTML tag to use
    class: 'classes',   // CSS classes
    args: { }           // HTML attributes
});
```

## Parameters

| Parameter | Type   | Description                                                    |
| --------- | ------ | -------------------------------------------------------------- |
| `as`      | String | HTML tag name (e.g., `'div'`, `'section'`, `'article'`, `'a'`) |
| `class`   | String | CSS classes to apply to the element                            |
| `args`    | Object | HTML attributes as key-value pairs                             |

## Basic Examples

### Setting the Tag

```javascript
const transformHook = (rw) => {
    rw.setRootElement({
        as: 'section'
    });
};

exports.transformHook = transformHook;
```

Output: `<section>...</section>`

### Adding Classes

```javascript
const transformHook = (rw) => {
    const { padding, margin } = rw.props;
    
    rw.setRootElement({
        as: 'div',
        class: `container ${padding} ${margin}`
    });
};

exports.transformHook = transformHook;
```

Output: `<div class="container p-4 m-2">...</div>`

### Adding Attributes

```javascript
const transformHook = (rw) => {
    const { customId } = rw.props;
    
    rw.setRootElement({
        as: 'div',
        class: 'my-component',
        args: {
            id: customId,
            'data-theme': 'dark',
            'aria-label': 'Main content'
        }
    });
};

exports.transformHook = transformHook;
```

Output: `<div class="my-component" id="hero" data-theme="dark" aria-label="Main content">...</div>`

## Common Patterns

### Dynamic Tag Based on Link

```javascript
const transformHook = (rw) => {
    const { link } = rw.props;
    const hasLink = link && link.href && link.href.length > 0;
    
    rw.setRootElement({
        as: hasLink ? 'a' : 'div',
        class: 'card',
        args: hasLink ? {
            href: link.href,
            title: link.title,
            target: link.target
        } : {}
    });
    
    rw.setProps({ hasLink });
};

exports.transformHook = transformHook;
```

### Custom HTML Tag from Properties

```javascript
const transformHook = (rw) => {
    const { htmlTag, customTag } = rw.props;
    
    // Allow user to select tag or enter custom
    const tag = htmlTag === 'custom' 
        ? customTag.replace(/[^a-zA-Z0-9]/g, '') 
        : htmlTag;
    
    rw.setRootElement({
        as: tag || 'div'
    });
};

exports.transformHook = transformHook;
```

### Container with Full Styling

```javascript
const transformHook = (rw) => {
    const { 
        customId, 
        padding, 
        background, 
        shadow, 
        rounded 
    } = rw.props;
    const { id } = rw.node;
    
    const classes = [
        `group/${id}`,
        padding,
        background,
        shadow,
        rounded
    ].filter(Boolean).join(' ');
    
    rw.setRootElement({
        as: 'section',
        class: classes,
        args: {
            id: customId || undefined,
            'data-component': 'container'
        }
    });
    
    // Register anchor if custom ID is set
    if (customId) {
        rw.addAnchor(customId);
    }
};

exports.transformHook = transformHook;
```

### Alpine.js Integration

```javascript
const transformHook = (rw) => {
    const { id } = rw.node;
    
    rw.setRootElement({
        as: 'div',
        class: 'gallery',
        args: {
            'x-data': `gallery('${id}')`,
            'x-init': 'init()'
        }
    });
};

exports.transformHook = transformHook;
```

### Accordion/Disclosure Pattern

```javascript
const transformHook = (rw) => {
    const { id } = rw.node;
    const { openOnLoad } = rw.props;
    
    rw.setRootElement({
        as: 'div',
        class: `group/${id}`,
        args: {
            'x-data': `accordion('${id}', { openOnLoad: ${openOnLoad} })`,
            'x-bind': 'details',
            'data-open': 'false',
            'role': 'group',
            'aria-roledescription': 'accordion'
        }
    });
};

exports.transformHook = transformHook;
```

## Notes

* The `args` object is spread directly as HTML attributes
* Use `undefined` values to conditionally omit attributes
* Classes are concatenated into a single `class` attribute
* Works seamlessly with Tailwind CSS utility classes


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.realmacsoftware.com/elements-docs/elements-language/component/hooks.js/available-functions/rw.setrootelement.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
