forked from academind/react-typescript-course-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIconButton.tsx
39 lines (34 loc) · 887 Bytes
/
IconButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Example: A Button component that has an icon and text
// The icon is passed via a prop, which is a function that returns JSX code
import type { ComponentPropsWithoutRef, ElementType, ReactNode } from 'react';
type IconButtonProps = {
icon: ElementType;
onClick: () => void;
children: ReactNode;
} & ComponentPropsWithoutRef<'button'>;
export function IconButton({
// icon is aliased to Icon because it should be used like a custom component name
icon: Icon,
children,
...otherProps
}: IconButtonProps) {
return (
<button {...otherProps}>
<span>
<Icon />
</span>
<span>{children}</span>
</button>
);
}
// Example Usage:
function HeartIcon() {
return <span>❤️</span>;
}
export function Demo() {
return (
<IconButton icon={HeartIcon} onClick={() => console.log('Button clicked!')}>
Like
</IconButton>
);
}