Skip to content

Commit

Permalink
feat: Refactor custom button and use tailwind classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas0912 committed Nov 5, 2024
1 parent 16aa9ee commit 0e01723
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 85 deletions.
88 changes: 14 additions & 74 deletions src/components/Buttons/CustomButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client';

import { Button, ButtonProps } from '@nextui-org/button';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';
import clsx from 'clsx';

interface Props extends ButtonProps {
children: React.ReactNode;
Expand All @@ -12,79 +9,22 @@ interface Props extends ButtonProps {
* Custom button component in the variants solid, bordered and flat. It can be used like a normal NextUI button
* component.
* @param variant solid | bordered | flat
* @constructor
*/

export function CustomButton({ children, ...attributes }: Props) {
const [isHovered, setIsHovered] = useState<boolean>(false);
const [bgFlat, setBgFlat] = useState<string>('');
const [colorFlat, setColorFlat] = useState<string>('');

const { theme } = useTheme();
const { variant } = attributes;

useEffect(() => {
setBgFlat(theme === 'light' ? 'var(--flat-button)' : 'var(--flat-button-dark)');
setColorFlat(theme === 'light' ? 'var(--button-dark-text)' : 'var(--button-light-text)');
}, [theme]);

switch (variant as string) {
case 'bordered':
return (
<Button
{...attributes}
style={
isHovered
? {
backgroundColor:
theme === 'light' ? 'var(--outlined-button-hovered)' : 'var(--outlined-button-hovered-dark)',
}
: {}
}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
{children}
</Button>
);
case 'flat':
return (
<Button
{...attributes}
style={
isHovered
? { color: 'var(--button-light-text)', backgroundColor: 'var(--button-hovered)' }
: {
color: colorFlat,
backgroundColor: bgFlat,
}
}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
{children}
</Button>
);
case 'solid':
return (
<Button
{...attributes}
backgroundColor="green"
style={
isHovered
? { color: 'var(--button-light-text)', backgroundColor: 'var(--button-hovered)' }
: {
color: 'var(--button-light-text)',
backgroundColor: 'var(--button-main)',
}
}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
{children}
</Button>
);
default:
return <Button {...attributes}>{children}</Button>;
}
return (
<Button
{...attributes}
className={clsx({
'hover:bg-outlinedHover': variant === 'bordered',
'hover:bg-hover dark:text-foreground': variant === 'flat' || variant === 'solid',
'bg-clickableSecondary hover:text-background ': variant === 'flat',
'bg-primary dark:hover:bg-hover text-background': variant === 'solid',
})}
>
{children}
</Button>
);
}
11 changes: 0 additions & 11 deletions src/styles/globals.css
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
--button-main: #157dbc;
--button-hovered: #0f6396;
--button-light-text: #EFEFEF;
--button-dark-text: #333333;
--outlined-button-hovered: #e3f2fd;
--outlined-button-hovered-dark: #0f6396;
--flat-button: #EEEEEE;
--flat-button-dark: #424242;
}
6 changes: 6 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = {
background: '#F5F5F5',
divider: '#157DBC',
focus: '#157DBC',
hover: '#005489',
outlinedHover: '#E3F2FD',
clickableSecondary: '#E6E6E6',
danger: '#D32F2F',
warning: '#FFB600',
clusterGreen: '#85E77C',
Expand All @@ -47,6 +50,9 @@ module.exports = {
background: '#121212',
divider: '#157DBC',
focus: '#157DBC',
hover: '#0F6396',
outlinedHover: '#0F6396',
clickableSecondary: '#424242',
danger: '#EF5350',
warning: '#FFEB3B',
clusterGreen: '#A3F39C',
Expand Down

0 comments on commit 0e01723

Please sign in to comment.