_rfs.less 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // LESS RFS mixin
  2. //
  3. // (This is a custom Less port of the
  4. // [Sass RFS Mixin](https://github.com/twbs/bootstrap/blob/master/scss/vendor/_rfs.scss).)
  5. //
  6. // Automated font-resizing
  7. //
  8. // See https://github.com/twbs/rfs
  9. // Configuration
  10. // Base font size
  11. @rfs-base-font-size: 1.25rem;
  12. @rfs-font-size-unit: rem;
  13. // Breakpoint at where font-size starts decreasing if screen width is smaller
  14. @rfs-breakpoint: 1200px;
  15. @rfs-breakpoint-unit: px;
  16. // Resize font-size based on screen height and width
  17. @rfs-two-dimensional: false;
  18. // Factor of decrease
  19. @rfs-factor: 10;
  20. & when not (isnumber(@rfs-factor)) or (@rfs-factor <= 1) {
  21. error("`@{rfs-factor}` is not a valid @rfs-factor, it must be greater than 1.");
  22. }
  23. // Generate enable or disable classes. Possibilities: false, "enable" or "disable"
  24. @rfs-class: false;
  25. // 1 rem = @rfs-rem-value px
  26. @rfs-rem-value: 16;
  27. // Safari iframe resize bug: https://github.com/twbs/rfs/issues/14
  28. @rfs-safari-iframe-resize-bug-fix: false;
  29. // Disable RFS by setting @enable-responsive-font-sizes to false
  30. // LESS PORT: This variable definition overrides the definition of the same variable in
  31. // `_variables.less`, so commenting here since it’s redundant anyway.
  32. //@enable-responsive-font-sizes: true;
  33. // Cache @rfs-base-font-size unit
  34. @rfs-base-font-size-unit: get-unit(@rfs-base-font-size);
  35. // Remove px-unit from @rfs-base-font-size for calculations
  36. // LESS PORT: Use `@_rfs-base-font-size` here instead of `@rfs-base-font-size` since Less doesn’t
  37. // allow recursive variable definitions.
  38. @_rfs-base-font-size: if((@rfs-base-font-size-unit = rem),
  39. unit((@rfs-base-font-size * @rfs-rem-value)),
  40. unit(@rfs-base-font-size)
  41. );
  42. // Cache @rfs-breakpoint unit to prevent multiple calls
  43. @rfs-breakpoint-unit-cache: get-unit(@rfs-breakpoint);
  44. // Remove unit from @rfs-breakpoint for calculations
  45. // LESS PORT: Use `@_rfs-breakpoint` here instead of `@rfs-breakpoint` since Less doesn’t
  46. // allow recursive variable definitions.
  47. @_rfs-breakpoint: if((@rfs-breakpoint-unit = rem),
  48. unit((@rfs-breakpoint * @rfs-rem-value)),
  49. unit(@rfs-breakpoint)
  50. );
  51. // Responsive font-size mixin
  52. #rfs(@fs, @important: false) {
  53. // Cache @fs unit
  54. @fs-unit: get-unit(@fs);
  55. // Add !important suffix if needed
  56. @rfs-suffix: if(@important, ~" !important", ~"");
  57. // If @fs isn't a number (like inherit) or @fs has a unit (not px or rem, like 1.5em) or @ is 0, just print the value
  58. & when
  59. not (isnumber(@fs))
  60. or (not (@fs-unit = px) and not (@fs-unit = rem))
  61. or (@fs = 0) {
  62. font-size: ~"@{fs}@{rfs-suffix}";
  63. }
  64. & when
  65. (isnumber(@fs))
  66. and ((@fs-unit = px) or (@fs-unit = rem))
  67. and not (@fs = 0) {
  68. // Remove px-unit from @fs for calculations
  69. // LESS PORT: Use `@_fs` here instead of `@fs` since Less doesn’t allow recursive variable
  70. // definitions.
  71. @_fs: if((@fs-unit = rem), unit((@fs * @rfs-rem-value)), unit(@fs));
  72. // Set default font-size
  73. @rfs-static: if((@rfs-font-size-unit = rem),
  74. %(~"%srem%s", (@_fs / @rfs-rem-value), @rfs-suffix),
  75. %(~"%spx%s", @_fs, @rfs-suffix)
  76. );
  77. & when (not (@rfs-font-size-unit = px) and not (@rfs-font-size-unit = rem)) {
  78. error("`@{rfs-font-size-unit}` is not a valid unit for @rfs-font-size-unit. Use `px` or `rem`.")
  79. }
  80. // Calculate minimum font-size for given font-size
  81. @fs-min: (@_rfs-base-font-size + (@_fs - @_rfs-base-font-size) / @rfs-factor);
  82. // Calculate difference between given font-size and minimum font-size for given font-size
  83. @fs-diff: (@_fs - @fs-min);
  84. // Base font-size formatting
  85. // No need to check if the unit is valid, because we did that before
  86. @min-width: if((@rfs-font-size-unit = rem), unit((@fs-min / @rfs-rem-value), rem), ~"@{fs-min}px");
  87. // If two-dimensional, use smallest of screen width and height
  88. @variable-unit: if(@rfs-two-dimensional, vmin, vw);
  89. // Calculate the variable width between 0 and @_rfs-breakpoint
  90. @variable-width: unit((@fs-diff * 100 / @_rfs-breakpoint), @variable-unit);
  91. // Only add media query if font-size is bigger as the minimum font-size
  92. // If @rfs-factor == 1, no rescaling will take place
  93. @rfs-fluid: if((@_fs > @_rfs-base-font-size) and (@enable-responsive-font-sizes),
  94. // Set the calculated font-size.
  95. calc(@min-width + @variable-width)@rfs-suffix,
  96. null
  97. );
  98. // Rendering
  99. & when (@rfs-fluid = null) {
  100. // Only render static font-size if no fluid font-size is available
  101. font-size: @rfs-static;
  102. }
  103. & when not (@rfs-fluid = null) {
  104. // RFS breakpoint formatting
  105. #mq-value(em) { @return: unit((@_rfs-breakpoint / @rfs-rem-value), @rfs-breakpoint-unit); }
  106. #mq-value(rem) { @return: unit((@_rfs-breakpoint / @rfs-rem-value), @rfs-breakpoint-unit); }
  107. #mq-value(px) { @return: unit(@_rfs-breakpoint, px); }
  108. #mq-value(@_) when (default()) {
  109. error("`@{rfs-breakpoint-unit}` is not a valid unit for @rfs-breakpoint-unit. Use `px`, `em` or `rem`.");
  110. @return: ~"";
  111. }
  112. @mq-value: #mq-value(@rfs-breakpoint-unit)[];
  113. & when (@rfs-class = "disable") {
  114. // Adding an extra class increases specificity,
  115. // which prevents the media query to override the font size
  116. &,
  117. .disable-responsive-font-size &,
  118. &.disable-responsive-font-size {
  119. font-size: @rfs-static;
  120. }
  121. }
  122. & when not (@rfs-class = "disable") {
  123. font-size: @rfs-static;
  124. }
  125. & when (@rfs-two-dimensional) {
  126. @media (max-width: @mq-value), (max-height: @mq-value) {
  127. & when (@rfs-class = "enable") {
  128. .enable-responsive-font-size &,
  129. &.enable-responsive-font-size {
  130. font-size: @rfs-fluid;
  131. }
  132. }
  133. & when not (@rfs-class = "enable") {
  134. font-size: @rfs-fluid;
  135. }
  136. & when (@rfs-safari-iframe-resize-bug-fix) {
  137. min-width: 0vw;
  138. }
  139. }
  140. }
  141. & when not (@rfs-two-dimensional) {
  142. @media (max-width: @mq-value) {
  143. & when (@rfs-class = "enable") {
  144. .enable-responsive-font-size &,
  145. &.enable-responsive-font-size {
  146. font-size: @rfs-fluid;
  147. }
  148. }
  149. & when not (@rfs-class = "enable") {
  150. font-size: @rfs-fluid;
  151. }
  152. & when (@rfs-safari-iframe-resize-bug-fix) {
  153. min-width: 0vw;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. // The font-size & responsive-font-size mixin uses RFS to rescale font sizes
  161. #font-size(@fs, @important: false) {
  162. // LESS PORT: Less doesn’t strip “empty” property values so we have to check for a value first.
  163. & when not (@fs = ~"") {
  164. #rfs(@fs, @important);
  165. }
  166. }
  167. #responsive-font-size(@fs, @important: false) {
  168. #rfs(@fs, @important);
  169. }