_functions.scss 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. @function theme-color-lighter($color, $background: #fff) {
  2. @return mix($color, $background, 10%);
  3. }
  4. @function theme-color-darker($color) {
  5. @return shade-color($color, 10%);
  6. }
  7. //
  8. // Replace all occurrences of a substring within a string.
  9. //
  10. @function str-replace($string, $search, $replace: "") {
  11. $index: str-index($string, $search);
  12. @if $index {
  13. @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  14. }
  15. @return $string;
  16. }
  17. @mixin media-breakpoint-down-than($name, $breakpoints: $grid-breakpoints) {
  18. $prev: breakpoint-prev($name);
  19. @if $prev == null {
  20. @content;
  21. } @else {
  22. $max: breakpoint-max($prev, $breakpoints);
  23. @if $max {
  24. @media (max-width: $max) {
  25. @content;
  26. }
  27. } @else {
  28. @content;
  29. }
  30. }
  31. }
  32. @function breakpoint-prev($name, $breakpoints: $grid-breakpoints, $breakpoint-names: map-keys($breakpoints)) {
  33. $n: index($breakpoint-names, $name);
  34. @if not $n {
  35. @error "breakpoint `#{$name}` not found in `#{$breakpoints}`";
  36. }
  37. @return if($n > 1, nth($breakpoint-names, $n - 1), null);
  38. }
  39. //
  40. // Escape SVG strings.
  41. //
  42. @function escape-svg($string) {
  43. @if str-index($string, "data:image/svg+xml") {
  44. @each $char, $encoded in $escaped-characters {
  45. // Do not escape the url brackets
  46. @if str-index($string, "url(") == 1 {
  47. $string: url("#{str-replace(str-slice($string, 6, -3), $char, $encoded)}");
  48. } @else {
  49. $string: str-replace($string, $char, $encoded);
  50. }
  51. }
  52. }
  53. @return $string;
  54. }
  55. /**
  56. * Converts a given value to a percentage string.
  57. *
  58. * @param {Number} $value - The value to be converted to a percentage.
  59. * @return {String} - The percentage representation of the value.
  60. */
  61. @function to-percentage($value) {
  62. @return if(unitless($value), percentage($value), $value);
  63. }
  64. /**
  65. * Generates a transparent version of the given color.
  66. *
  67. * @param {Color} $color - The base color to be made transparent.
  68. * @param {Number} $alpha - The level of transparency, ranging from 0 (fully transparent) to 1 (fully opaque). Default is 1.
  69. * @return {Color} - The resulting color with the specified transparency.
  70. */
  71. @function color-transparent($color, $alpha: 1, $background: transparent) {
  72. @if $alpha == 1 {
  73. @return $color;
  74. } @else {
  75. @return color-mix(in srgb, #{$color} #{to-percentage($alpha)}, $background);
  76. }
  77. }
  78. @function url-svg($svg) {
  79. $svg: str-replace($svg, '#', '%23');
  80. $svg: str-replace($svg, '<svg', '<svg xmlns="http://www.w3.org/2000/svg"');
  81. @return url('data:image/svg+xml;charset=UTF-8,#{$svg}');
  82. }