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

SlotFill: remove registration API from useSlot result #67070

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 16 additions & 9 deletions packages/components/src/slot-fill/bubbles-virtually/fill.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/**
* WordPress dependencies
*/
import { useRef, useState, useEffect, createPortal } from '@wordpress/element';
import { useObservableValue } from '@wordpress/compose';
import {
useContext,
useRef,
useState,
useEffect,
createPortal,
} from '@wordpress/element';

/**
* Internal dependencies
*/
import useSlot from './use-slot';
import SlotFillContext from './slot-fill-context';
import StyleProvider from '../../style-provider';
import type { FillComponentProps } from '../types';

Expand All @@ -28,23 +35,23 @@ function useForceUpdate() {
};
}

export default function Fill( props: FillComponentProps ) {
const { name, children } = props;
const { registerFill, unregisterFill, ...slot } = useSlot( name );
export default function Fill( { name, children }: FillComponentProps ) {
const registry = useContext( SlotFillContext );
const slot = useObservableValue( registry.slots, name );
const rerender = useForceUpdate();
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.
registerFill( ref );
registry.registerFill( name, ref );
return () => {
unregisterFill( ref );
registry.unregisterFill( name, ref );
};
}, [ registerFill, unregisterFill ] );
}, [ registry, name ] );

if ( ! slot.ref || ! slot.ref.current ) {
if ( ! slot || ! slot.ref.current ) {
return null;
}

Expand Down
31 changes: 3 additions & 28 deletions packages/components/src/slot-fill/bubbles-virtually/use-slot.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
/**
* WordPress dependencies
*/
import { useMemo, useContext } from '@wordpress/element';
import { useContext } from '@wordpress/element';
import { useObservableValue } from '@wordpress/compose';

/**
* Internal dependencies
*/
import SlotFillContext from './slot-fill-context';
import type {
SlotFillBubblesVirtuallyFillRef,
SlotFillBubblesVirtuallySlotRef,
FillProps,
SlotKey,
} from '../types';
import type { SlotKey } from '../types';

export default function useSlot( name: SlotKey ) {
const registry = useContext( SlotFillContext );
const slot = useObservableValue( registry.slots, name );

const api = useMemo(
() => ( {
updateSlot: (
ref: SlotFillBubblesVirtuallySlotRef,
fillProps: FillProps
) => registry.updateSlot( name, ref, fillProps ),
unregisterSlot: ( ref: SlotFillBubblesVirtuallySlotRef ) =>
registry.unregisterSlot( name, ref ),
registerFill: ( ref: SlotFillBubblesVirtuallyFillRef ) =>
registry.registerFill( name, ref ),
unregisterFill: ( ref: SlotFillBubblesVirtuallyFillRef ) =>
registry.unregisterFill( name, ref ),
} ),
[ name, registry ]
);

return {
...slot,
...api,
};
return { ...slot };
Copy link
Member Author

Choose a reason for hiding this comment

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

Returning { ...slot } instead of slot has two purposes:

  1. The hook always returns an object, even if the slot is undefined. Important for compatibility.
  2. The hook has a nice return type: an object with optional ref and fillProps fields.

}
Loading