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

fix: React inert prop compatibility #7519

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/@react-aria/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export {useEffectEvent} from './useEffectEvent';
export {useDeepMemo} from './useDeepMemo';
export {useFormReset} from './useFormReset';
export {useLoadMore} from './useLoadMore';
export {inertValue} from './inertValue';
export {CLEAR_FOCUS_EVENT, FOCUS_EVENT, UPDATE_ACTIVEDESCENDANT} from './constants';
9 changes: 9 additions & 0 deletions packages/@react-aria/utils/src/inertValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export function inertValue(value: any) {
snowystinger marked this conversation as resolved.
Show resolved Hide resolved
if (typeof React['use'] === 'function') {
snowystinger marked this conversation as resolved.
Show resolved Hide resolved
return !!value;
snowystinger marked this conversation as resolved.
Show resolved Hide resolved
}
// compatibility with React < 19
return value ? 'true' : undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Should this line actually be this?

Suggested change
return value ? 'true' : undefined;
if value == undefined {
return undefined;
}
return value ? 'true' : 'false';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is also how we handled it before. See facebook/react#17157 for more details about the behavior of the inert attribute in React < 19.

}
4 changes: 2 additions & 2 deletions packages/@react-spectrum/s2/src/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import intlMessages from '../intl/*.json';
import {Menu, MenuItem, MenuTrigger} from './Menu';
import {Text} from './Content';
import {useDOMRef, useResizeObserver} from '@react-spectrum/utils';
import {useLayoutEffect} from '@react-aria/utils';
import {inertValue, useLayoutEffect} from '@react-aria/utils';
import {useLocalizedStringFormatter} from '@react-aria/i18n';
import {useSpectrumContextProps} from './useSpectrumContextProps';

Expand Down Expand Up @@ -168,7 +168,7 @@ let HiddenBreadcrumbs = function (props: {listRef: RefObject<HTMLDivElement | nu
return (
<div
// @ts-ignore
inert="true"
inert={inertValue(true)}
ref={listRef}
className={style({
display: '[inherit]',
Expand Down
4 changes: 2 additions & 2 deletions packages/@react-spectrum/s2/src/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {ContentContext, FooterContext, TextContext} from './Content';
import {createContext, CSSProperties, forwardRef, ReactNode, useContext} from 'react';
import {DividerContext} from './Divider';
import {DOMProps, DOMRef, DOMRefValue} from '@react-types/shared';
import {filterDOMProps} from '@react-aria/utils';
import {filterDOMProps, inertValue} from '@react-aria/utils';
import {focusRing, lightDark, space, style} from '../style' with {type: 'macro'};
import {getAllowedOverrides, StyleProps, UnsafeStyles} from './style-utils' with {type: 'macro'};
import {IllustrationContext} from './Icon';
Expand Down Expand Up @@ -421,7 +421,7 @@ export const Card = forwardRef(function Card(props: CardProps, ref: DOMRef<HTMLD
{...filterDOMProps(otherProps)}
id={id != null ? String(id) : undefined}
// @ts-ignore - React < 19 compat
inert={isSkeleton ? 'true' : undefined}
inert={inertValue(isSkeleton)}
ref={domRef}
className={UNSAFE_className + card({size, density, variant, isCardView: ElementType !== 'div'}, styles)}
style={UNSAFE_style}>
Expand Down
3 changes: 2 additions & 1 deletion packages/@react-spectrum/s2/src/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import {ContextValue, Keyboard as KeyboardAria, Header as RACHeader, Heading as RACHeading, TextContext as RACTextContext, SlotProps, Text as TextAria} from 'react-aria-components';
import {createContext, forwardRef, ReactNode, useContext} from 'react';
import {DOMRef, DOMRefValue} from '@react-types/shared';
import {inertValue} from '@react-aria/utils';
import {StyleString} from '../style/types';
import {UnsafeStyles} from './style-utils';
import {useDOMRef} from '@react-spectrum/utils';
Expand Down Expand Up @@ -107,7 +108,7 @@ export const Text = forwardRef(function Text(props: ContentProps, ref: DOMRef) {
{...otherProps}
ref={domRef}
// @ts-ignore - compatibility with React < 19
inert={isSkeleton ? 'true' : undefined}
inert={inertValue(isSkeleton)}
className={UNSAFE_className + styles}
style={UNSAFE_style}
slot={slot || undefined}
Expand Down
4 changes: 2 additions & 2 deletions packages/@react-spectrum/s2/src/Skeleton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import {cloneElement, createContext, CSSProperties, ReactElement, ReactNode, Ref, useCallback, useContext, useRef} from 'react';
import {colorToken} from '../style/tokens' with {type: 'macro'};
import {mergeRefs} from '@react-aria/utils';
import {inertValue, mergeRefs} from '@react-aria/utils';
import {mergeStyles} from '../style/runtime';
import {raw} from '../style/style-macro' with {type: 'macro'};
import {style} from '../style' with {type: 'macro'};
Expand Down Expand Up @@ -103,7 +103,7 @@ export function SkeletonText({children}) {
return (
<span
// @ts-ignore - compatibility with React < 19
inert="true"
inert={inertValue(true)}
ref={useLoadingAnimation(true)}
className={loadingStyle + style({
color: 'transparent',
Expand Down
4 changes: 2 additions & 2 deletions packages/@react-spectrum/s2/src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {Picker, PickerItem} from './TabsPicker';
import {Text, TextContext} from './Content';
import {useControlledState} from '@react-stately/utils';
import {useDOMRef} from '@react-spectrum/utils';
import {useEffectEvent, useLayoutEffect, useResizeObserver} from '@react-aria/utils';
import {inertValue, useEffectEvent, useLayoutEffect, useResizeObserver} from '@react-aria/utils';
import {useLocale, useLocalizedStringFormatter} from '@react-aria/i18n';
import {useSpectrumContextProps} from './useSpectrumContextProps';

Expand Down Expand Up @@ -447,7 +447,7 @@ let HiddenTabs = function (props: {
return (
<div
// @ts-ignore
inert="true"
inert={inertValue(true)}
ref={listRef}
className={style({
display: '[inherit]',
Expand Down
4 changes: 2 additions & 2 deletions packages/@react-spectrum/s2/src/TagGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import intlMessages from '../intl/*.json';
import {pressScale} from './pressScale';
import {Text, TextContext} from './Content';
import {useDOMRef} from '@react-spectrum/utils';
import {useEffectEvent, useId, useLayoutEffect, useResizeObserver} from '@react-aria/utils';
import {inertValue, useEffectEvent, useId, useLayoutEffect, useResizeObserver} from '@react-aria/utils';
import {useLocalizedStringFormatter} from '@react-aria/i18n';
import {useSpectrumContextProps} from './useSpectrumContextProps';

Expand Down Expand Up @@ -319,7 +319,7 @@ function TagGroupInner<T>({
{maxRows != null && (
<div
// @ts-ignore
inert="true"
inert={inertValue(true)}
ref={hiddenTagsRef}
className={style({
display: 'inline',
Expand Down
4 changes: 2 additions & 2 deletions packages/react-aria-components/src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {AriaTabListProps, AriaTabPanelProps, mergeProps, Orientation, useFocusRi
import {Collection, CollectionBuilder, createHideableComponent, createLeafComponent} from '@react-aria/collections';
import {CollectionProps, CollectionRendererContext, DefaultCollectionRenderer, usePersistedKeys} from './Collection';
import {ContextValue, Provider, RenderProps, SlotProps, StyleRenderProps, useContextProps, useRenderProps, useSlottedContext} from './utils';
import {filterDOMProps, useObjectRef} from '@react-aria/utils';
import {filterDOMProps, inertValue, useObjectRef} from '@react-aria/utils';
import {Collection as ICollection, Node, TabListState, useTabListState} from 'react-stately';
import React, {createContext, ForwardedRef, forwardRef, JSX, useContext, useMemo} from 'react';

Expand Down Expand Up @@ -323,7 +323,7 @@ export const TabPanel = /*#__PURE__*/ createHideableComponent(function TabPanel(
data-focused={isFocused || undefined}
data-focus-visible={isFocusVisible || undefined}
// @ts-ignore
inert={!isSelected ? 'true' : undefined}
inert={inertValue(!isSelected)}
data-inert={!isSelected ? 'true' : undefined}>
<Provider
values={[
Expand Down