This repository was archived by the owner on Mar 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.tsx
More file actions
121 lines (115 loc) · 2.51 KB
/
Button.tsx
File metadata and controls
121 lines (115 loc) · 2.51 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React from 'react';
import Link from 'next/link';
import styles from '../styles/components/Button.module.scss';
import Image from '../components/Image';
import { type ImageDescription } from '../config';
export enum ButtonStyle {
// Regular
Default,
// Social
Social,
// Navbar
NavButton,
NavToggle,
}
const getButtonStyle = (buttonStyle: ButtonStyle) => {
switch (buttonStyle) {
// Regular
case ButtonStyle.Default:
return [styles.default];
// Social
case ButtonStyle.Social:
return [styles.social];
// Navbar
case ButtonStyle.NavButton:
return [styles.nav];
case ButtonStyle.NavToggle:
return [styles.navToggle];
}
};
/**
* A list of possible button modifiers.
*/
export enum ButtonModifier {
/**
* Uses the active variant of the button if available.
*/
Active,
/**
* Uses the dark variant of the button if available.
*/
Dark,
}
const getButtonModifiers = (buttonModifiers: ButtonModifier[]) => {
const modifierStyles: string[] = [];
for (const modifier of buttonModifiers) {
switch (modifier) {
case ButtonModifier.Active:
modifierStyles.push(styles.active);
break;
case ButtonModifier.Dark:
modifierStyles.push(styles.dark);
break;
}
}
return modifierStyles;
};
interface Props {
buttonStyle?: ButtonStyle;
buttonModifiers?: ButtonModifier[];
className?: string;
onClick?: () => void;
href?: string;
}
interface LabelProps extends Props {
label: string;
image?: never;
}
interface IconProps extends Props {
image: ImageDescription;
label?: string;
}
const Button = ({
onClick,
href = '',
className,
buttonStyle = ButtonStyle.Default,
buttonModifiers = [],
label,
image,
}: LabelProps | IconProps) => {
// Get Styles
const classes = [
className,
styles.container,
...getButtonStyle(buttonStyle),
...getButtonModifiers(buttonModifiers),
].join(' ');
// Build Content
const content = [];
if (image != undefined) {
content.push(
<div key='button-image'>
<Image src={image.src} alt={image.alt} fill={true} />
</div>
);
}
if (label != undefined) {
content.push(
<span className={styles.buttonText} key='button-content'>
{label}
</span>
);
}
// Wrap Component
return href ? (
<Link className={classes} href={href} onClick={onClick}>
{content}
</Link>
) : (
<button className={classes} onClick={onClick}>
{content}
</button>
);
};
export default Button;