Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-pets-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@radix-ui/react-slot': patch
---

Support slotting onto nested children
99 changes: 99 additions & 0 deletions apps/ssr-testing/app/slot/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use client';

import * as React from 'react';
import { Slot } from 'radix-ui';

export const Link = React.forwardRef<
React.ComponentRef<'a'>,
React.ComponentProps<'a'> & { asChild?: boolean }
>(({ asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'a';
return <Comp {...props} ref={forwardedRef} />;
});

export const LinkSlottable = React.forwardRef<
React.ComponentRef<'a'>,
React.ComponentProps<'a'> & { asChild?: boolean }
>(({ asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'a';
return (
<Comp {...props} ref={forwardedRef}>
<span>left</span>
<Slot.Slottable>{props.children}</Slot.Slottable>
<span>right</span>
</Comp>
);
});

export const LinkButton = React.forwardRef<
React.ComponentRef<typeof Link>,
React.ComponentProps<typeof Link>
>((props, forwardedRef) => (
<Button asChild>
<Link {...props} ref={forwardedRef}>
{props.children}
</Link>
</Button>
));

export const Button = React.forwardRef<
React.ComponentRef<'button'>,
React.ComponentProps<'button'> & { asChild?: boolean }
>(({ asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'button';
return <Comp {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }} />;
});

export const ButtonSlottable = React.forwardRef<
React.ComponentRef<'button'>,
React.ComponentProps<'button'> & { asChild?: boolean }
>(({ children, asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'button';
return (
<Comp {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }}>
<span>left</span>
<Slot.Slottable>{children}</Slot.Slottable>
<span>right</span>
</Comp>
);
});

export const ButtonNestedSlottable = React.forwardRef<
React.ComponentRef<typeof Button>,
React.ComponentProps<typeof Button>
>(({ children, asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'button';
return (
<Comp {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }}>
<Slot.Slottable child={children}>
{(slottable) => (
<>
<span>left</span>
<b>bold {slottable}</b>
<span>right</span>
</>
)}
</Slot.Slottable>
</Comp>
);
});

export const IconButtonNestedSlottable = React.forwardRef<
React.ComponentRef<typeof Button>,
React.ComponentProps<typeof Button>
>(({ children, ...props }, forwardedRef) => {
return (
<Button {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }}>
<Slot.Root>
<Slot.Slottable child={children}>
{(slottable) => (
<>
<span>ICON</span>
<b>bold {slottable}</b>
</>
)}
</Slot.Slottable>
</Slot.Root>
</Button>
);
});
142 changes: 135 additions & 7 deletions apps/ssr-testing/app/slot/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,141 @@
import * as React from 'react';
import { Slot } from 'radix-ui';
import * as Client from './client';
import * as Server from './server';

export default function Page() {
return (
<Slot.Root>
<span>I'm in a </span>
<Slot.Slottable>
<em>Slot!?</em>
</Slot.Slottable>
</Slot.Root>
<>
<p>All components should be rendered as links</p>

<h2>Client.LinkButton</h2>

<Client.LinkButton href="/">children</Client.LinkButton>

<h2>Client.Button as Client.Link</h2>

<Client.Button asChild>
<Client.Link href="/">children</Client.Link>
</Client.Button>

<h2>Client.Button as Server.Link</h2>

<Client.Button asChild>
<Server.Link href="/">children</Server.Link>
</Client.Button>

<h2>Client.Button as Client.LinkSlottable</h2>

<Client.Button asChild>
<Client.LinkSlottable href="/">children</Client.LinkSlottable>
</Client.Button>

<h2>Client.Button as Server.LinkSlottable</h2>

<Client.Button asChild>
<Server.LinkSlottable href="/">children</Server.LinkSlottable>
</Client.Button>

<h2>Client.ButtonSlottable as Server.Link</h2>

<Client.ButtonSlottable asChild>
<Server.Link href="/">children</Server.Link>
</Client.ButtonSlottable>

<h2>Client.ButtonSlottable as Client.Link</h2>

<Client.ButtonSlottable asChild>
<Client.Link href="/">children</Client.Link>
</Client.ButtonSlottable>

<h2>Client.ButtonNestedSlottable as Server.Link</h2>

<Client.ButtonNestedSlottable asChild>
<Server.Link href="/">children</Server.Link>
</Client.ButtonNestedSlottable>

<h2>Client.ButtonNestedSlottable as Client.Link</h2>

<Client.ButtonNestedSlottable asChild>
<Client.Link href="/">children</Client.Link>
</Client.ButtonNestedSlottable>

<h2>Client.IconButtonNestedSlottable as Server.Link</h2>

<Client.IconButtonNestedSlottable asChild>
<Server.Link href="/">children</Server.Link>
</Client.IconButtonNestedSlottable>

<h2>Client.IconButtonNestedSlottable as Client.Link</h2>

<Client.IconButtonNestedSlottable asChild>
<Client.Link href="/">children</Client.Link>
</Client.IconButtonNestedSlottable>

<hr />

<h2>Server.LinkButton</h2>

<Server.LinkButton href="/">children</Server.LinkButton>

<h2>Server.Button as Server.Link</h2>

<Server.Button asChild>
<Server.Link href="/">children</Server.Link>
</Server.Button>

<h2>Server.Button as Client.Link</h2>

<Server.Button asChild>
<Client.Link href="/">children</Client.Link>
</Server.Button>

<h2>Server.Button as Server.LinkSlottable</h2>

<Server.Button asChild>
<Server.LinkSlottable href="/">children</Server.LinkSlottable>
</Server.Button>

<h2>Server.Button as Client.LinkSlottable</h2>

<Server.Button asChild>
<Client.LinkSlottable href="/">children</Client.LinkSlottable>
</Server.Button>

<h2>Server.ButtonSlottable as Client.Link</h2>

<Server.ButtonSlottable asChild>
<Client.Link href="/">children</Client.Link>
</Server.ButtonSlottable>

<h2>Server.ButtonSlottable as Server.Link</h2>

<Server.ButtonSlottable asChild>
<Server.Link href="/">children</Server.Link>
</Server.ButtonSlottable>

<h2>Server.ButtonNestedSlottable as Client.Link</h2>

<Server.ButtonNestedSlottable asChild>
<Client.Link href="/">children</Client.Link>
</Server.ButtonNestedSlottable>

<h2>Server.ButtonNestedSlottable as Server.Link</h2>

<Server.ButtonNestedSlottable asChild>
<Server.Link href="/">children</Server.Link>
</Server.ButtonNestedSlottable>

<h2>Server.IconButtonNestedSlottable as Server.Link</h2>

<Server.IconButtonNestedSlottable asChild>
<Server.Link href="/">children</Server.Link>
</Server.IconButtonNestedSlottable>

<h2>Server.IconButtonNestedSlottable as Client.Link</h2>

<Server.IconButtonNestedSlottable asChild>
<Client.Link href="/">children</Client.Link>
</Server.IconButtonNestedSlottable>
</>
);
}
98 changes: 98 additions & 0 deletions apps/ssr-testing/app/slot/server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as React from 'react';
import { Slot } from 'radix-ui';
import * as Client from './client';

export const Link = React.forwardRef<
React.ComponentRef<'a'>,
React.ComponentProps<'a'> & { asChild?: boolean }
>(({ asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'a';
return <Comp {...props} ref={forwardedRef} />;
});

export const LinkSlottable = React.forwardRef<
React.ComponentRef<'a'>,
React.ComponentProps<'a'> & { asChild?: boolean }
>(({ asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'a';
return (
<Comp {...props} ref={forwardedRef}>
<span>left</span>
<Slot.Slottable>{props.children}</Slot.Slottable>
<span>right</span>
</Comp>
);
});

export const LinkButton = React.forwardRef<
React.ComponentRef<typeof Link>,
React.ComponentProps<typeof Link>
>((props, forwardedRef) => (
<Button asChild>
<Link {...props} ref={forwardedRef}>
{props.children}
</Link>
</Button>
));

export const Button = React.forwardRef<
React.ComponentRef<'button'>,
React.ComponentProps<'button'> & { asChild?: boolean }
>(({ asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'button';
return <Comp {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }} />;
});

export const ButtonSlottable = React.forwardRef<
React.ComponentRef<'button'>,
React.ComponentProps<'button'> & { asChild?: boolean }
>(({ children, asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'button';
return (
<Comp {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }}>
<span>left</span>
<Slot.Slottable>{children}</Slot.Slottable>
<span>right</span>
</Comp>
);
});

export const ButtonNestedSlottable = React.forwardRef<
React.ComponentRef<typeof Button>,
React.ComponentProps<typeof Button>
>(({ children, asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot.Root : 'button';
return (
<Comp {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }}>
<Slot.Slottable child={children}>
{(slottable) => (
<>
<span>left</span>
<b>bold {slottable}</b>
<span>right</span>
</>
)}
</Slot.Slottable>
</Comp>
);
});

export const IconButtonNestedSlottable = React.forwardRef<
React.ComponentRef<typeof Button>,
React.ComponentProps<typeof Button>
>(({ children, ...props }, forwardedRef) => {
return (
<Client.Button {...props} ref={forwardedRef} style={{ display: 'flex', gap: '3rem' }}>
<Slot.Root>
<Slot.Slottable child={children}>
Comment on lines +85 to +87
Copy link
Contributor Author

@jjenzz jjenzz Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this example because it's important that Slot.Root and its desired Slot.Slottable are on the same client or server boundary. This is perhaps something worth documenting. It isn't specific to nested slottables—it's important if we want to change the element that an existing component slots onto.

{(slottable) => (
<>
<span>ICON</span>
<b>bold {slottable}</b>
</>
)}
</Slot.Slottable>
</Slot.Root>
</Client.Button>
);
});
Loading