Skip to content

Commit 1bd22c0

Browse files
committed
refactor and add types to CustomButton component
1 parent 74c9c22 commit 1bd22c0

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

packages/gatsby-theme/src/components/CustomButton.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,39 @@ import React from 'react';
22
import { ButtonProps } from './shared/base.d';
33
import { Flex, Image, Button } from './shared/base';
44

5-
interface CustomButtonProps extends ButtonProps {
6-
type?: string;
7-
}
8-
9-
export const CustomButton: React.FunctionComponent<CustomButtonProps> = ({
10-
type,
11-
children,
12-
...buttonProps
13-
}) => {
5+
export const CustomButton: React.FunctionComponent<
6+
CustomButtonProps,
7+
{ type?: 'github' | 'tutorial' | 'spectrum' | 'default' }
8+
> = ({ type, children, ...buttonProps }) => {
149
const { icon, bg } = customButtonTypes[type] || customButtonTypes.default;
15-
const text = children;
1610

1711
if (!type) {
18-
return <Button {...buttonProps}>{text}</Button>;
12+
return <Button {...buttonProps}>{children}</Button>;
1913
}
2014

2115
return (
2216
<Button {...buttonProps} bg={buttonProps.bg || bg}>
2317
<Flex alignItems="center" p={0} flexDirection="row">
2418
<Image src={icon} m={1} />
25-
{text}
19+
{children}
2620
</Flex>
2721
</Button>
2822
);
2923
};
3024

31-
const customButtonTypes = {
25+
interface ButtonType {
26+
icon: string;
27+
bg: string;
28+
}
29+
30+
interface CustomButtonType {
31+
tutorial: ButtonType;
32+
github: ButtonType;
33+
spectrum: ButtonType;
34+
default: ButtonType;
35+
}
36+
37+
const customButtonTypes: CustomButtonType = {
3238
tutorial: {
3339
icon: 'https://i.ibb.co/MBKTQ9b/Icon.png',
3440
bg: 'primary',

0 commit comments

Comments
 (0)