Skip to content

Commit

Permalink
SlotFill: fix dependencies of registration effects, deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Nov 18, 2024
1 parent 92199e1 commit 23864f6
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions packages/components/src/slot-fill/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useSlot from './use-slot';
import type { FillComponentProps } from './types';

export default function Fill( { name, children }: FillComponentProps ) {
const { registerFill, unregisterFill } = useContext( SlotFillContext );
const registry = useContext( SlotFillContext );
const slot = useSlot( name );

const ref = useRef( {
Expand All @@ -21,32 +21,17 @@ export default function Fill( { name, children }: FillComponentProps ) {

useLayoutEffect( () => {
const refValue = ref.current;
registerFill( name, refValue );
return () => unregisterFill( name, refValue );
// The useLayoutEffects here are written to fire at specific times, and introducing new dependencies could cause unexpected changes in behavior.
// We'll leave them as-is until a more detailed investigation/refactor can be performed.
}, [] );
refValue.name = name;
registry.registerFill( name, refValue );
return () => registry.unregisterFill( name, refValue );
}, [ registry, name ] );

useLayoutEffect( () => {
ref.current.children = children;
if ( slot ) {
slot.forceUpdate();
}
// The useLayoutEffects here are written to fire at specific times, and introducing new dependencies could cause unexpected changes in behavior.
// We'll leave them as-is until a more detailed investigation/refactor can be performed.
}, [ children ] );

useLayoutEffect( () => {
if ( name === ref.current.name ) {
// Ignore initial effect.
return;
}
unregisterFill( ref.current.name, ref.current );
ref.current.name = name;
registerFill( name, ref.current );
// The useLayoutEffects here are written to fire at specific times, and introducing new dependencies could cause unexpected changes in behavior.
// We'll leave them as-is until a more detailed investigation/refactor can be performed.
}, [ name ] );
}, [ slot, children ] );

return null;
}

0 comments on commit 23864f6

Please sign in to comment.