Skip to content

Commit

Permalink
Make @leafygreen-ui/leafygreen-provider changes minor (#2528)
Browse files Browse the repository at this point in the history
* Make LG Provider changes minor

* Remove unused imports
  • Loading branch information
stephl3 authored Nov 4, 2024
1 parent 22b35d3 commit 7fa15ab
Show file tree
Hide file tree
Showing 30 changed files with 596 additions and 615 deletions.
4 changes: 2 additions & 2 deletions packages/code/src/CopyButton/CopyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CopyIcon from '@leafygreen-ui/icon/dist/Copy';
import IconButton from '@leafygreen-ui/icon-button';
import {
useDarkMode,
usePopoverContext,
usePopoverPortalContainer,
} from '@leafygreen-ui/leafygreen-provider';
import { keyMap } from '@leafygreen-ui/lib';
import Tooltip, {
Expand Down Expand Up @@ -37,7 +37,7 @@ function CopyButton({ onCopy, contents }: CopyProps) {
const buttonRef = useRef<HTMLButtonElement>(null);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const { theme } = useDarkMode();
const { portalContainer } = usePopoverContext();
const { portalContainer } = usePopoverPortalContainer();

/**
* toggles `tooltipOpen` state
Expand Down
6 changes: 3 additions & 3 deletions packages/combobox/src/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import Icon from '@leafygreen-ui/icon';
import IconButton from '@leafygreen-ui/icon-button';
import LeafyGreenProvider, {
PopoverProvider,
PopoverPropsProvider,
useDarkMode,
} from '@leafygreen-ui/leafygreen-provider';
import { consoleOnce, isComponentType, keyMap } from '@leafygreen-ui/lib';
Expand Down Expand Up @@ -1326,7 +1326,7 @@ export function Combobox<M extends boolean>({
* Menu *
/ *******/}

<PopoverProvider {...popoverProps}>
<PopoverPropsProvider {...popoverProps}>
<ComboboxMenu
id={menuId}
labelId={labelId}
Expand All @@ -1339,7 +1339,7 @@ export function Combobox<M extends boolean>({
>
{renderedOptionsJSX}
</ComboboxMenu>
</PopoverProvider>
</PopoverPropsProvider>
</div>
</ComboboxContext.Provider>
</LeafyGreenProvider>
Expand Down
4 changes: 2 additions & 2 deletions packages/copyable/src/Copyable/Copyable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useIdAllocator } from '@leafygreen-ui/hooks';
import CopyIcon from '@leafygreen-ui/icon/dist/Copy';
import {
useDarkMode,
usePopoverContext,
usePopoverPortalContainer,
} from '@leafygreen-ui/leafygreen-provider';
import { BaseFontSize } from '@leafygreen-ui/tokens';
import Tooltip, {
Expand Down Expand Up @@ -59,7 +59,7 @@ export default function Copyable({
const [buttonRef, setButtonRef] = useState<HTMLButtonElement>();
const codeRef = useRef<HTMLElement>(null);

const { portalContainer } = usePopoverContext();
const { portalContainer } = usePopoverPortalContainer();

const baseFontSize = useUpdatedBaseFontSize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { forwardRef } from 'react';

import { cx } from '@leafygreen-ui/emotion';
import {
ModalPopoverProvider,
PopoverProvider,
useDarkMode,
} from '@leafygreen-ui/leafygreen-provider';
import { HTMLElementProps } from '@leafygreen-ui/lib';
Expand Down Expand Up @@ -44,9 +44,9 @@ export const MenuWrapper = forwardRef<HTMLDivElement, MenuWrapperProps>(
>
{/*
* Prevents the opening and closing state of a select dropdown from propagating up
* to other ModalPopoverProviders in parent components. E.g. Modal
* to other PopoverProviders in parent components. E.g. Modal
*/}
<ModalPopoverProvider>{children}</ModalPopoverProvider>
<PopoverProvider>{children}</PopoverProvider>
</Popover>
);
},
Expand Down
8 changes: 5 additions & 3 deletions packages/leafygreen-provider/src/LeafyGreenContext.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import { cleanup, render } from '@testing-library/react';

import { LeafyGreenProviderProps } from './LeafyGreenContext';
import { LeafyGreenProviderProps } from './LeafyGreenContext.types';
import LeafyGreenProvider, {
useBaseFontSize,
useDarkMode,
usePopoverContext,
usePopoverPortalContainer,
} from '.';

afterAll(cleanup);

const ContextChecker = () => {
const { darkMode } = useDarkMode();
const baseFontSize = useBaseFontSize();
const { portalContainer, scrollContainer } = usePopoverContext();
const { portalContainer, scrollContainer } = usePopoverPortalContainer();

return (
<>
Expand Down Expand Up @@ -53,6 +53,7 @@ describe('packages/leafygreen-provider/LeafyGreenProvider', () => {
baseFontSize = 16,
portalId = 'portal',
scrollId = 'scroll',
forceUseTopLayer = false,
) => {
const portalContainer = document.createElement('div');
portalContainer.setAttribute('id', portalId);
Expand All @@ -66,6 +67,7 @@ describe('packages/leafygreen-provider/LeafyGreenProvider', () => {
portalContainer,
scrollContainer,
},
forceUseTopLayer,
} as Required<LeafyGreenProviderProps>;
};

Expand Down
84 changes: 24 additions & 60 deletions packages/leafygreen-provider/src/LeafyGreenContext.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
import React, { PropsWithChildren, useEffect, useState } from 'react';
import PropTypes from 'prop-types';

import { DarkModeProps } from '@leafygreen-ui/lib';

import { RenderMode } from './PopoverContext/PopoverContext.types';
import DarkModeProvider, { useDarkModeContext } from './DarkModeContext';
import { LeafyGreenProviderProps } from './LeafyGreenContext.types';
import { MigrationProvider, useMigrationContext } from './MigrationContext';
import {
MigrationContextType,
MigrationProvider,
useMigrationContext,
} from './MigrationContext';
import {
PopoverProvider,
PopoverProviderProps,
usePopoverContext,
} from './PopoverContext';
import TypographyProvider, {
TypographyProviderProps,
useBaseFontSize,
} from './TypographyContext';
PortalContextProvider,
usePopoverPortalContainer,
} from './PortalContext';
import TypographyProvider, { useBaseFontSize } from './TypographyContext';
import UsingKeyboardProvider from './UsingKeyboardContext';

type PopoverPortalContainerType = Pick<
PopoverProviderProps,
'portalContainer' | 'scrollContainer'
>;

export type LeafyGreenProviderProps = {
/**
* Define a container HTMLElement for components that utilize the `Portal` component
*/
popoverPortalContainer?: PopoverPortalContainerType;
} & TypographyProviderProps &
DarkModeProps &
MigrationContextType;

function LeafyGreenProvider({
children,
baseFontSize: fontSizeProp,
Expand All @@ -60,46 +36,34 @@ function LeafyGreenProvider({
const inheritedFontSize = useBaseFontSize();
const baseFontSize = fontSizeProp ?? inheritedFontSize;

/**
* If `popoverPortalContainer` prop is provided, use that. Otherwise, use context value
*/
const inheritedContainer = usePopoverPortalContainer();
const popoverPortalContainer =
popoverPortalContainerProp ?? inheritedContainer;

/**
* If `forceUseTopLayerProp` is true, it will globally apply to all children
*/
const migrationContext = useMigrationContext();
const forceUseTopLayer =
forceUseTopLayerProp || migrationContext.forceUseTopLayer;

/**
* If `popoverPortalContainer` prop is provided, use that. Otherwise, use context value
*/
const popoverContext = usePopoverContext();
const inheritedPopoverContextContainers: PopoverPortalContainerType =
Object.fromEntries(
Object.entries(popoverContext).filter(([key, _]) =>
['portalContainer', 'scrollContainer'].includes(key),
),
);
const { portalContainer, scrollContainer } =
popoverPortalContainerProp ?? inheritedPopoverContextContainers;
const popoverProviderProps =
portalContainer || scrollContainer
? { renderMode: RenderMode.Portal, portalContainer, scrollContainer }
: ({
renderMode: RenderMode.TopLayer,
} as const);

return (
<UsingKeyboardProvider>
<TypographyProvider baseFontSize={baseFontSize}>
<DarkModeProvider
contextDarkMode={darkModeState}
setDarkMode={setDarkMode}
>
<MigrationProvider forceUseTopLayer={forceUseTopLayer}>
<PopoverProvider {...popoverProviderProps}>
<PortalContextProvider popover={popoverPortalContainer}>
<TypographyProvider baseFontSize={baseFontSize}>
<DarkModeProvider
contextDarkMode={darkModeState}
setDarkMode={setDarkMode}
>
<MigrationProvider forceUseTopLayer={forceUseTopLayer}>
{children}
</PopoverProvider>
</MigrationProvider>
</DarkModeProvider>
</TypographyProvider>
</MigrationProvider>
</DarkModeProvider>
</TypographyProvider>
</PortalContextProvider>
</UsingKeyboardProvider>
);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/leafygreen-provider/src/LeafyGreenContext.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DarkModeProps } from '@leafygreen-ui/lib';

import { MigrationContextType } from './MigrationContext';
import { PortalContextValues } from './PortalContext';
import { TypographyProviderProps } from './TypographyContext';

export type LeafyGreenProviderProps = {
/**
* Define a container HTMLElement for components that utilize the `Portal` component
*/
popoverPortalContainer?: PortalContextValues['popover'];
} & TypographyProviderProps &
DarkModeProps &
MigrationContextType;

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7fa15ab

Please sign in to comment.