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:
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}:
- 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. If you callevent.preventDefault(), the disclosure action is canceled. - Ref Forwarding: The
refyou 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.
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.