Skip to content

Commit

Permalink
Merge pull request JedWatson#3928 from dpordomingo/update-context-api
Browse files Browse the repository at this point in the history
Update MenuPlacer context usage to the new context API
  • Loading branch information
JedWatson authored Aug 27, 2020
2 parents 0eb1ef9 + ad608c8 commit 801cd5b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/update-context-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-select': patch
---

Update MenuPlacer context usage in order to the new React Context API
23 changes: 10 additions & 13 deletions packages/react-select/src/components/Menu.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// @flow
/** @jsx jsx */
import {
createContext,
Component,
type Element as ReactElement,
type ElementRef,
type Node,
} from 'react';
import { jsx } from '@emotion/core';
import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';

import {
animatedScrollTo,
Expand Down Expand Up @@ -258,15 +258,16 @@ export const menuCSS = ({
zIndex: 1,
});

const PortalPlacementContext = createContext<() => void>(() => { });

// NOTE: internal only
export class MenuPlacer extends Component<MenuPlacerProps, MenuState> {
state = {
maxHeight: this.props.maxMenuHeight,
placement: null,
};
static contextTypes = {
getPortalPlacement: PropTypes.func,
};
static contextType = PortalPlacementContext;

getPlacement = (ref: ElementRef<*>) => {
const {
minMenuHeight,
Expand Down Expand Up @@ -481,14 +482,6 @@ export const menuPortalCSS = ({ rect, offset, position }: PortalStyleArgs) => ({

export class MenuPortal extends Component<MenuPortalProps, MenuPortalState> {
state = { placement: null };
static childContextTypes = {
getPortalPlacement: PropTypes.func,
};
getChildContext() {
return {
getPortalPlacement: this.getPortalPlacement,
};
}

// callback for occassions where the menu must "flip"
getPortalPlacement = ({ placement }: MenuState) => {
Expand Down Expand Up @@ -526,6 +519,10 @@ export class MenuPortal extends Component<MenuPortalProps, MenuPortalState> {
<div css={getStyles('menuPortal', state)}>{children}</div>
);

return appendTo ? createPortal(menuWrapper, appendTo) : menuWrapper;
return (
<PortalPlacementContext.Provider value={this.getPortalPlacement}>
{appendTo ? createPortal(menuWrapper, appendTo) : menuWrapper}
</PortalPlacementContext.Provider>
);
}
}

0 comments on commit 801cd5b

Please sign in to comment.