Skip to content
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

Fix slots infinite rendering when no context prop is provided #2219

Merged
merged 5 commits into from
Aug 11, 2022
Merged
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/fix-slots-infinite-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Fix slots infinite rendering when no `context` prop is provided
12 changes: 6 additions & 6 deletions src/__tests__/utils/createSlots.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import createSlots from '../../utils/create-slots'

// setup a component with slots
const {Slots, Slot} = createSlots(['One', 'Two', 'Three'])
type ContextTypes = {salutation?: string}
type Props = {context?: {salutation: string}}

const ComponentWithSlots: React.FC<React.PropsWithChildren<ContextTypes>> = ({salutation, children}) => {
const ComponentWithSlots: React.FC<React.PropsWithChildren<Props>> = ({context, children}) => {
return (
<Slots context={{salutation}}>
<Slots context={context}>
{slots => (
<div>
{slots.One}
Expand All @@ -25,9 +25,9 @@ const SlotItem1: React.FC<React.PropsWithChildren<unknown>> = ({children}) => <S
const SlotItem2: React.FC<React.PropsWithChildren<unknown>> = ({children}) => <Slot name="Two">{children}</Slot>
const SlotItem3: React.FC<React.PropsWithChildren<unknown>> = ({children}) => (
<Slot name="Three">
{(context: ContextTypes) => (
{(context: Props['context']) => (
<>
{context.salutation} {children}
{context?.salutation} {children}
</>
)}
</Slot>
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('ComponentWithSlots', () => {

it('renders with context passed to children', async () => {
const component = render(
<ComponentWithSlots salutation="hi">
<ComponentWithSlots context={{salutation: 'hi'}}>
<SlotItem3>third</SlotItem3>
free form
</ComponentWithSlots>
Expand Down
5 changes: 4 additions & 1 deletion src/utils/create-slots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const createSlots = <SlotNames extends string>(slotNames: SlotNames[]) => {
context: {}
})

// maintain a static reference to avoid infinite render loop
const defaultContext = Object.freeze({})

/** Slots uses a Double render strategy inspired by [reach-ui/descendants](https://github.com/reach/reach-ui/tree/develop/packages/descendants)
* Slot registers themself with the Slots parent.
* When all the children have mounted = registered themselves in slot,
Expand All @@ -33,7 +36,7 @@ const createSlots = <SlotNames extends string>(slotNames: SlotNames[]) => {
context?: ContextProps['context']
children: (slots: Slots) => React.ReactNode
}>
> = ({context = {}, children}) => {
> = ({context = defaultContext, children}) => {
// initialise slots
const slotsDefinition: Slots = {}
slotNames.map(name => (slotsDefinition[name] = null))
Expand Down