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:
TypeScript Attribute Narrowing#
With as="tag", Moraine narrows the allowed props, event types, and ref callback parameter to that HTML tag:
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:
When writing a custom component target for as={Component}:
- Spread the received props onto a single root DOM element and forward its
refso Moraine can resolve native element semantics and manage focus. Dialog,Sheet,Drawer,BaseDialog,DropdownMenu, andContextMenutriggers throw on mount if no DOM element resolves from the custom component.
Overlay Triggers and Composition#
Overlay triggers (Dialog.Trigger, Sheet.Trigger, Drawer.Trigger, Popover.Trigger, Tooltip.Trigger, DropdownMenu.Trigger) are designed to compose with as={Button}:
- State Injection: The trigger injects ARIA state (
aria-haspopup,aria-expanded, andaria-controlswhile the content is present) and the click, pointer, and keyboard handlers it needs into the rendered element.Tooltip.Triggersetsaria-describedbyinstead. - Event Merging: If you provide your own
onClickhandler on<Dialog.Trigger as={Button} onClick={...}>, Moraine calls your handler first. Callingevent.preventDefault()cancels the disclosure action. - Ref Forwarding: The
refyou pass receives the rendered DOM element for focus management.
BaseDialog, Dialog, Sheet, Drawer, Popover, Tooltip, HoverCard, and DropdownMenu triggers attach click activation directly to the resolved DOM element after the custom component’s own click handler runs (and cancel activation if preventDefault() was called), including inside an <iframe> document. Other polymorphic triggers (BaseSelect.Trigger and ContextMenu.Trigger) forward onClick through props.
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 for details.