-
Hi @fabien-ml, I'm exploring the new polymorphism API and am struggling to figure out how to make it work. Consider the following import { Button as ButtonBase } from '@kobalte/core';
import type { OverrideComponentProps } from '@kobalte/utils';
import { Show, splitProps } from 'solid-js';
import type { IconProps } from '../Icon/index.js';
import { Icon } from '../Icon/index.js';
export interface ButtonOpts extends ButtonBase.ButtonRootOptions {
/** If added, the button will show an icon before the button's content. */
leftIcon?: IconProps['icon'];
}
export type ButtonProps = OverrideComponentProps<'button', ButtonOpts>;
export function Button(props: ButtonProps) {
const [local, others] = splitProps(props, ['leftIcon', 'children']);
return (
<ButtonBase.Root {...others}>
<Show when={local.leftIcon}>
<Icon icon={local.leftIcon!} />
</Show>
<Show when={local.children}>
<div class="px-2">{local.children}</div>
</Show>
</ButtonBase.Root>
);
} Before the new polymorphic API, I'm using this <Button as={Link} href="/" leftIcon="arrow-left">
Go Home
</Button> With the new polymorphic API, I've updated to this: <Button leftIcon="arrow-left" asChild>
<As component={Link} href="/">
Go Home
</As>
</Button> However, I immediately run into the "[kobalte]: Component is expected to render Building on the base Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi, the downside of the In your case with tailwind you can try to do: <ButtonBase.Root class="space-x-2" {...others}>
<Show when={local.leftIcon}>
<Icon icon={local.leftIcon!} />
</Show>
{local.children}
</ButtonBase.Root> Or use the But yeah now with the new polymorphic API you can't conditionnaly show |
Beta Was this translation helpful? Give feedback.
With the current API for Hope UI i've decided that almost all components will not be polymorphic.
For the case like yours i've created a
LinkButton
component which render an<a>
by default and looks like a button.In my API this one is polymorphic because we need to be able to replace
<a>
by solidjs router link and i was able to do it because to content is simpler thanButton
.