Skip to content

Commit

Permalink
fix: inert value in next15 (heroui-inc#4491)
Browse files Browse the repository at this point in the history
* feat: add post install

* feat: add postinstall

* feat: add postinstall

* fix: type

* fix: type

* fix: next version

* chore(changeset): update package name

---------

Co-authored-by: աӄա <[email protected]>
  • Loading branch information
winchesHe and wingkwong authored Feb 5, 2025
1 parent 12a5c15 commit 7402e00
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-eagles-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/shared-utils": patch
---

Tabs with prop destroyInactiveTabPanel error in nextjs15(#4344)
16 changes: 13 additions & 3 deletions packages/utilities/shared-utils/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @heroui/system
# @heroui/shared-utils

A Quick description of the component

Expand All @@ -7,9 +7,9 @@ A Quick description of the component
## Installation

```sh
yarn add @heroui/system
yarn add @heroui/shared-utils
# or
npm i @heroui/system
npm i @heroui/shared-utils
```

## Contribution
Expand All @@ -18,6 +18,16 @@ Yes please! See the
[contributing guidelines](https://github.com/heroui-inc/heroui/blob/master/CONTRIBUTING.md)
for details.

## File structure

```
src/
├── common/ # Common utilities for all React versions
└── demi/ # Demi utilities for different React versions
├── react18/
└── react19/
```

## License

This project is licensed under the terms of the
Expand Down
13 changes: 9 additions & 4 deletions packages/utilities/shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"main": "src/index.ts",
"sideEffects": false,
"files": [
"dist"
"dist",
"scripts"
],
"publishConfig": {
"access": "public"
Expand All @@ -25,13 +26,15 @@
"url": "https://github.com/heroui-inc/heroui/issues"
},
"scripts": {
"postinstall": "node -e \"try{require('./scripts/postinstall.js')}catch(e){}\"",
"build": "tsup src --dts",
"dev": "pnpm build:fast --watch",
"clean": "rimraf dist .turbo",
"typecheck": "tsc --noEmit",
"build:fast": "tsup src",
"prepack": "clean-package",
"postpack": "clean-package restore"
"postpack": "clean-package restore",
"postbuild": "npm run postinstall"
},
"devDependencies": {
"clean-package": "2.2.0"
Expand All @@ -43,6 +46,8 @@
"format": [
"cjs",
"esm"
]
],
"splitting": false,
"entry": ["src/demi/react18", "src/demi/react19"]
}
}
}
41 changes: 41 additions & 0 deletions packages/utilities/shared-utils/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const path = require('path')
const fs = require('fs')

function tryRequirePkg(pkg) {
try {
return require(pkg);
} catch (e) {
return null;
}
}

function copyDemiDir(dir) {
const src = path.join(__dirname, '..', 'dist', 'demi', dir)
const dest = path.join(__dirname, '..', 'dist')

fs.cpSync(src, dest, { recursive: true })
}

function modifyDts(path) {
const dts = fs.readFileSync(path, 'utf8')
const modifiedDts = dts.replace(/\.\.\/\.\.\/common/g, './common')

fs.writeFileSync(path, modifiedDts, 'utf8')
}

function postinstall() {
const nextjs = tryRequirePkg('next/package.json')
const react = tryRequirePkg('react/package.json')
const nextjsVersion = Number((nextjs?.version || '').split('.')[0])
const reactVersion = Number((react?.version || '').split('.')[0])

if (reactVersion === 18 && nextjsVersion < 15) {
copyDemiDir('react18')
} else {
copyDemiDir('react19')
}

modifyDts(path.join(__dirname, '..', 'dist', 'index.d.ts'))
}

postinstall();
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from "react";

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

type AnyFunction<T = any> = (...args: T[]) => any;
Expand Down Expand Up @@ -391,30 +389,3 @@ export const intersectionBy = <T>(...args: [...arrays: T[][], iteratee: Iteratee

return res;
};

/**
* Checks if the current React version is 19.x.x
*
* @returns {boolean} - Returns `true` if the React major version is 19, otherwise returns `false`.
*/
export const isReact19 = (): boolean => {
return React.version.split(".")[0] === "19";
};

/**
* Returns an appropriate value for the `inert` attribute based on the React version.
*
* In React 19, the attribute `inert` is a boolean. In versions prior to 19, the attribute
* behaves differently: setting `inert=""` will make it `true`, and `inert=undefined` will make it `false`.
*
* @param {boolean} v - The desired boolean state for the `inert` attribute.
* @returns {boolean | string | undefined} - Depending on the React version:
* - Returns `boolean` if React version is 19 (the input value `v` directly).
* - Returns `string` (empty string) if `v` is `true` in older React versions.
* - Returns `undefined` if `v` is `false` in older React versions.
*
* @see {@link https://github.com/facebook/react/issues/17157} for more details on the behavior in older React versions.
*/
export const getInertValue = (v: boolean): boolean | string | undefined => {
return isReact19() ? v : v ? "" : undefined;
};
11 changes: 11 additions & 0 deletions packages/utilities/shared-utils/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export * from "./assertion";
export * from "./clsx";
export * from "./object";
export * from "./text";
export * from "./dimensions";
export * from "./functions";
export * from "./numbers";
export * from "./console";
export * from "./types";
export * from "./dates";
export * from "./regex";
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/utilities/shared-utils/src/demi/react18/getInertValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Returns an appropriate value for the `inert` attribute based on the React version.
*
* In React 19, the attribute `inert` is a boolean. In versions prior to 19, the attribute
* behaves differently: setting `inert=""` will make it `true`, and `inert=undefined` will make it `false`.
*
* @param {boolean} v - The desired boolean state for the `inert` attribute.
* @returns {boolean | string | undefined} - Depending on the React version:
* - Returns `boolean` if React version is 19 (the input value `v` directly).
* - Returns `string` (empty string) if `v` is `true` in older React versions.
* - Returns `undefined` if `v` is `false` in older React versions.
*
* @see {@link https://github.com/facebook/react/issues/17157} for more details on the behavior in older React versions.
*/
export const getInertValue = (v: boolean): boolean | string | undefined => {
return v ? "" : undefined;
};
3 changes: 3 additions & 0 deletions packages/utilities/shared-utils/src/demi/react18/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./getInertValue";

export * from "../../common";
17 changes: 17 additions & 0 deletions packages/utilities/shared-utils/src/demi/react19/getInertValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Returns an appropriate value for the `inert` attribute based on the React version.
*
* In React 19, the attribute `inert` is a boolean. In versions prior to 19, the attribute
* behaves differently: setting `inert=""` will make it `true`, and `inert=undefined` will make it `false`.
*
* @param {boolean} v - The desired boolean state for the `inert` attribute.
* @returns {boolean | string | undefined} - Depending on the React version:
* - Returns `boolean` if React version is 19 (the input value `v` directly).
* - Returns `string` (empty string) if `v` is `true` in older React versions.
* - Returns `undefined` if `v` is `false` in older React versions.
*
* @see {@link https://github.com/facebook/react/issues/17157} for more details on the behavior in older React versions.
*/
export const getInertValue = (v: boolean): boolean | string | undefined => {
return v;
};
3 changes: 3 additions & 0 deletions packages/utilities/shared-utils/src/demi/react19/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./getInertValue";

export * from "../../common";
16 changes: 5 additions & 11 deletions packages/utilities/shared-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
export * from "./assertion";
export * from "./clsx";
export * from "./object";
export * from "./text";
export * from "./dimensions";
export * from "./functions";
export * from "./numbers";
export * from "./console";
export * from "./types";
export * from "./dates";
export * from "./regex";
/**
* For Development
*/
export * from "./common";
export * from "./demi/react18";

0 comments on commit 7402e00

Please sign in to comment.