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

Update MenuPlacer context usage to the new context API #3928

Merged
merged 1 commit into from
Aug 27, 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
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>
);
}
}