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

Components: Refactor Slot and Fill away from Lodash #42153

Merged
merged 2 commits into from
Jul 5, 2022
Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- `TreeSelect`: Refactor away from `_.repeat()` ([#42070](https://github.com/WordPress/gutenberg/pull/42070/)).
- `FocalPointPicker` updated to satisfy `react/exhuastive-deps` eslint rule ([#41520](https://github.com/WordPress/gutenberg/pull/41520)).
- `ColorPicker` updated to satisfy `react/exhuastive-deps` eslint rule ([#41294](https://github.com/WordPress/gutenberg/pull/41294)).
- `Slot`/`Fill`: Refactor away from Lodash ([#42153](https://github.com/WordPress/gutenberg/pull/42153/)).

### Bug Fix

Expand Down
9 changes: 2 additions & 7 deletions packages/components/src/slot-fill/index.native.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { omit } from 'lodash';

/**
* Internal dependencies
*/
Expand All @@ -12,8 +7,8 @@ import Provider from './provider';

export { Fill, Provider };

export function Slot( props ) {
return <BaseSlot { ...omit( props, 'bubblesVirtually' ) } />;
export function Slot( { bubblesVirtually, ...restProps } ) {
return <BaseSlot { ...restProps } />;
}

export function createSlotFill( name ) {
Expand Down
10 changes: 3 additions & 7 deletions packages/components/src/slot-fill/provider.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
import { without } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -78,7 +73,8 @@ export default class SlotFillProvider extends Component {
}

unregisterFill( name, instance ) {
this.fills[ name ] = without( this.fills[ name ], instance );
this.fills[ name ] =
this.fills[ name ]?.filter( ( fill ) => fill !== instance ) ?? [];
this.forceUpdateSlot( name );
}

Expand Down Expand Up @@ -115,7 +111,7 @@ export default class SlotFillProvider extends Component {
this.listeners.push( listener );

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

Expand Down
45 changes: 21 additions & 24 deletions packages/components/src/slot-fill/slot.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
import { isString, map } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -72,25 +67,27 @@ class SlotComponent extends Component {
render() {
const { children, name, fillProps = {}, getFills } = this.props;

const fills = map( getFills( name, this ), ( fill ) => {
const fillChildren = isFunction( fill.children )
? fill.children( fillProps )
: fill.children;

return Children.map( fillChildren, ( child, childIndex ) => {
if ( ! child || isString( child ) ) {
return child;
}

const childKey = child.key || childIndex;
return cloneElement( child, { key: childKey } );
} );
} ).filter(
// In some cases fills are rendered only when some conditions apply.
// This ensures that we only use non-empty fills when rendering, i.e.,
// it allows us to render wrappers only when the fills are actually present.
( element ) => ! isEmptyElement( element )
);
const fills = ( getFills( name, this ) ?? [] )
.map( ( fill ) => {
const fillChildren = isFunction( fill.children )
? fill.children( fillProps )
: fill.children;

return Children.map( fillChildren, ( child, childIndex ) => {
if ( ! child || typeof child === 'string' ) {
return child;
}

const childKey = child.key || childIndex;
return cloneElement( child, { key: childKey } );
} );
} )
.filter(
// In some cases fills are rendered only when some conditions apply.
// This ensures that we only use non-empty fills when rendering, i.e.,
// it allows us to render wrappers only when the fills are actually present.
( element ) => ! isEmptyElement( element )
);

return <>{ isFunction( children ) ? children( fills ) : fills }</>;
}
Expand Down