Skip to content

Commit

Permalink
Migrate Popover component to the new context Consumer API
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Oct 26, 2018
1 parent 28091fc commit 4474b64
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
36 changes: 19 additions & 17 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { noop } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -20,7 +19,7 @@ import withConstrainedTabbing from '../higher-order/with-constrained-tabbing';
import PopoverDetectOutside from './detect-outside';
import IconButton from '../icon-button';
import ScrollLock from '../scroll-lock';
import { Slot, Fill } from '../slot-fill';
import { Slot, Fill, SlotFillConsumer } from '../slot-fill';

const FocusManaged = withConstrainedTabbing( withFocusReturn( ( { children } ) => children ) );

Expand Down Expand Up @@ -302,17 +301,24 @@ class Popover extends Component {
content = <FocusManaged>{ content }</FocusManaged>;
}

// In case there is no slot context in which to render, default to an
// in-place rendering.
const { getSlot } = this.context;
if ( getSlot && getSlot( SLOT_NAME ) ) {
content = <Fill name={ SLOT_NAME }>{ content }</Fill>;
}

return <span ref={ this.anchorNode }>
{ content }
{ isMobile && expandOnMobile && <ScrollLock /> }
</span>;
return (
<SlotFillConsumer>
{ ( { getSlot } ) => {
// In case there is no slot context in which to render,
// default to an in-place rendering.
if ( getSlot && getSlot( SLOT_NAME ) ) {
content = <Fill name={ SLOT_NAME }>{ content }</Fill>;
}

return (
<span ref={ this.anchorNode }>
{ content }
{ isMobile && expandOnMobile && <ScrollLock /> }
</span>
);
} }
</SlotFillConsumer>
);
}
}

Expand All @@ -323,10 +329,6 @@ Popover.defaultProps = {

const PopoverContainer = Popover;

PopoverContainer.contextTypes = {
getSlot: noop,
};

PopoverContainer.Slot = () => <Slot bubblesVirtually name={ SLOT_NAME } />;

export default PopoverContainer;
4 changes: 2 additions & 2 deletions packages/components/src/slot-fill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*/
import Slot from './slot';
import Fill from './fill';
import Provider from './provider';
import Provider, { SlotFillConsumer } from './provider';

export { Slot };
export { Fill };
export { Provider };
export { Provider, SlotFillConsumer };

export function createSlotFill( name ) {
const FillComponent = ( props ) => <Fill name={ name } { ...props } />;
Expand Down

0 comments on commit 4474b64

Please sign in to comment.