Skip to content

Commit 31c7947

Browse files
committed
refactor: simplify color block generation in SCSS and enhance contrast logic
1 parent a7978a1 commit 31c7947

File tree

1 file changed

+36
-59
lines changed

1 file changed

+36
-59
lines changed

docs/lib/sage-frontend/stylesheets/docs/_colors.scss

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -43,78 +43,55 @@
4343
}
4444

4545
// build individual color blocks
46+
/* stylelint-disable max-nesting-depth */
4647
@each $name, $color in $sage-colors {
47-
// Skip if $color is not a map (handles aliases like primary, sage, orange, charcoal)
48-
@if type-of($color) == map {
49-
@each $value, $map in $color {
50-
// Skip if $map is null or not a map (safety check)
51-
@if $map != null and type-of($map) == map {
52-
// Use sage-color-hex() for SCSS operations (lightness() requires actual color values)
53-
$hex: sage-color-hex($name, $value);
54-
55-
// Determine class name for $value == 50
56-
$classname: ".color-#{"" + $name}-#{$value}";
57-
58-
/* stylelint-disable max-nesting-depth, block-closing-brace-newline-after */
59-
// This should be numeric, not string
60-
@if ($value == 50) {
61-
$classname: ".color-#{"" + $name}-0#{$value}";
62-
}
63-
64-
#{$classname} {
65-
padding: sage-spacing(sm) sage-spacing();
66-
font-size: sage-font-size(body);
67-
background: $hex;
68-
69-
// Use lightness-based logic for dynamic contrast (prioritized for better readability)
70-
// If background is dark (lightness < 65%), use light text; otherwise use dark text
71-
@if (lightness($hex) < 65) {
72-
// Dark background → use light text color
73-
@if ($name == white) {
74-
color: sage-color(black);
75-
} @else if ($name == grey or $name == black or $name == charcoal) {
76-
color: sage-color(white);
77-
} @else {
78-
// For colored backgrounds, use the lightest shade (100) or white
79-
color: sage-color($name, 100);
80-
}
81-
} @else {
82-
// Light background → use dark text color
83-
@if ($name == white) {
84-
color: sage-color(grey, 950);
85-
} @else if ($name == black or $name == charcoal) {
86-
color: sage-color(white);
87-
} @else if ($name == grey) {
88-
color: sage-color(grey, 950);
89-
} @else {
90-
// For colored backgrounds, use a darker shade (400+) for contrast
91-
color: sage-color($name, 400);
92-
}
93-
}
94-
95-
&::after {
96-
content: "#{$hex}";
97-
padding-inline-start: sage-spacing(xs);
98-
font-weight: sage-font-weight(semibold);
99-
text-transform: uppercase;
100-
}
101-
}
102-
/* stylelint-enable max-nesting-depth, block-closing-brace-newline-after */
48+
@each $value, $map in $color {
49+
// Use sage-color-hex() for actual hex values (needed for color documentation display)
50+
// This ensures colors are displayed correctly regardless of dark/light mode
51+
$hex: sage-color-hex($name, $value);
52+
53+
// Determine class name for $value == 50
54+
$classname: ".color-#{"" + $name}-#{$value}";
55+
56+
// This should be numeric, not string
57+
@if ($value == 50) {
58+
$classname: ".color-#{"" + $name}-0#{$value}";
59+
}
60+
61+
#{$classname} {
62+
padding: sage-spacing(sm) sage-spacing();
63+
font-size: sage-font-size(body);
64+
background: $hex;
65+
66+
// Simple lightness-based contrast: white text on dark, black text on light
67+
@if (lightness($hex) < 55%) {
68+
color: sage-color-hex(white);
69+
}
70+
@else {
71+
color: sage-color-hex(grey, 950);
72+
}
73+
74+
&::after {
75+
content: "#{$hex}";
76+
padding-inline-start: sage-spacing(xs);
77+
font-weight: sage-font-weight(semibold);
78+
text-transform: uppercase;
10379
}
10480
}
10581
}
10682
}
83+
/* stylelint-enable max-nesting-depth */
10784

10885
[class*="color-"]:only-child {
10986
border-radius: sage-border(radius);
11087
}
11188

11289
.color-white-100 {
11390
border-radius: sage-border(radius);
114-
border: 1px solid sage-color(grey, 800);
115-
box-shadow: inset 0 0 0.0625rem sage-color(grey, 800);
91+
border: 1px solid sage-color-hex(grey, 300);
92+
box-shadow: inset 0 0 0.0625rem sage-color-hex(grey, 300);
11693
}
11794

11895
.color-black-100 {
119-
color: sage-color(white);
96+
color: sage-color-hex(white);
12097
}

0 commit comments

Comments
 (0)