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: Refactor slotfill base #51385

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Internal

- `SlotFill`: Refactor SlotFill base ([#51385](https://github.com/WordPress/gutenberg/pull/51385)).

## 25.1.0 (2023-06-07)

### Enhancements
Expand Down
17 changes: 0 additions & 17 deletions packages/components/src/slot-fill/context.js

This file was deleted.

21 changes: 21 additions & 0 deletions packages/components/src/slot-fill/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import type { BaseSlotFillContext } from './types';

const initialValue: BaseSlotFillContext = {
registerSlot: () => {},
unregisterSlot: () => {},
registerFill: () => {},
unregisterFill: () => {},
getSlot: () => undefined,
getFills: () => [],
subscribe: () => () => {},
};
export const SlotFillContext = createContext( initialValue );

export default SlotFillContext;
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { useContext, useLayoutEffect, useRef } from '@wordpress/element';
*/
import SlotFillContext from './context';
import useSlot from './use-slot';
import type { FillComponentProps } from './types';

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

Expand Down
119 changes: 0 additions & 119 deletions packages/components/src/slot-fill/provider.js

This file was deleted.

131 changes: 131 additions & 0 deletions packages/components/src/slot-fill/provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* WordPress dependencies
*/
import type { Component } from '@wordpress/element';

/**
* Internal dependencies
*/
import SlotFillContext from './context';
import type {
BaseFillObject,
BaseSlotFillContext,
SlotComponentProps,
} from './types';
import { useState } from '@wordpress/element';
/**
* External dependencies
*/
import type { ReactNode } from 'react';

export function createSlotRegistory(): BaseSlotFillContext {
const slots: Record< string, Component< SlotComponentProps > > = {};
const fills: Record< string, BaseFillObject[] > = {};
let listeners: Array< () => void > = [];

function registerSlot(
name: string,
slot: Component< SlotComponentProps >
) {
const previousSlot = slots[ name ];
slots[ name ] = slot;
triggerListeners();

// Sometimes the fills are registered after the initial render of slot
// But before the registerSlot call, we need to rerender the slot.
forceUpdateSlot( name );

// If a new instance of a slot is being mounted while another with the
// same name exists, force its update _after_ the new slot has been
// 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();
}
}

function registerFill( name: string, instance: BaseFillObject ) {
fills[ name ] = [ ...( fills[ name ] || [] ), instance ];
forceUpdateSlot( name );
}

function unregisterSlot(
name: string,
instance: Component< SlotComponentProps >
) {
// 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.
if ( slots[ name ] !== instance ) {
return;
}

delete slots[ name ];
triggerListeners();
}

function unregisterFill( name: string, instance: BaseFillObject ) {
fills[ name ] =
fills[ name ]?.filter( ( fill ) => fill !== instance ) ?? [];
forceUpdateSlot( name );
}

function getSlot(
name: string
): Component< SlotComponentProps > | undefined {
return slots[ name ];
}

function getFills(
name: string,
slotInstance: Component< SlotComponentProps >
): BaseFillObject[] {
// Fills should only be returned for the current instance of the slot
// in which they occupy.
if ( slots[ name ] !== slotInstance ) {
return [];
}
return fills[ name ];
}

function forceUpdateSlot( name: string ) {
const slot = getSlot( name );

if ( slot ) {
slot.forceUpdate();
}
}

function triggerListeners() {
listeners.forEach( ( listener ) => listener() );
}

function subscribe( listener: () => void ) {
listeners.push( listener );

return () => {
listeners = listeners.filter( ( l ) => l !== listener );
};
}

return {
registerSlot,
unregisterSlot,
registerFill,
unregisterFill,
getSlot,
getFills,
subscribe,
};
}

export function SlotFillProvider( { children }: { children: ReactNode } ) {
const [ contextValue ] = useState( createSlotRegistory );
return (
<SlotFillContext.Provider value={ contextValue }>
{ children }
</SlotFillContext.Provider>
);
}

export default SlotFillProvider;
Loading