-
Notifications
You must be signed in to change notification settings - Fork 1k
[Slot] support slotting onto nested children #3695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjenzz
wants to merge
1
commit into
radix-ui:main
Choose a base branch
from
jjenzz:slot-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+567
−91
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@radix-ui/react-slot': patch | ||
--- | ||
|
||
Support slotting onto nested children |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}> | ||
{(slottable) => ( | ||
<> | ||
<span>ICON</span> | ||
<b>bold {slottable}</b> | ||
</> | ||
)} | ||
</Slot.Slottable> | ||
</Slot.Root> | ||
</Client.Button> | ||
); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 desiredSlot.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.