-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
SlotFill: remove explicit rerender from portal version #67471
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import { useObservableValue } from '@wordpress/compose'; | ||
import { | ||
useContext, | ||
useReducer, | ||
useRef, | ||
useEffect, | ||
createPortal, | ||
|
@@ -20,18 +19,15 @@ import type { FillComponentProps } from '../types'; | |
export default function Fill( { name, children }: FillComponentProps ) { | ||
const registry = useContext( SlotFillContext ); | ||
const slot = useObservableValue( registry.slots, name ); | ||
const [ , rerender ] = useReducer( () => [], [] ); | ||
const ref = useRef( { rerender } ); | ||
const instanceRef = useRef( {} ); | ||
|
||
// We register fills so we can keep track of their existence. | ||
// Slots can use the `useSlotFills` hook to know if there're already fills | ||
// registered so they can choose to render themselves or not. | ||
useEffect( () => { | ||
// We register fills so we can keep track of their existence. | ||
// Some Slot implementations need to know if there're already fills | ||
// registered so they can choose to render themselves or not. | ||
const refValue = ref.current; | ||
registry.registerFill( name, refValue ); | ||
return () => { | ||
registry.unregisterFill( name, refValue ); | ||
}; | ||
const instance = instanceRef.current; | ||
registry.registerFill( name, instance ); | ||
return () => registry.unregisterFill( name, instance ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This effect stays the same except renaming the |
||
}, [ registry, name ] ); | ||
|
||
if ( ! slot || ! slot.ref.current ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,7 @@ function createSlotRegistry(): SlotFillBubblesVirtuallyContext { | |
ref, | ||
fillProps | ||
) => { | ||
const slot = slots.get( name ); | ||
|
||
slots.set( name, { | ||
...slot, | ||
ref: ref || slot?.ref, | ||
fillProps: fillProps || slot?.fillProps || {}, | ||
} ); | ||
slots.set( name, { ref, fillProps } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A drive-by change removing redundant code. All the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense 👍 |
||
}; | ||
|
||
const unregisterSlot: SlotFillBubblesVirtuallyContext[ 'unregisterSlot' ] = | ||
|
@@ -66,12 +60,7 @@ function createSlotRegistry(): SlotFillBubblesVirtuallyContext { | |
return; | ||
} | ||
|
||
slot.fillProps = fillProps; | ||
const slotFills = fills.get( name ); | ||
if ( slotFills ) { | ||
// Force update fills. | ||
slotFills.forEach( ( fill ) => fill.rerender() ); | ||
} | ||
slots.set( name, { ref, fillProps } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the key change in this PR! Instead of silently updating a field with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much clearer and more concise, looks great 🚀 |
||
}; | ||
|
||
const registerFill: SlotFillBubblesVirtuallyContext[ 'registerFill' ] = ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,8 +39,7 @@ function Slot( | |
...restProps | ||
} = props; | ||
|
||
const { registerSlot, unregisterSlot, ...registry } = | ||
useContext( SlotFillContext ); | ||
const registry = useContext( SlotFillContext ); | ||
|
||
const ref = useRef< HTMLElement >( null ); | ||
|
||
|
@@ -54,11 +53,9 @@ function Slot( | |
}, [ fillProps ] ); | ||
|
||
useLayoutEffect( () => { | ||
registerSlot( name, ref, fillPropsRef.current ); | ||
return () => { | ||
unregisterSlot( name, ref ); | ||
}; | ||
}, [ registerSlot, unregisterSlot, name ] ); | ||
registry.registerSlot( name, ref, fillPropsRef.current ); | ||
return () => registry.unregisterSlot( name, ref ); | ||
}, [ registry, name ] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A mere style change: don't destructure fields of |
||
|
||
useLayoutEffect( () => { | ||
registry.updateSlot( name, ref, fillPropsRef.current ); | ||
|
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.
The
rerender
API goes away and the fill now registers only an unique instance object. I'm renaming it fromref
toinstance
when registering, something that @tyxla asked for in one of the previous PRs.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.
Thanks!