|
6 | 6 | @use 'webcore.config.scss' as *; |
7 | 7 | @use './color-palette.scss' as *; |
8 | 8 | @use './css-values.scss' as *; |
| 9 | +@use './functions.scss' as *; |
9 | 10 | @use './layout.scss' as *; |
10 | 11 | @use './typography.scss' as *; |
11 | 12 | @use './variables.scss' as *; |
|
406 | 407 | } |
407 | 408 | } |
408 | 409 | } |
| 410 | + |
| 411 | +@mixin validate-contrast($themeName, $tokens, $level, $print) { |
| 412 | + $required: wcag-required($level); |
| 413 | + $pairs: ( |
| 414 | + primary: (bg: primary-70, fg: primary), |
| 415 | + secondary: (bg: primary-70, fg: primary-10), |
| 416 | + muted: (bg: primary-70, fg: primary-20), |
| 417 | + disabled: (bg: primary-70, fg: primary-30), |
| 418 | + info: (bg: info, fg: info-fg), |
| 419 | + success: (bg: success, fg: success-fg), |
| 420 | + warning: (bg: warning, fg: warning-fg), |
| 421 | + alert: (bg: alert, fg: alert-fg) |
| 422 | + ); |
| 423 | + |
| 424 | + @each $name, $pair in $pairs { |
| 425 | + $bg: map.get($tokens, map.get($pair, bg)); |
| 426 | + $fg: map.get($tokens, map.get($pair, fg)); |
| 427 | + |
| 428 | + $ratio: contrast($fg, $bg); |
| 429 | + |
| 430 | + $logName: '#{$themeName} #{$name}'; |
| 431 | + |
| 432 | + @if not $print { |
| 433 | + $logName: '#{map.get($pair, fg)} against #{map.get($pair, bg)} in theme "#{$themeName}"'; |
| 434 | + } |
| 435 | + |
| 436 | + @if $ratio < $required { |
| 437 | + @if $print { |
| 438 | + contrast: '[#{$logName}] - FAILED: actual: #{$ratio}, required: #{$required} (fg: #{$fg}, bg: #{$bg})'; |
| 439 | + } @else { |
| 440 | + @warn '#{$logName} is below WCAG #{$level} (actual: #{$ratio}, required: #{$required})'; |
| 441 | + } |
| 442 | + } @else { |
| 443 | + @if $print { |
| 444 | + contrast: '[#{$logName}] - PASSED'; |
| 445 | + } |
| 446 | + } |
| 447 | + } |
| 448 | +} |
| 449 | + |
| 450 | +@mixin validate-tokens($referenceMap, $themeName, $tokens) { |
| 451 | + $referenceKeys: map.keys($referenceMap); |
| 452 | + $themeKeys: map.keys($tokens); |
| 453 | + |
| 454 | + @each $key in $referenceKeys { |
| 455 | + @if not list.index($themeKeys, $key) { |
| 456 | + @warn 'Theme `#{$themeName}` is missing key `#{$key}`.'; |
| 457 | + } |
| 458 | + } |
| 459 | + |
| 460 | + @each $key in $themeKeys { |
| 461 | + @if not list.index($referenceKeys, $key) { |
| 462 | + @warn 'Theme `#{$themeName}` has unknown key `#{$key}`.'; |
| 463 | + } |
| 464 | + } |
| 465 | +} |
| 466 | + |
| 467 | +@mixin validate-theme($themes, $selectedTheme, $level, $print: false) { |
| 468 | + $referenceName: list.nth(map.keys($themes), 1); |
| 469 | + $referenceMap: map.get($themes, $referenceName); |
| 470 | + $tokens: map.get($themes, $selectedTheme); |
| 471 | + |
| 472 | + @include validate-contrast($selectedTheme, $tokens, $level, $print); |
| 473 | + @include validate-tokens($referenceMap, $selectedTheme, $tokens); |
| 474 | +} |
0 commit comments