-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: rewrite base Slot to functional, unify rerenderable refs #67153
Changes from all commits
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,8 +4,8 @@ | |
import { useObservableValue } from '@wordpress/compose'; | ||
import { | ||
useContext, | ||
useReducer, | ||
useRef, | ||
useState, | ||
useEffect, | ||
createPortal, | ||
} from '@wordpress/element'; | ||
|
@@ -17,37 +17,20 @@ import SlotFillContext from './slot-fill-context'; | |
import StyleProvider from '../../style-provider'; | ||
import type { FillComponentProps } from '../types'; | ||
|
||
function useForceUpdate() { | ||
const [ , setState ] = useState( {} ); | ||
const mountedRef = useRef( true ); | ||
|
||
useEffect( () => { | ||
mountedRef.current = true; | ||
return () => { | ||
mountedRef.current = false; | ||
}; | ||
}, [] ); | ||
|
||
return () => { | ||
if ( mountedRef.current ) { | ||
setState( {} ); | ||
} | ||
}; | ||
} | ||
|
||
export default function Fill( { name, children }: FillComponentProps ) { | ||
const registry = useContext( SlotFillContext ); | ||
const slot = useObservableValue( registry.slots, name ); | ||
const rerender = useForceUpdate(); | ||
const [ , rerender ] = useReducer( () => [], [] ); | ||
const ref = useRef( { rerender } ); | ||
|
||
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. | ||
registry.registerFill( name, ref ); | ||
const refValue = ref.current; | ||
registry.registerFill( name, refValue ); | ||
return () => { | ||
registry.unregisterFill( name, ref ); | ||
registry.unregisterFill( name, refValue ); | ||
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. Instead of registering the |
||
}; | ||
}, [ registry, name ] ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ function createSlotRegistry(): SlotFillBubblesVirtuallyContext { | |
const slotFills = fills.get( name ); | ||
if ( slotFills ) { | ||
// Force update fills. | ||
slotFills.forEach( ( fill ) => fill.current.rerender() ); | ||
slotFills.forEach( ( fill ) => fill.rerender() ); | ||
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 a benefit of registering the |
||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ export default function Fill( { name, children }: FillComponentProps ) { | |
useLayoutEffect( () => { | ||
ref.current.children = children; | ||
if ( slot ) { | ||
slot.forceUpdate(); | ||
slot.rerender(); | ||
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.
|
||
} | ||
}, [ slot, children ] ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import type { Component } from '@wordpress/element'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
|
@@ -11,20 +10,17 @@ import SlotFillContext from './context'; | |
import type { | ||
FillComponentProps, | ||
BaseSlotFillContext, | ||
BaseSlotComponentProps, | ||
SlotFillProviderProps, | ||
SlotKey, | ||
Rerenderable, | ||
} from './types'; | ||
|
||
function createSlotRegistry(): BaseSlotFillContext { | ||
const slots: Record< SlotKey, Component< BaseSlotComponentProps > > = {}; | ||
const slots: Record< SlotKey, Rerenderable > = {}; | ||
const fills: Record< SlotKey, FillComponentProps[] > = {}; | ||
let listeners: Array< () => void > = []; | ||
|
||
function registerSlot( | ||
name: SlotKey, | ||
slot: Component< BaseSlotComponentProps > | ||
) { | ||
function registerSlot( name: SlotKey, slot: Rerenderable ) { | ||
const previousSlot = slots[ name ]; | ||
slots[ name ] = slot; | ||
triggerListeners(); | ||
|
@@ -38,7 +34,7 @@ function createSlotRegistry(): BaseSlotFillContext { | |
// assigned into the instance, such that its own rendering of children | ||
// will be empty (the new Slot will subsume all fills for this name). | ||
if ( previousSlot ) { | ||
previousSlot.forceUpdate(); | ||
previousSlot.rerender(); | ||
} | ||
} | ||
|
||
|
@@ -47,10 +43,7 @@ function createSlotRegistry(): BaseSlotFillContext { | |
forceUpdateSlot( name ); | ||
} | ||
|
||
function unregisterSlot( | ||
name: SlotKey, | ||
instance: Component< BaseSlotComponentProps > | ||
) { | ||
function unregisterSlot( name: SlotKey, instance: Rerenderable ) { | ||
// If a previous instance of a Slot by this name unmounts, do nothing, | ||
// as the slot and its fills should only be removed for the current | ||
// known instance. | ||
|
@@ -68,15 +61,13 @@ function createSlotRegistry(): BaseSlotFillContext { | |
forceUpdateSlot( name ); | ||
} | ||
|
||
function getSlot( | ||
name: SlotKey | ||
): Component< BaseSlotComponentProps > | undefined { | ||
function getSlot( name: SlotKey ): Rerenderable | undefined { | ||
return slots[ name ]; | ||
} | ||
|
||
function getFills( | ||
name: SlotKey, | ||
slotInstance: Component< BaseSlotComponentProps > | ||
slotInstance: Rerenderable | ||
): FillComponentProps[] { | ||
// Fills should only be returned for the current instance of the slot | ||
// in which they occupy. | ||
|
@@ -90,7 +81,7 @@ function createSlotRegistry(): BaseSlotFillContext { | |
const slot = getSlot( name ); | ||
|
||
if ( slot ) { | ||
slot.forceUpdate(); | ||
slot.rerender(); | ||
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. Should we also rename the internal |
||
} | ||
} | ||
|
||
|
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.
This is extracted to a separate module because the
useForceUpdate
hook is used by two components now. Also with React 18 we no longer needmountedRef
because callingsetState
after unmount is no longer a problem.