Skip to content

Commit

Permalink
chore: revert export provider context (#7416)
Browse files Browse the repository at this point in the history
* chore: revert exporting provider context

* fix circular dependency

* fix all the things

* Update packages/@react-aria/focus/src/FocusScope.tsx

Co-authored-by: Devon Govett <devongovett@gmail.com>

---------

Co-authored-by: Devon Govett <devongovett@gmail.com>
  • Loading branch information
snowystinger and devongovett authored Nov 21, 2024
1 parent c6bd2cb commit a530335
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 35 deletions.
1 change: 0 additions & 1 deletion packages/@react-aria/focus/src/FocusScope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,6 @@ function useRestoreFocus(scopeRef: RefObject<Element[] | null>, restoreFocus?: b
restoreFocus
&& nodeToRestore
&& (
// eslint-disable-next-line react-hooks/exhaustive-deps
((ownerDocument.activeElement && isElementInChildScope(ownerDocument.activeElement, scopeRef)) || (ownerDocument.activeElement === ownerDocument.body && shouldRestoreFocus(scopeRef)))
)
) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@react-spectrum/color/src/ColorThumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

import {classNames} from '@react-spectrum/utils';
import {Color} from '@react-types/color';
import {Context} from '@react-spectrum/provider';
import {DOMProps, RefObject} from '@react-types/shared';
import {Overlay} from '@react-spectrum/overlays';
import React, {CSSProperties, ReactElement, useContext, useRef, useState} from 'react';
import React, {CSSProperties, ReactElement, useRef, useState} from 'react';
import stylesHandle from '@adobe/spectrum-css-temp/components/colorhandle/vars.css';
import stylesLoupe from '@adobe/spectrum-css-temp/components/colorloupe/vars.css';
import {useId, useLayoutEffect} from '@react-aria/utils';
import {useProvider} from '@react-spectrum/provider';

interface ColorThumbProps extends DOMProps {
value: Color,
Expand All @@ -36,7 +36,7 @@ function ColorThumb(props: ColorThumbProps) {

let valueCSS = value.toString('css');
let loupeRef = useRef<HTMLElement | null>(null);
let provider = useContext(Context);
let provider = useProvider();

return (
<div className={classNames(stylesHandle, 'spectrum-ColorHandle', {'is-focused': isFocused, 'is-disabled': isDisabled}) + ' ' + className} style={style} {...otherProps}>
Expand Down
16 changes: 6 additions & 10 deletions packages/@react-spectrum/color/test/ColorArea.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

import {ColorArea} from '../';
import {composeStories} from '@storybook/react';
import {defaultTheme} from '@adobe/react-spectrum';
import {fireEvent, installMouseEvent, installPointerEvent, pointerMap, render} from '@react-spectrum/test-utils-internal';
import {fireEvent, installMouseEvent, installPointerEvent, pointerMap, renderv3 as render} from '@react-spectrum/test-utils-internal';
import {parseColor} from '@react-stately/color';
import {Provider} from '@react-spectrum/provider';
import React from 'react';
import * as stories from '../stories/ColorArea.stories';
import userEvent from '@testing-library/user-event';
Expand Down Expand Up @@ -207,13 +205,11 @@ describe('ColorArea', () => {
${'home/end'} | ${{defaultValue: parseColor('#f000f0')}} | ${{forward: (elem) => pressKey(elem, {key: 'End'}), backward: (elem) => pressKey(elem, {key: 'Home'})}} | ${parseColor('#df00f0')}
`('$Name RTL', async ({props, actions: {forward, backward}, result}) => {
let {getAllByRole} = render(
<Provider locale="ar-AE" theme={defaultTheme}>
<Component
{...props}
onChange={onChangeSpy}
onChangeEnd={onChangeEndSpy} />
</Provider>
);
<Component
{...props}
onChange={onChangeSpy}
onChangeEnd={onChangeEndSpy} />
, undefined, {locale: 'ar-AE'});
let [xSlider, ySlider] = getAllByRole('slider', {hidden: true});

expect(xSlider.getAttribute('aria-valuetext')).toBe([
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/color/test/ColorPicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import {act, pointerMap, render, within} from '@react-spectrum/test-utils-internal';
import {act, pointerMap, renderv3 as render, within} from '@react-spectrum/test-utils-internal';
import {ColorEditor, ColorPicker} from '../src';
import {Provider} from '@react-spectrum/provider';
import React from 'react';
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/color/test/ColorSlider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import {act, fireEvent, installMouseEvent, installPointerEvent, pointerMap, render} from '@react-spectrum/test-utils-internal';
import {act, fireEvent, installMouseEvent, installPointerEvent, pointerMap, renderv3 as render} from '@react-spectrum/test-utils-internal';
import {ColorSlider} from '../';
import {parseColor} from '@react-stately/color';
import React from 'react';
Expand Down
10 changes: 5 additions & 5 deletions packages/@react-spectrum/color/test/ColorWheel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import {act, fireEvent, installMouseEvent, installPointerEvent, pointerMap, render} from '@react-spectrum/test-utils-internal';
import {act, fireEvent, installMouseEvent, installPointerEvent, pointerMap, renderv3 as render} from '@react-spectrum/test-utils-internal';
import {ColorWheel} from '../';
import {ControlledHSL} from '../stories/ColorWheel.stories';
import {parseColor} from '@react-stately/color';
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('ColorWheel', () => {
let {container: _container, getByRole} = render(<ColorWheel defaultValue={defaultColor} onChange={onChangeSpy} />);
let slider = getByRole('slider');
let thumb = slider.parentElement;
let container = _container?.firstChild?.firstChild as HTMLElement;
let container = _container?.firstChild?.firstChild?.firstChild as HTMLElement;
container.getBoundingClientRect = getBoundingClientRect;

expect(document.activeElement).not.toBe(slider);
Expand All @@ -320,7 +320,7 @@ describe('ColorWheel', () => {
let defaultColor = parseColor('hsl(0, 100%, 50%)');
let {container: _container, getByRole} = render(<ColorWheel defaultValue={defaultColor} onChange={onChangeSpy} isDisabled />);
let slider = getByRole('slider');
let container = _container?.firstChild?.firstChild as HTMLElement;
let container = _container?.firstChild?.firstChild?.firstChild as HTMLElement;
container.getBoundingClientRect = getBoundingClientRect;

expect(document.activeElement).not.toBe(slider);
Expand All @@ -341,7 +341,7 @@ describe('ColorWheel', () => {
let defaultColor = parseColor('hsl(0, 100%, 50%)');
let {container: _container, getByRole} = render(<ControlledHSL defaultValue={defaultColor} onChange={onChangeSpy} onChangeEnd={onChangeEndSpy} />);
let slider = getByRole('slider');
let container = _container?.firstChild?.firstChild?.firstChild as HTMLElement;
let container = _container?.firstChild?.firstChild?.firstChild?.firstChild as HTMLElement;
container.getBoundingClientRect = getBoundingClientRect;

expect(document.activeElement).not.toBe(slider);
Expand Down Expand Up @@ -372,7 +372,7 @@ describe('ColorWheel', () => {
...
input.spectrum-ColorWheel-slider
*/
let handleColorElement = _container?.firstChild?.firstChild?.nextSibling?.firstChild as HTMLElement;
let handleColorElement = _container?.firstChild?.firstChild?.firstChild?.nextSibling?.firstChild as HTMLElement;
let thumbColor = parseColor(handleColorElement.style.backgroundColor);
expect(defaultColor.getChannelValue('alpha')).toEqual(0.5);
expect(thumbColor.getChannelValue('alpha')).toEqual(1);
Expand Down
14 changes: 10 additions & 4 deletions packages/@react-spectrum/icon/src/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import {AriaLabelingProps, DOMProps, IconColorValue, StyleProps} from '@react-types/shared';
import {baseStyleProps, classNames, StyleHandlers, useSlotProps, useStyleProps} from '@react-spectrum/utils';
import {Context} from '@react-spectrum/provider';
import {filterDOMProps} from '@react-aria/utils';
import React, {ReactElement, useContext} from 'react';
import {ProviderContext, useProvider} from '@react-spectrum/provider';
import React, {ReactElement} from 'react';
import styles from '@adobe/spectrum-css-temp/components/icon/vars.css';

export interface IconProps extends DOMProps, AriaLabelingProps, StyleProps {
Expand Down Expand Up @@ -70,9 +70,15 @@ export function Icon(props: IconProps) {
} = props;
let {styleProps} = useStyleProps(otherProps, iconStyleProps);

let provider = useContext(Context);
let provider: undefined | ProviderContext;
try {
// eslint-disable-next-line react-hooks/rules-of-hooks
provider = useProvider();
} catch {
// ignore
}
let scale = 'M';
if (provider !== null) {
if (provider != null) {
scale = provider.scale === 'large' ? 'L' : 'M';
}
if (!ariaHidden) {
Expand Down
14 changes: 10 additions & 4 deletions packages/@react-spectrum/icon/src/UIIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import {AriaLabelingProps, DOMProps, StyleProps} from '@react-types/shared';
import {classNames, useSlotProps, useStyleProps} from '@react-spectrum/utils';
import {Context} from '@react-spectrum/provider';
import {filterDOMProps} from '@react-aria/utils';
import React, {ReactElement, useContext} from 'react';
import {ProviderContext, useProvider} from '@react-spectrum/provider';
import React, {ReactElement} from 'react';
import styles from '@adobe/spectrum-css-temp/components/icon/vars.css';

export interface UIIconProps extends DOMProps, AriaLabelingProps, StyleProps {
Expand All @@ -38,9 +38,15 @@ export function UIIcon(props: UIIconProps) {
} = props;

let {styleProps} = useStyleProps(otherProps);
let provider = useContext(Context);
let provider: undefined | ProviderContext;
try {
// eslint-disable-next-line react-hooks/rules-of-hooks
provider = useProvider();
} catch {
// ignore
}
let scale = 'M';
if (provider !== null) {
if (provider != null) {
scale = provider.scale === 'large' ? 'L' : 'M';
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@react-spectrum/provider/src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {DOMRef} from '@react-types/shared';
import {filterDOMProps, RouterProvider} from '@react-aria/utils';
import {I18nProvider, useLocale} from '@react-aria/i18n';
import {ModalProvider, useModalProvider} from '@react-aria/overlays';
import {ProviderProps} from '@react-types/provider';
import {ProviderContext, ProviderProps} from '@react-types/provider';
import React, {useContext, useEffect, useRef} from 'react';
import styles from '@adobe/spectrum-css-temp/components/page/vars.css';
import typographyStyles from '@adobe/spectrum-css-temp/components/typography/index.css';
Expand Down Expand Up @@ -195,7 +195,7 @@ const ProviderWrapper = React.forwardRef(function ProviderWrapper(props: Provide
* Returns the various settings and styles applied by the nearest parent Provider.
* Properties explicitly set by the nearest parent Provider override those provided by preceeding Providers.
*/
export function useProvider() {
export function useProvider(): ProviderContext {
let context = useContext(Context);
if (!context) {
throw new Error(
Expand Down
1 change: 0 additions & 1 deletion packages/@react-spectrum/provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
/// <reference types="css-module-types" />

export {Provider, useProvider, useProviderProps} from './Provider';
export {Context} from './context';
export type {ProviderContext} from '@react-types/provider';
export type {ProviderProps} from '@react-types/provider';
11 changes: 8 additions & 3 deletions packages/@spectrum-icons/ui/scripts/generateIcons.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ function template(iconName) {
return (
`import {${iconName} as IconComponent} from '@adobe/react-spectrum-ui/dist/${iconName}.js';
import {UIIcon, UIIconPropsWithoutChildren} from '@react-spectrum/icon';
import {Context} from '@react-spectrum/provider';
import React, {useContext} from 'react';
import {useProvider} from '@react-spectrum/provider';
import React from 'react';
${jsx}
ExpressIcon.displayName = IconComponent.displayName;
export default function ${iconName}(props: UIIconPropsWithoutChildren) {
let provider = useContext(Context);
let provider;
try {
provider = useProvider();
} catch {
// ignore
}
return <UIIcon {...props}>{provider?.theme?.global?.express ? <ExpressIcon /> : <IconComponent />}</UIIcon>;
}
`
Expand Down
2 changes: 2 additions & 0 deletions packages/dev/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@react-aria/ssr": "^3.0.0",
"@react-spectrum/test-utils": "1.0.0-alpha.2",
"@react-spectrum/theme-default": "^3.5.13",
"@swc/helpers": "^0.5.0",
"@testing-library/dom": "^10.1.0",
"@testing-library/jest-dom": "^5.16.4",
Expand All @@ -31,6 +32,7 @@
"resolve": "^1.17.0"
},
"peerDependencies": {
"@react-spectrum/provider": "^3.9.12",
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1",
"react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/dev/test-utils/src/renderOverride.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
* governing permissions and limitations under the License.
*/

import {Provider} from '@react-spectrum/provider';
import React from 'react';
import {render} from '@testing-library/react';
import {StrictModeWrapper} from './StrictModeWrapper';
import {theme} from '@react-spectrum/theme-default';

let reactTestingLibrary = require('@testing-library/react');

Expand All @@ -34,3 +37,7 @@ function customRender(ui, options) {

// override render method with
export {customRender as render};

export function renderv3(ui, options, providerProps) {
return render(ui, {wrapper: (props) => <Provider theme={theme} {...providerProps}><StrictModeWrapper {...props} /></Provider>, ...options});
}
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8073,6 +8073,7 @@ __metadata:
"@adobe/spectrum-css-temp": "npm:3.0.0-alpha.1"
"@react-aria/ssr": "npm:^3.0.0"
"@react-spectrum/test-utils": "npm:1.0.0-alpha.2"
"@react-spectrum/theme-default": "npm:^3.5.13"
"@swc/helpers": "npm:^0.5.0"
"@testing-library/dom": "npm:^10.1.0"
"@testing-library/jest-dom": "npm:^5.16.4"
Expand All @@ -8081,6 +8082,7 @@ __metadata:
jest: "npm:^29.5.0"
resolve: "npm:^1.17.0"
peerDependencies:
"@react-spectrum/provider": ^3.9.12
react: ^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1
react-dom: ^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1
languageName: unknown
Expand Down

1 comment on commit a530335

@rspbot
Copy link

@rspbot rspbot commented on a530335 Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.