Skip to content

Commit

Permalink
Merge pull request #578 from gselderslaghs/buttons-scss-mixin
Browse files Browse the repository at this point in the history
refactor(Buttons) implement buttons styling with mixin
  • Loading branch information
wuda-io authored Jan 30, 2025
2 parents 1d8cb84 + 97e6837 commit 3321da9
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 43 deletions.
64 changes: 22 additions & 42 deletions sass/components/_buttons.scss
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
.btn, .btn-floating, .btn-large, .btn-small, .btn-flat {
:root {
--btn-height: 40px;
--btn-font-size-icon: 16px;
--btn-padding: 24px;
--btn-padding-icon: 16px;
--btn-gap-icon: 8px;
--btn-border-radius: 4px;
--btn-font-size: 14px;
}

height: var(--btn-height);
border: none;
border-radius: var(--btn-border-radius);
padding-left: var(--btn-padding);
padding-right: var(--btn-padding);
font-size: var(--btn-font-size);
font-weight: 500;
text-decoration: none;
display: inline-flex;
align-items: center;
cursor: pointer;
-webkit-tap-highlight-color: transparent; // Gets rid of tap active state
white-space: nowrap;
outline: 0;
user-select: none;
transition: background-color .2s ease-out;
.btn, .btn-floating, .btn-large, .btn-small, .btn-flat {
@include btn(
var(--btn-height),
var(--btn-border-radius),
var(--btn-padding),
var(--btn-padding),
var(--btn-font-size)
);
}

// Icon
Expand Down Expand Up @@ -54,49 +47,36 @@
//------------------ Enabled

.btn.filled {
color: var(--md-sys-color-on-primary);
background-color: var(--md-sys-color-primary);
@include btn-filled;
}

.btn.tonal {
color: var(--md-sys-color-on-secondary-container);
background-color: var(--md-sys-color-secondary-container);
@include btn-tonal;
}

.btn.elevated {
color: var(--md-sys-color-on-secondary-container);
background-color: var(--md-sys-color-secondary-container);
@extend .z-depth-1;
@include btn-elevated;
}

.btn.outlined {
background-color: transparent;
color: var(--md-sys-color-primary);
border: 1px solid var(--md-sys-color-outline);
@include btn-outlined;
}

.btn.text, .btn-flat {
@extend .z-depth-0;
color: var(--md-sys-color-primary);
background-color: transparent;
@include btn-flat;
}

//------------------ Disabled

.btn.disabled, .btn-floating.disabled, .btn-large.disabled, .btn-small.disabled, .btn-flat.disabled,
.btn:disabled, .btn-floating:disabled, .btn-large:disabled, .btn-small:disabled, .btn-flat:disabled,
.btn[disabled], .btn-floating[disabled], .btn-large[disabled], .btn-small[disabled], .btn-flat[disabled] {
@extend .z-depth-0;
color: color-mix(in srgb, transparent, var(--md-sys-color-on-surface) 76%);
background-color: color-mix(in srgb, transparent, var(--md-sys-color-on-surface) 24%);
pointer-events: none;
box-shadow: none;
cursor: default;
@include btn-disabled();
}

//------------------ Hover

.btn.elevated:hover {
/*.btn.elevated:hover {
@extend .z-depth-2;
color: var(--md-sys-color-primary);
background-color: color-mix(in srgb, var(--md-sys-color-secondary-container), var(--md-sys-color-on-secondary-container) 16%);
Expand All @@ -122,11 +102,11 @@
.btn.text:hover {
color: var(--md-sys-color-primary);
background-color: color-mix(in srgb, var(--md-sys-color-primary) 16%, transparent);
}
}*/

//------------------ Focus

.btn:focus {
/*.btn:focus {
background-color: var(--md-sys-color-primary-container);
}
Expand Down Expand Up @@ -157,10 +137,10 @@
.btn:focus.text {
color: var(--md-sys-color-primary);
background-color: color-mix(in srgb, transparent, var(--md-sys-color-primary) 20%);
}
}*/

// Focus with Keyboard
.btn:focus-visible {
/*.btn:focus-visible {
&.filled,
&.elevated,
&.tonal,
Expand All @@ -169,7 +149,7 @@
outline: 3px solid var(--md-sys-color-secondary);
outline-offset: 2px;
}
}
}*/

//----------

Expand Down
155 changes: 155 additions & 0 deletions sass/components/mixins.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
@mixin btn(
$height: var(--btn-height),
$border-radius: var(--btn-border-radius),
$padding-left: var(--btn-padding),
$padding-right: var(--btn-padding),
$font-size: var(--btn-font-size),
) {
height: $height;
border-radius: $border-radius;
padding-left: $padding-left;
padding-right: $padding-right;
font-size: $font-size;
font-weight: 500;
text-decoration: none;
display: inline-flex;
align-items: center;
cursor: pointer;
-webkit-tap-highlight-color: transparent; // Gets rid of tap active state
white-space: nowrap;
outline: 0;
user-select: none;
transition: background-color .2s ease-out;

&:focus {
background-color: var(--md-sys-color-primary-container);
}
}

@mixin btn-filled {
color: var(--md-sys-color-on-primary);
background-color: var(--md-sys-color-primary);

&:hover,
&:focus {
color: var(--md-sys-color-on-primary);
}

&:hover {
@extend .z-depth-1;
background-color: color-mix(in srgb, var(--md-sys-color-primary), var(--md-sys-color-on-primary) 16%);
}

&:focus {
@extend .z-depth-0;
background-color: color-mix(in srgb, var(--md-sys-color-primary), var(--md-sys-color-on-primary) 20%);
}

@include focus-visible();
}

@mixin btn-tonal {
color: var(--md-sys-color-on-secondary-container);
background-color: var(--md-sys-color-secondary-container);

&:hover,
&:focus {
color: var(--md-sys-color-on-secondary-container);
}

&:hover {
@extend .z-depth-1;
background-color: color-mix(in srgb, var(--md-sys-color-secondary-container), var(--md-sys-color-on-secondary-container) 16%);
}

&:focus {
@extend .z-depth-0;
background-color: color-mix(in srgb, var(--md-sys-color-secondary-container), var(--md-sys-color-on-secondary-container) 20%);
}

@include focus-visible();
}

@mixin btn-elevated {
color: var(--md-sys-color-on-secondary-container);
background-color: var(--md-sys-color-secondary-container);
@extend .z-depth-1;

&:hover,
&:focus {
color: var(--md-sys-color-primary);
}

&:hover {
@extend .z-depth-2;
background-color: color-mix(in srgb, var(--md-sys-color-secondary-container), var(--md-sys-color-on-secondary-container) 16%);
}

&:focus {
@extend .z-depth-1;
background-color: color-mix(in srgb, var(--md-sys-color-secondary-container), var(--md-sys-color-primary) 20%);
}

@include focus-visible();
}

@mixin btn-outlined {
background-color: transparent;
color: var(--md-sys-color-primary);
border: 1px solid var(--md-sys-color-outline);

&:hover,
&:focus {
color: var(--md-sys-color-primary);
}

&:hover {
background-color: color-mix(in srgb, transparent, var(--md-sys-color-primary) 16%);
}

&:focus {
background-color: color-mix(in srgb, transparent, var(--md-sys-color-primary) 20%);
border: 1px solid var(--md-sys-color-primary);
}

@include focus-visible();
}

@mixin btn-flat {
@extend .z-depth-0;
color: var(--md-sys-color-primary);
background-color: transparent;

&:hover,
&:focus {
color: var(--md-sys-color-primary);
}

&:hover {
background-color: color-mix(in srgb, var(--md-sys-color-primary) 16%, transparent);
}

&:focus {
background-color: color-mix(in srgb, transparent, var(--md-sys-color-primary) 20%);
}

@include focus-visible();
}

@mixin btn-disabled {
@extend .z-depth-0;
color: color-mix(in srgb, transparent, var(--md-sys-color-on-surface) 76%);
background-color: color-mix(in srgb, transparent, var(--md-sys-color-on-surface) 24%);
pointer-events: none;
box-shadow: none;
cursor: default;
}

// Focus with Keyboard
@mixin focus-visible {
&:focus-visible {
outline: 3px solid var(--md-sys-color-secondary);
outline-offset: 2px;
}
}

3 changes: 2 additions & 1 deletion sass/materialize.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//@import "components/_theme_variables";
@import "components/colors.module";
@import "components/typography.module";
@import "components/mixins.module";

// Color
@import "components/color-variables";
Expand All @@ -17,6 +18,7 @@

// components
@import "components/global";
@import "components/buttons";
@import "components/collection";
@import "components/badges";
@import "components/icons-material-design";
Expand All @@ -28,7 +30,6 @@
@import "components/toast";
@import "components/tabs";
@import "components/tooltip";
@import "components/buttons";
@import "components/dropdown";

@import "components/modal";
Expand Down

0 comments on commit 3321da9

Please sign in to comment.