Skip to content

Commit 3e30cc3

Browse files
committed
feat: add primitives module use-effect-event
1 parent 9dd5b73 commit 3e30cc3

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "soybean-react-ui/use-effect-event",
3+
"version": "0.0.2",
4+
"license": "MIT",
5+
"homepage": "https://radix-ui.com/primitives",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/radix-ui/primitives.git"
9+
},
10+
"bugs": {
11+
"url": "https://github.com/radix-ui/primitives/issues"
12+
},
13+
"publishConfig": {
14+
"main": "./dist/index.js",
15+
"module": "./dist/index.mjs",
16+
"types": "./dist/index.d.ts",
17+
"exports": {
18+
".": {
19+
"import": {
20+
"types": "./dist/index.d.mts",
21+
"default": "./dist/index.mjs"
22+
},
23+
"require": {
24+
"types": "./dist/index.d.ts",
25+
"default": "./dist/index.js"
26+
}
27+
}
28+
}
29+
},
30+
"sideEffects": false,
31+
"main": "./src/index.ts",
32+
"module": "./src/index.ts",
33+
"files": ["dist", "src", "README.md"],
34+
"scripts": {
35+
"build": "radix-build",
36+
"clean": "rm -rf dist",
37+
"lint": "eslint --max-warnings 0 src",
38+
"typecheck": "tsc --noEmit"
39+
},
40+
"peerDependencies": {
41+
"@types/react": "*",
42+
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
43+
},
44+
"peerDependenciesMeta": {
45+
"@types/react": {
46+
"optional": true
47+
}
48+
},
49+
"dependencies": {
50+
"soybean-react-ui/use-layout-effect": "workspace:*"
51+
},
52+
"devDependencies": {
53+
"@types/react": "19.0.7",
54+
"@types/react-dom": "19.0.3",
55+
"@types/use-sync-external-store": "0.0.6",
56+
"react": "19.1.0",
57+
"react-dom": "19.1.0",
58+
"typescript": "5.7.3"
59+
},
60+
"source": "./src/index.ts"
61+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { useEffectEvent } from './use-effect-event';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable react-hooks/rules-of-hooks */
2+
import * as React from 'react';
3+
import { useLayoutEffect } from 'soybean-react-ui/use-layout-effect';
4+
5+
type AnyFunction = (...args: any[]) => any;
6+
7+
// See https://github.com/webpack/webpack/issues/14814
8+
const useReactEffectEvent = (React as any)[' useEffectEvent '.trim().toString()];
9+
const useReactInsertionEffect = (React as any)[' useInsertionEffect '.trim().toString()];
10+
11+
/**
12+
* Designed to approximate the behavior on `experimental_useEffectEvent` as best
13+
* as possible until its stable release, and back-fill it as a shim as needed.
14+
*/
15+
export function useEffectEvent<T extends AnyFunction>(callback?: T): T {
16+
if (typeof useReactEffectEvent === 'function') {
17+
return useReactEffectEvent(callback);
18+
}
19+
20+
const ref = React.useRef<AnyFunction | undefined>(() => {
21+
throw new Error('Cannot call an event handler while rendering.');
22+
});
23+
// See https://github.com/webpack/webpack/issues/14814
24+
if (typeof useReactInsertionEffect === 'function') {
25+
useReactInsertionEffect(() => {
26+
ref.current = callback;
27+
});
28+
} else {
29+
useLayoutEffect(() => {
30+
ref.current = callback;
31+
});
32+
}
33+
34+
// https://github.com/facebook/react/issues/19240
35+
return React.useMemo(() => ((...args) => ref.current?.(...args)) as T, []);
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": ["src"],
7+
"exclude": ["node_modules", "dist"]
8+
}

0 commit comments

Comments
 (0)