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

Show a warning when SlotFillProvider is missing #23493

Merged
merged 2 commits into from
Jun 26, 2020
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
18 changes: 10 additions & 8 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
WritingFlow,
ObserveTyping
} from '@wordpress/block-editor';
import { Popover } from '@wordpress/components';
import { SlotFillProvider, Popover } from '@wordpress/components';
import { useState } from '@wordpress/element';

function MyEditorComponent () {
Expand All @@ -33,13 +33,15 @@ function MyEditorComponent () {
onInput={ updateBlocks }
onChange={ updateBlocks }
>
<Popover.Slot name="block-toolbar" />
<WritingFlow>
<ObserveTyping>
<BlockList />
</ObserveTyping>
</WritingFlow>
<Popover.Slot />
<SlotFillProvider>
<Popover.Slot name="block-toolbar" />
<WritingFlow>
<ObserveTyping>
<BlockList />
</ObserveTyping>
</WritingFlow>
<Popover.Slot />
</SlotFillProvider>
</BlockEditorProvider>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';
import warning from '@wordpress/warning';

const SlotFillContext = createContext( {
slots: {},
fills: {},
registerSlot: () => {},
registerSlot: () => {
warning(
'Components must be wrapped within `SlotFillProvider`. ' +
'See https://developer.wordpress.org/block-editor/components/slot-fill/'
);
},
updateSlot: () => {},
unregisterSlot: () => {},
registerFill: () => {},
Expand Down
22 changes: 8 additions & 14 deletions packages/components/src/slot-fill/test/__snapshots__/slot.js.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Slot bubblesVirtually false should not break without a Provider 1`] = `
<div>
<div />
</div>
`;

exports[`Slot bubblesVirtually false should subsume another slot by the same name 1`] = `
<div>
<div
Expand Down Expand Up @@ -43,14 +37,6 @@ exports[`Slot bubblesVirtually false should unmount two slots with the same name
</div>
`;

exports[`Slot bubblesVirtually true should not break without a Provider 1`] = `
<div>
<div>
<div />
</div>
</div>
`;

exports[`Slot bubblesVirtually true should subsume another slot by the same name 1`] = `
<div>
<div
Expand Down Expand Up @@ -178,3 +164,11 @@ exports[`Slot should render in expected order 1`] = `
/>
</div>
`;

exports[`Slot should warn without a Provider 1`] = `
<div>
<div>
<div />
</div>
</div>
`;
30 changes: 14 additions & 16 deletions packages/components/src/slot-fill/test/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ describe( 'Slot', () => {
expect( container ).toMatchSnapshot();
} );

it( 'should warn without a Provider', () => {
const { container } = render(
<>
<div>
<Slot name="chicken" bubblesVirtually />
</div>
<Fill name="chicken" />
</>
);

expect( container ).toMatchSnapshot();
expect( console ).toHaveWarned();
} );

describe.each( [ false, true ] )(
'bubblesVirtually %p',
( bubblesVirtually ) => {
Expand Down Expand Up @@ -300,22 +314,6 @@ describe( 'Slot', () => {
);
expect( container ).toMatchSnapshot();
} );

it( 'should not break without a Provider', () => {
const { container } = render(
<>
<div>
<Slot
name="chicken"
bubblesVirtually={ bubblesVirtually }
/>
</div>
<Fill name="chicken" />
</>
);

expect( container ).toMatchSnapshot();
} );
}
);
} );