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

[EuiPortal] Add global settings for Portal-Based Components #6889

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 30 additions & 0 deletions src/components/portal/portal.provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React, { PropsWithChildren, useContext } from 'react';
import { EuiPortalInsertion } from './portal.types';

const PortalContext = React.createContext<EuiPortalInsertion | undefined>(
undefined
);

export function usePortalInsertion() {
return useContext(PortalContext);
}

export type PortalProviderProps = PropsWithChildren<{
insert: EuiPortalInsertion;
}>;

export function PortalProvider(props: PortalProviderProps) {
return (
<PortalContext.Provider value={props.insert}>
{props.children}
</PortalContext.Provider>
);
}
35 changes: 15 additions & 20 deletions src/components/portal/portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,12 @@
* into portals.
*/

import { Component, ReactNode } from 'react';
import React, { Component, ReactNode } from 'react';
import { createPortal } from 'react-dom';

import { EuiNestedThemeContext } from '../../services';
import { keysOf } from '../common';

interface InsertPositionsMap {
after: InsertPosition;
before: InsertPosition;
}

export const insertPositions: InsertPositionsMap = {
after: 'afterend',
before: 'beforebegin',
};

type EuiPortalInsertPosition = keyof typeof insertPositions;

export const INSERT_POSITIONS: EuiPortalInsertPosition[] =
keysOf(insertPositions);
import { usePortalInsertion } from './portal.provider';
import { insertPositions } from './portal.types';

export interface EuiPortalProps {
/**
Expand All @@ -41,7 +27,16 @@ export interface EuiPortalProps {
portalRef?: (ref: HTMLDivElement | null) => void;
}

export class EuiPortal extends Component<EuiPortalProps> {
export function EuiPortal(props: EuiPortalProps) {
const ctxInsertion = usePortalInsertion();
return (
<EuiPortalClassComponent {...props} insert={props.insert || ctxInsertion}>
{props.children}
</EuiPortalClassComponent>
);
}

export class EuiPortalClassComponent extends Component<EuiPortalProps> {
static contextType = EuiNestedThemeContext;

portalNode: HTMLDivElement | null = null;
Expand Down Expand Up @@ -78,7 +73,7 @@ export class EuiPortal extends Component<EuiPortalProps> {
}

// Set the inherited color of the portal based on the wrapping EuiThemeProvider
setThemeColor() {
private setThemeColor() {
if (this.portalNode && this.context) {
const { hasDifferentColorFromGlobalTheme, colorClassName } = this.context;

Expand All @@ -88,7 +83,7 @@ export class EuiPortal extends Component<EuiPortalProps> {
}
}

updatePortalRef(ref: HTMLDivElement | null) {
private updatePortalRef(ref: HTMLDivElement | null) {
if (this.props.portalRef) {
this.props.portalRef(ref);
}
Expand Down
29 changes: 29 additions & 0 deletions src/components/portal/portal.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { keysOf } from '../common';

interface InsertPositionsMap {
after: InsertPosition;
before: InsertPosition;
}

export const insertPositions: InsertPositionsMap = {
after: 'afterend',
before: 'beforebegin',
};

export type EuiPortalInsertPosition = keyof typeof insertPositions;

export const INSERT_POSITIONS: EuiPortalInsertPosition[] =
keysOf(insertPositions);

export interface EuiPortalInsertion {
sibling: HTMLElement;
position: EuiPortalInsertPosition;
}
21 changes: 20 additions & 1 deletion src/components/provider/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from '../../services';
import { EuiThemeAmsterdam } from '../../themes';
import { EuiCacheProvider } from './cache';
import { EuiPortalInsertion } from '../portal/portal.types';
import { PortalProvider } from '../portal/portal.provider';

const isEmotionCacheObject = (
obj: EmotionCache | Object
Expand Down Expand Up @@ -61,6 +63,16 @@ export interface EuiProviderProps<T>
global?: EmotionCache;
utility?: EmotionCache;
};
/**
* Provide global settings for EuiPortal props.
*/
portal?: {
/**
* Provide a global setting for EuiPortal's default insertion position.
* If not specified or set to `null`, `EuiPortal` will insert itself into the `document.body` by default.
*/
insert: EuiPortalInsertion | null;
};
}

export const EuiProvider = <T extends {} = {}>({
Expand All @@ -71,6 +83,7 @@ export const EuiProvider = <T extends {} = {}>({
colorMode,
modify,
children,
portal,
}: PropsWithChildren<EuiProviderProps<T>>) => {
let defaultCache;
let globalCache;
Expand Down Expand Up @@ -113,7 +126,13 @@ export const EuiProvider = <T extends {} = {}>({
/>
</>
)}
<CurrentEuiBreakpointProvider>{children}</CurrentEuiBreakpointProvider>
<CurrentEuiBreakpointProvider>
{portal?.insert ? (
<PortalProvider insert={portal.insert}>{children}</PortalProvider>
) : (
children
)}
</CurrentEuiBreakpointProvider>
</EuiThemeProvider>
</EuiCacheProvider>
);
Expand Down
1 change: 1 addition & 0 deletions upcoming_changelogs/6889.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added `portal` prop to `EuiProvider`.