Skip to content

Commit fb8cd77

Browse files
committed
Allow LocalToastTarget to accept and fowrard ref
1 parent 4d7c37f commit fb8cd77

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

.size-snapshot.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
{
22
"react-local-toast.cjs.development.js": {
3-
"bundled": 19552,
4-
"minified": 9729,
5-
"gzipped": 2987
3+
"bundled": 19764,
4+
"minified": 9865,
5+
"gzipped": 3026
66
},
77
"react-local-toast.cjs.production.js": {
8-
"bundled": 19552,
9-
"minified": 9729,
10-
"gzipped": 2987
8+
"bundled": 19764,
9+
"minified": 9865,
10+
"gzipped": 3026
1111
},
1212
"react-local-toast.esm.js": {
13-
"bundled": 18374,
14-
"minified": 8770,
15-
"gzipped": 2837,
13+
"bundled": 18549,
14+
"minified": 8873,
15+
"gzipped": 2873,
1616
"treeshaked": {
1717
"rollup": {
18-
"code": 225,
19-
"import_statements": 225
18+
"code": 262,
19+
"import_statements": 262
2020
},
2121
"webpack": {
22-
"code": 8631
22+
"code": 8757
2323
}
2424
}
2525
},
2626
"react-local-toast.umd.development.js": {
27-
"bundled": 108971,
28-
"minified": 34283,
29-
"gzipped": 11230
27+
"bundled": 110084,
28+
"minified": 34477,
29+
"gzipped": 11263
3030
},
3131
"react-local-toast.umd.production.js": {
32-
"bundled": 58443,
33-
"minified": 21288,
34-
"gzipped": 7138
32+
"bundled": 59556,
33+
"minified": 21483,
34+
"gzipped": 7172
3535
},
3636
"internals.js": {
3737
"bundled": 10559,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-local-toast",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Local toast helps you to provide feedback related to particular components on page",
55
"main": "dist/index.js",
66
"module": "dist/react-local-toast.esm.js",
@@ -49,6 +49,7 @@
4949
},
5050
"dependencies": {
5151
"@babel/runtime": "^7.14.6",
52+
"@radix-ui/react-compose-refs": "^1.0.0",
5253
"@radix-ui/react-slot": "^0.1.2",
5354
"clsx": "^1.1.1",
5455
"react-icons": "^4.4.0",

src/target.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1+
import { useComposedRefs } from '@radix-ui/react-compose-refs';
12
import { Slot } from '@radix-ui/react-slot';
23
import React from 'react';
34
import { LocalToastContextType } from './context';
45

5-
export interface LocalToastTargetProps {
6+
export interface LocalToastTargetPropsWithoutRef {
67
name: string;
78
children: React.ReactNode;
89
}
910

10-
export type LocalToastTargetType = (props: LocalToastTargetProps) => JSX.Element;
11+
export interface LocalToastTargetProps<R> extends LocalToastTargetPropsWithoutRef {
12+
ref?: React.Ref<R>
13+
}
14+
15+
export type LocalToastTargetType = <R extends HTMLElement>(props: LocalToastTargetProps<R>) => JSX.Element;
1116

1217
export const createTarget = <T,>(Context: React.Context<LocalToastContextType<T>>): LocalToastTargetType => {
13-
return ({ name, children }: LocalToastTargetProps) => {
18+
return React.forwardRef<HTMLElement, LocalToastTargetPropsWithoutRef>(<R extends HTMLElement,>({ name, children }: LocalToastTargetPropsWithoutRef, forwardedRef: React.ForwardedRef<R>) => {
1419
const ctx = React.useContext(Context);
15-
const ref = React.useRef<HTMLElement>(null);
20+
const ref = React.useRef<R>(null);
21+
22+
const composeRefs = useComposedRefs(forwardedRef, ref);
23+
1624
React.useEffect(() => {
1725
ctx.registerRef(name, ref);
1826
return () => {
1927
ctx.removeRef(name);
2028
};
2129
}, []);
2230

23-
return <Slot ref={ref}>{children}</Slot>;
24-
};
31+
return <Slot ref={composeRefs}>{children}</Slot>;
32+
}) as LocalToastTargetType;
2533
};

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,13 @@
11261126
dependencies:
11271127
"@babel/runtime" "^7.13.10"
11281128

1129+
"@radix-ui/react-compose-refs@^1.0.0":
1130+
version "1.0.0"
1131+
resolved "https://registry.yarnpkg.com/@radix-ui/react-compose-refs/-/react-compose-refs-1.0.0.tgz#37595b1f16ec7f228d698590e78eeed18ff218ae"
1132+
integrity sha512-0KaSv6sx787/hK3eF53iOkiSLwAGlFMx5lotrqD2pTjB18KbybKoEIgkNZTKC60YECDQTKGTRcDBILwZVqVKvA==
1133+
dependencies:
1134+
"@babel/runtime" "^7.13.10"
1135+
11291136
"@radix-ui/react-slot@^0.1.2":
11301137
version "0.1.2"
11311138
resolved "https://registry.yarnpkg.com/@radix-ui/react-slot/-/react-slot-0.1.2.tgz#e6f7ad9caa8ce81cc8d532c854c56f9b8b6307c8"

0 commit comments

Comments
 (0)