From 7b779e4a734da1f5e4e35fcccd9f1d289b27b860 Mon Sep 17 00:00:00 2001 From: winches <329487092@qq.com> Date: Tue, 7 Jan 2025 00:07:09 +0800 Subject: [PATCH] feat: add post install --- .changeset/smart-eagles-hope.md | 5 ++++ packages/utilities/shared-utils/package.json | 4 ++- .../shared-utils/scripts/postinstall.js | 19 ++++++++++++ .../utilities/shared-utils/src/demi/demi.d.ts | 15 ++++++++++ .../utilities/shared-utils/src/demi/index.ts | 2 ++ .../shared-utils/src/demi/react18/index.ts | 17 +++++++++++ .../shared-utils/src/demi/react19/index.ts | 17 +++++++++++ .../utilities/shared-utils/src/functions.ts | 29 ------------------- packages/utilities/shared-utils/src/index.ts | 2 ++ 9 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 .changeset/smart-eagles-hope.md create mode 100644 packages/utilities/shared-utils/scripts/postinstall.js create mode 100644 packages/utilities/shared-utils/src/demi/demi.d.ts create mode 100644 packages/utilities/shared-utils/src/demi/index.ts create mode 100644 packages/utilities/shared-utils/src/demi/react18/index.ts create mode 100644 packages/utilities/shared-utils/src/demi/react19/index.ts diff --git a/.changeset/smart-eagles-hope.md b/.changeset/smart-eagles-hope.md new file mode 100644 index 0000000000..c8cf276405 --- /dev/null +++ b/.changeset/smart-eagles-hope.md @@ -0,0 +1,5 @@ +--- +"@nextui-org/shared-utils": patch +--- + +Tabs with prop destroyInactiveTabPanel error in nextjs15(#4344) diff --git a/packages/utilities/shared-utils/package.json b/packages/utilities/shared-utils/package.json index e6eb2579ea..7e16c8c5ce 100644 --- a/packages/utilities/shared-utils/package.json +++ b/packages/utilities/shared-utils/package.json @@ -25,6 +25,7 @@ "url": "https://github.com/nextui-org/nextui/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", @@ -43,6 +44,7 @@ "format": [ "cjs", "esm" - ] + ], + "splitting": false } } \ No newline at end of file diff --git a/packages/utilities/shared-utils/scripts/postinstall.js b/packages/utilities/shared-utils/scripts/postinstall.js new file mode 100644 index 0000000000..7baf14e5b1 --- /dev/null +++ b/packages/utilities/shared-utils/scripts/postinstall.js @@ -0,0 +1,19 @@ +const { switchVersion, loadModule } = require('./utils') + +const Vue = loadModule('vue') + +if (!Vue || typeof Vue.version !== 'string') { + console.warn('[vue-demi] Vue is not found. Please run "npm install vue" to install.') +} +else if (Vue.version.startsWith('2.7.')) { + switchVersion(2.7) +} +else if (Vue.version.startsWith('2.')) { + switchVersion(2) +} +else if (Vue.version.startsWith('3.')) { + switchVersion(3) +} +else { + console.warn(`[vue-demi] Vue version v${Vue.version} is not supported.`) +} diff --git a/packages/utilities/shared-utils/src/demi/demi.d.ts b/packages/utilities/shared-utils/src/demi/demi.d.ts new file mode 100644 index 0000000000..3c60423032 --- /dev/null +++ b/packages/utilities/shared-utils/src/demi/demi.d.ts @@ -0,0 +1,15 @@ +/** + * 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 declare function getInertValue(v: boolean): boolean | string | undefined; diff --git a/packages/utilities/shared-utils/src/demi/index.ts b/packages/utilities/shared-utils/src/demi/index.ts new file mode 100644 index 0000000000..12d535cfb8 --- /dev/null +++ b/packages/utilities/shared-utils/src/demi/index.ts @@ -0,0 +1,2 @@ +export * from "./react19"; +export * from "./react18"; diff --git a/packages/utilities/shared-utils/src/demi/react18/index.ts b/packages/utilities/shared-utils/src/demi/react18/index.ts new file mode 100644 index 0000000000..fecfe49700 --- /dev/null +++ b/packages/utilities/shared-utils/src/demi/react18/index.ts @@ -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 getInertValueReact18 = (v: boolean): boolean | string | undefined => { + return v ? "" : undefined; +}; diff --git a/packages/utilities/shared-utils/src/demi/react19/index.ts b/packages/utilities/shared-utils/src/demi/react19/index.ts new file mode 100644 index 0000000000..1e38de5503 --- /dev/null +++ b/packages/utilities/shared-utils/src/demi/react19/index.ts @@ -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 getInertValueReact19 = (v: boolean): boolean | string | undefined => { + return v; +}; diff --git a/packages/utilities/shared-utils/src/functions.ts b/packages/utilities/shared-utils/src/functions.ts index bdbcec3db4..2eece226c6 100644 --- a/packages/utilities/shared-utils/src/functions.ts +++ b/packages/utilities/shared-utils/src/functions.ts @@ -1,5 +1,3 @@ -import React from "react"; - type Args = T extends (...args: infer R) => any ? R : never; type AnyFunction = (...args: T[]) => any; @@ -391,30 +389,3 @@ export const intersectionBy = (...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; -}; diff --git a/packages/utilities/shared-utils/src/index.ts b/packages/utilities/shared-utils/src/index.ts index eec14e1c31..34293353b2 100644 --- a/packages/utilities/shared-utils/src/index.ts +++ b/packages/utilities/shared-utils/src/index.ts @@ -9,3 +9,5 @@ export * from "./console"; export * from "./types"; export * from "./dates"; export * from "./regex"; + +export type {getInertValue} from "./demi/demi";