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

Improve Slot/Fill performance #44642

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 33 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/block-editor/src/components/block-controls/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useContext } from '@wordpress/element';
import {
__experimentalToolbarContext as ToolbarContext,
ToolbarGroup,
__experimentalUseSlot as useSlot,
__experimentalUseSlotFills as useSlotFills,
} from '@wordpress/components';

/**
Expand All @@ -16,8 +16,8 @@ import groups from './groups';
export default function BlockControlsSlot( { group = 'default', ...props } ) {
const accessibleToolbarState = useContext( ToolbarContext );
const Slot = groups[ group ].Slot;
const slot = useSlot( Slot.__unstableName );
const hasFills = Boolean( slot.fills && slot.fills.length );
const fills = useSlotFills( Slot.__unstableName );
const hasFills = Boolean( fills && fills.length );

if ( ! hasFills ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@wordpress/blocks';
import {
PanelBody,
__experimentalUseSlot as useSlot,
__experimentalUseSlotFills as useSlotFills,
FlexItem,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
Expand Down Expand Up @@ -284,8 +284,8 @@ const BlockInspectorSingleBlock = ( { clientId, blockName } ) => {
};

const AdvancedControls = () => {
const slot = useSlot( InspectorAdvancedControls.slotName );
const hasFills = Boolean( slot.fills && slot.fills.length );
const fills = useSlotFills( InspectorAdvancedControls.slotName );
const hasFills = Boolean( fills && fills.length );

if ( ! hasFills ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* WordPress dependencies
*/
import { __experimentalUseSlot as useSlot } from '@wordpress/components';
import {
__experimentalUseSlot as useSlot,
__experimentalUseSlotFills as useSlotFills,
} from '@wordpress/components';
import warning from '@wordpress/warning';

/**
Expand All @@ -18,12 +21,13 @@ export default function InspectorControlsSlot( {
} ) {
const Slot = groups[ group ]?.Slot;
const slot = useSlot( Slot?.__unstableName );
const fills = useSlotFills( Slot?.__unstableName );
if ( ! Slot || ! slot ) {
warning( `Unknown InspectorControl group "${ group }" provided.` );
return null;
}

const hasFills = Boolean( slot.fills && slot.fills.length );
const hasFills = Boolean( fills && fills.length );
if ( ! hasFills ) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"reakit": "^1.3.8",
"remove-accents": "^0.4.2",
"use-lilius": "^2.0.1",
"uuid": "^8.3.0"
"uuid": "^8.3.0",
"valtio": "^1.7.0"
},
"peerDependencies": {
"react": "^17.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export {
Fill,
Provider as SlotFillProvider,
useSlot as __experimentalUseSlot,
useSlotFills as __experimentalUseSlotFills,
} from './slot-fill';
export { default as __experimentalStyleProvider } from './style-provider';
export { ZStack as __experimentalZStack } from './z-stack';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// @ts-nocheck
/**
* External dependencies
*/
import { proxyMap } from 'valtio/utils';
/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';
import warning from '@wordpress/warning';

const SlotFillContext = createContext( {
slots: {},
fills: {},
slots: proxyMap(),
fills: proxyMap(),
registerSlot: () => {
warning(
'Components must be wrapped within `SlotFillProvider`. ' +
Expand Down
Loading