---
title: Polymorphism
description: Render components as alternative HTML elements or custom SolidJS
  components with type safety through the as prop.
package: moraine
version: 0.11.0
repository: https://github.com/subframe7536/moraine
---

# Polymorphism

> Render components as alternative HTML elements or custom SolidJS components with type safety through the as prop.

Moraine components and parts that expose `as` can render a different element or Solid component while keeping their styles and typed props. State-only roots and native form controls such as `Input` do not expose `as`. When you change the element, give the result an accessible name and the semantics its role needs.

## Rendering as an HTML Element (`as="tag"`)

By default, components render a semantic element, such as `<button>` for `Button` and `<div>` for `Card`. Use `as="tag"` to change the element:

```tsx
import { Button } from 'moraine'

export function NavigationLink() {
  return (
    // Renders an <a> element with Button styles and anchor attributes:
    <Button as="a" href="/docs/getting-started" target="_blank">
      Documentation
    </Button>
  )
}
```

### TypeScript Attribute Narrowing

With `as="tag"`, Moraine narrows the allowed props, event types, and `ref` callback parameter to that HTML tag:

```tsx
import { Button } from 'moraine'

// Valid: 'href' and 'target' exist on <a>
<Button as="a" href="/dashboard" target="_blank">Dashboard</Button>

// Type error: 'href' is not a <button> attribute
<Button as="button" href="/dashboard">Dashboard</Button>
```

## Rendering as a Solid Component (`as={Component}`)

Pass a SolidJS component to `as`. Moraine accepts the target component's props and checks them against its types, so autocompletion and type checking apply:

```tsx
import { Button, DropdownMenu } from 'moraine'

export function MenuExample() {
  return (
    <DropdownMenu>
      {/* Renders a Button and type-checks its 'variant' and 'size' props */}
      <DropdownMenu.Trigger as={Button} variant="ghost" size="icon-xs">
        Menu
      </DropdownMenu.Trigger>
      <DropdownMenu.Content items={[{ label: 'Profile' }, { label: 'Settings' }]} />
    </DropdownMenu>
  )
}
```

Custom components receive ordinary event props and can wrap, conditionally forward, or invoke them. Spread the received props onto one root element and forward its `ref`, so Moraine can resolve native element semantics and manage focus. `Dialog`, `Sheet`, `Drawer`, `BaseDialog`, `DropdownMenu`, and `ContextMenu` triggers throw on mount when no element resolves from the component.

## Overlay Triggers and Composition

Overlay triggers (`Dialog.Trigger`, `Sheet.Trigger`, `Drawer.Trigger`, `Popover.Trigger`, `Tooltip.Trigger`, `DropdownMenu.Trigger`) are designed to be composed with `as={Button}`:

1. **State Injection**: The trigger injects ARIA state (`aria-haspopup`, `aria-expanded`, and `aria-controls` while the content is present) and the click, pointer, and keyboard handlers it needs into the rendered element. `Tooltip.Trigger` sets `aria-describedby` instead.
2. **Event Merging**: If you provide your own `onClick` handler on `<Dialog.Trigger as={Button} onClick={...}>`, Moraine calls your handler first. If you call `event.preventDefault()`, the disclosure action is canceled.
3. **Ref Forwarding**: The `ref` you pass receives the rendered DOM element, so you can manage focus.

`BaseDialog`, `Dialog`, `Sheet`, `Drawer`, `Popover`, `Tooltip`, `HoverCard`, and `DropdownMenu` triggers handle clicks on a custom component after its own click handler runs. These custom targets do not receive the composed `onClick` prop; the target's own DOM click handler can call `preventDefault()` to cancel activation. Other polymorphic roots, including `BaseSelect` and `ContextMenu` triggers, forward `onClick` normally. Delegated events, including keyboard events, also reach custom targets rendered in an iframe document.

```tsx
import { Button, Dialog } from 'moraine'

export function DeleteConfirmation() {
  let triggerRef: HTMLButtonElement | undefined

  return (
    <Dialog>
      <Dialog.Trigger
        as={Button}
        variant="solid"
        tone="danger"
        ref={(el) => (triggerRef = el)}
        onClick={() => console.log('Opening delete modal')}
      >
        Delete Account
      </Dialog.Trigger>
      <Dialog.Content title="Are you sure?">
        <Dialog.Body>This action cannot be undone.</Dialog.Body>
      </Dialog.Content>
    </Dialog>
  )
}
```

## Global Type Configuration

To restrict `as` autocompletion to standard HTML element tags (excluding SVG and MathML elements), enable `simpleHtmlTags` through module augmentation. To relax the tag-specific attribute narrowing, enable `simpleRootAttributes`. See the [TypeScript guide](https://moraine.subf.dev/docs/typescript.md#global-type-configuration-moraine-type-config) for details.

