Skip to content

Commit

Permalink
SlotFill: fix dependencies of registration effects, deduplicate code (#…
Browse files Browse the repository at this point in the history
…67071)

* SlotFill: fix dependencies of registration effects, deduplicate code

* Add changelog entry

Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
4 people authored Nov 18, 2024
1 parent c22ecef commit f6e6e45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

- `SlotFill`: Remove registration API methods from return value of `__experimentalUseSlot` ([#67070](https://github.com/WordPress/gutenberg/pull/67070)).

### Internal

- `SlotFill`: fix dependencies of `Fill` registration effects ([#67071](https://github.com/WordPress/gutenberg/pull/67071)).

## 28.12.0 (2024-11-16)

### Deprecations
Expand Down
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;
}

1 comment on commit f6e6e45

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in f6e6e45.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11894499501
📝 Reported issues:

Please sign in to comment.