To refactor the provided code, we can streamline the repeated styles and leverage SCSS features such as mixins and variables to reduce redundancy. Here's how you can refactor it:
// Disabled
$disabledOpacity: 0.4;
// Define a mixin for button styles
@mixin button-styles($bgColor, $bgGradient, $borderColor, $borderImage, $textColor, $bgGradientActive: null) {
background-color: $bgColor;
background: $bgGradient;
border-color: $borderColor;
border-image: $borderImage;
color: $textColor;
&:active {
background: if($bgGradientActive, $bgGradientActive, $bgGradient);
}
&:disabled {
background-color: $bgColor;
}
}
// Define a mixin for secondary button styles
@mixin secondary-styles($bgColor, $bgGradient, $borderColor, $borderImage, $textColor, $activeBorderColor, $activeTextColor) {
background-color: $bgColor;
background: $bgGradient;
border-color: $borderColor;
border-image: $borderImage;
color: $textColor;
&:active {
border-color: $activeBorderColor;
color: $activeTextColor;
}
&:disabled {
background-color: $bgColor;
}
}
.button {
&.use-gradient {
&.yellow {
// Default State
$primaryBg: transparent;
$primaryGradient: linear-gradient(180deg, #FFE768 2.02%, #F6B737 57.52%, #F48E30 99.78%);
$primaryBorder: #FFBE34;
$primaryBorderImage: none;
$primaryColor: #850404;
$secondaryBg: #fff;
$secondaryBorder: #FFBE34;
$secondaryColor: #CD6200;
$onlyIconBg: transparent;
$onlyIconGradient: linear-gradient(180deg, #FFE768 0.27%, #F6B737 57.77%, #F48E30 91.93%);
$onlyIconBorder: transparent;
$onlyIconBorderImage: linear-gradient(360deg, #FFBC3A -0.62%, #FFC556 100.36%);
// Active State (On Pressed)
$activeGradient: linear-gradient(180deg, #FEEA91 0.27%, #FAD279 57.77%, #F8B06F 91.93%);
&.primary {
@include button-styles($primaryBg, $primaryGradient, $primaryBorder, $primaryBorderImage, $primaryColor, $activeGradient);
}
&.primary-with-icon {
@include button-styles($primaryBg, $primaryGradient, $primaryBorder, $primaryBorderImage, $primaryColor, $activeGradient);
}
&.secondary {
@include secondary-styles($secondaryBg, none, $secondaryBorder, none, $secondaryColor, #F9CB73, #DC914D);
}
&.only-icon {
@include button-styles($onlyIconBg, $onlyIconGradient, $onlyIconBorder, $onlyIconBorderImage, transparent);
}
}
&.green {
// Default State
$primaryBg: transparent;
$primaryGradient: linear-gradient(180deg, #71DC19 0.27%, #129F12 57.77%, #0D950D 91.93%);
$primaryBorder: transparent;
$primaryBorderImage: linear-gradient(360deg, #56B110 -0.62%, #64C916 100.36%);
$primaryColor: #fff;
$secondaryBg: #fff;
$secondaryBorder: #169D00;
$secondaryColor: #169D00;
$onlyIconBg: transparent;
$onlyIconGradient: linear-gradient(180deg, #71DC19 0.27%, #129F12 57.77%, #0D950D 91.93%);
$onlyIconBorder: transparent;
$onlyIconBorderImage: linear-gradient(360deg, #56B110 -0.62%, #64C916 100.36%);
// Active State (On Pressed)
$activeGradient: linear-gradient(180deg, #71DC19 0.27%, #129F12 57.77%, #0D950D 91.93%), linear-gradient(0deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.2));
&.primary {
@include button-styles($primaryBg, $primaryGradient, $primaryBorder, $primaryBorderImage, $primaryColor, $activeGradient);
}
&.primary-with-icon {
@include button-styles($primaryBg, $primaryGradient, $primaryBorder, $primaryBorderImage, $primaryColor, $activeGradient);
}
&.secondary {
@include secondary-styles($secondaryBg, none, $secondaryBorder, none, $secondaryColor, #4AC437, #4AC437);
}
&.only-icon {
@include button-styles($onlyIconBg, $onlyIconGradient, $onlyIconBorder, $onlyIconBorderImage, transparent);
}
}
// Common style
box-shadow: 0px 2px 5px 0px #20000036;
&:disabled {
opacity: $disabledOpacity;
}
}
}
Changes Made:
- Mixins: Introduced
button-styles and secondary-styles mixins to encapsulate repeated styling logic. This reduces the need to repeatedly define the same styles for different button types.
- Variables & Clarity: Maintained the use of variables for backgrounds, gradients, and colors to make it easy to adjust in one place.
- Whitespace & Comments: Enhanced readability with consistent indentation and organized comments.
- Single-Responsibility Principle: Each button variant now has a more focused style application, minimizing redundancy.
This refactor allows for easier maintenance and readability of the CSS while preserving functionality.