Skip to content

Commit

Permalink
fix: inert value in next15
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Jan 4, 2025
1 parent 44d466e commit e5415f0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/components/calendar/src/calendar-month.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {HTMLNextUIProps} from "@nextui-org/system";
import {useLocale} from "@react-aria/i18n";
import {useCalendarGrid} from "@react-aria/calendar";
import {m} from "framer-motion";
import {dataAttr, getInertValue} from "@nextui-org/shared-utils";
import {dataAttr, useInertValue} from "@nextui-org/shared-utils";

import {CalendarCell} from "./calendar-cell";
import {slideVariants} from "./calendar-transitions";
Expand Down Expand Up @@ -34,14 +34,16 @@ export function CalendarMonth(props: CalendarMonthProps) {
state,
);

const inertValue = useInertValue(!!isHeaderExpanded);

const bodyContent = [...new Array(weeksInMonth).keys()].map((weekIndex) => (
<tr
key={weekIndex}
className={slots?.gridBodyRow({class: classNames?.gridBodyRow})}
data-slot="grid-body-row"
// makes the browser ignore the element and its children when tabbing
// @ts-ignore
inert={getInertValue(!!isHeaderExpanded)}
inert={inertValue}
>
{state
.getDatesInWeek(weekIndex, startDate)
Expand Down
6 changes: 4 additions & 2 deletions packages/components/calendar/src/calendar-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {CalendarPickerProps} from "./use-calendar-picker";

import {HTMLNextUIProps} from "@nextui-org/system";
import {useCallback} from "react";
import {getInertValue} from "@nextui-org/shared-utils";
import {useInertValue} from "@nextui-org/shared-utils";

import {CalendarPickerItem} from "./calendar-picker-item";
import {useCalendarPicker} from "./use-calendar-picker";
Expand Down Expand Up @@ -60,6 +60,8 @@ export function CalendarPicker(props: CalendarPickerProps) {
[EmptyItem],
);

const inertValue = useInertValue(!isHeaderExpanded);

return (
<div
className={slots?.pickerWrapper({
Expand All @@ -68,7 +70,7 @@ export function CalendarPicker(props: CalendarPickerProps) {
data-slot="picker-wrapper"
// makes the browser ignore the element and its children when tabbing
// @ts-ignore
inert={getInertValue(!isHeaderExpanded)}
inert={inertValue}
>
<div
ref={highlightRef}
Expand Down
6 changes: 4 additions & 2 deletions packages/components/tabs/src/tab-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {AriaTabPanelProps} from "@react-aria/tabs";
import {Key} from "@react-types/shared";
import {forwardRef, HTMLNextUIProps} from "@nextui-org/system";
import {useDOMRef} from "@nextui-org/react-utils";
import {clsx, getInertValue} from "@nextui-org/shared-utils";
import {clsx, useInertValue} from "@nextui-org/shared-utils";
import {mergeProps} from "@react-aria/utils";
import {useTabPanel} from "@react-aria/tabs";
import {useFocusRing} from "@react-aria/focus";
Expand Down Expand Up @@ -59,6 +59,8 @@ const TabPanel = forwardRef<"div", TabPanelProps>((props, ref) => {

const isSelected = tabKey === selectedItem?.key;

const inertValue = useInertValue(!isSelected);

if (!content || (!isSelected && destroyInactiveTabPanel)) {
return null;
}
Expand All @@ -70,7 +72,7 @@ const TabPanel = forwardRef<"div", TabPanelProps>((props, ref) => {
data-focus-visible={isFocusVisible}
data-inert={!isSelected ? "true" : undefined}
// makes the browser ignore the element and its children when tabbing
inert={getInertValue(!isSelected)}
inert={inertValue}
{...(isSelected && mergeProps(tabPanelProps, focusProps, otherProps))}
className={slots.panel?.({class: tabPanelStyles})}
data-slot="panel"
Expand Down
12 changes: 11 additions & 1 deletion packages/utilities/shared-utils/src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, {useEffect, useState} from "react";

type Args<T extends Function> = T extends (...args: infer R) => any ? R : never;

Expand Down Expand Up @@ -435,3 +435,13 @@ export const isNextJS15 = async () => {
export const getInertValue = async (v: boolean): Promise<boolean | string | undefined> => {
return isReact19() || (await isNextJS15()) ? v : v ? "" : undefined;
};

export const useInertValue = (v: boolean) => {
const [inertValue, setInertValue] = useState<boolean | string | undefined>(v);

useEffect(() => {
getInertValue(v).then(setInertValue);
}, [v]);

return inertValue;
};

0 comments on commit e5415f0

Please sign in to comment.