1
- import { useMemo } from "react" ;
1
+ import React , {
2
+ type ComponentProps ,
3
+ type ElementType ,
4
+ type FC ,
5
+ type PropsWithChildren ,
6
+ } from "react" ;
2
7
import type { Prettify } from "./types" ;
3
8
4
9
// eslint-disable-next-line react-refresh/only-export-components
5
- const SlotFragment : React . FC < React . PropsWithChildren < { [ x : string ] : any } > > = ( {
10
+ const SlotFragment : FC < PropsWithChildren < { [ x : string ] : any } > > = ( {
6
11
children,
7
12
} ) => {
8
13
// eslint-disable-next-line react/jsx-no-useless-fragment
9
14
return < > { children } </ > ;
10
15
} ;
11
16
SlotFragment . displayName = "@zemd/react-slottable/SlotFragment" ;
12
17
13
- type SlotOptions < T extends React . ElementType > = {
18
+ type SlotOptions < T extends ElementType > = {
14
19
slot ?: T ;
15
20
} ;
16
21
17
- type SlotsProps < T extends Record < string , React . ElementType > > = {
22
+ type SlotsProps < T extends Record < string , ElementType > > = {
18
23
slots ?: T ;
19
24
} ;
20
25
@@ -26,12 +31,12 @@ type SlotPropsProps<T extends Record<string, any>> = {
26
31
* A hook that returns a component for a given slot.
27
32
*/
28
33
export function useSlot <
29
- ArgSlotType extends React . ElementType ,
34
+ ArgSlotType extends ElementType ,
30
35
ArgSlotOptions extends SlotOptions < ArgSlotType > ,
31
36
ReturnComponent extends ArgSlotOptions extends SlotOptions < infer T >
32
37
? T
33
- : React . ElementType ,
34
- ArgSlotsKeys extends Record < string , React . ElementType > ,
38
+ : ElementType ,
39
+ ArgSlotsKeys extends Record < string , ElementType > ,
35
40
> (
36
41
name : keyof ArgSlotsKeys ,
37
42
props : Prettify <
@@ -41,15 +46,15 @@ export function useSlot<
41
46
) : ReturnComponent {
42
47
const { slots, slotProps } = props ;
43
48
44
- const Slot = useMemo ( ( ) => {
49
+ const Slot = React . useMemo ( ( ) => {
45
50
return ( slots ?. [ name ] ?? options . slot ?? SlotFragment ) as ReturnComponent ;
46
51
} , [ name , slots , options . slot ] ) ;
47
52
48
- return useMemo < ReturnComponent > ( ( ) => {
49
- const WrappedComponent : React . FC < React . ComponentProps < ReturnComponent > > = (
53
+ return React . useMemo < ReturnComponent > ( ( ) => {
54
+ const WrappedComponent : FC < ComponentProps < ReturnComponent > > = (
50
55
wrappedProps ,
51
56
) => {
52
- const mergedProps : React . ComponentProps < ReturnComponent > = {
57
+ const mergedProps : ComponentProps < ReturnComponent > = {
53
58
...slotProps ?. [ name ] ,
54
59
...wrappedProps ,
55
60
} ;
0 commit comments