Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rfs.styl « stylus - github.com/twbs/rfs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 88419c2577460ea5e6c1d771b900435998bfd496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// stylelint-disable declaration-property-value-blacklist

// Stylus RFS mixin
//
// Automated font-resizing
//
// See https://github.com/project-rfs/rfs

// Configuration

// Minimum font size
$rfs-minimum-font-size ?= 1rem
$rfs-font-size-unit ?= rem

// Breakpoint at where font-size starts decreasing if screen width is smaller
$rfs-breakpoint ?= 1200px
$rfs-breakpoint-unit ?= px

// Resize font-size based on screen height and width
$rfs-two-dimensional ?= false

// Factor of decrease
$rfs-factor ?= 5

// Generate enable or disable classes. Possibilities: false, "enable" or "disable"
$rfs-classes ?= false

// 1 rem = $rfs-rem-value px
$rfs-rem-value ?= 16

// Safari iframe resize bug: https://github.com/project-rfs/rfs/issues/14
$rfs-safari-iframe-resize-bug-fix ?= false

// Disable RFS by setting $enable-responsive-font-sizes to false
$enable-responsive-font-sizes ?= true

if $enable-responsive-font-sizes == false
  // If $rfs-factor is set to 1, fluid font-resizing is disabled
  $rfs-factor = 1

// Remove px-unit from $rfs-minimum-font-size for calculations
if unit($rfs-minimum-font-size) == "px"
  $rfs-minimum-font-size = unit($rfs-minimum-font-size, "")
else if unit($rfs-minimum-font-size) == "rem"
  $rfs-minimum-font-size = unit($rfs-minimum-font-size * $rfs-rem-value, "")

// Remove unit from $rfs-breakpoint for calculations
if unit($rfs-breakpoint) == "px"
  $rfs-breakpoint = unit($rfs-breakpoint, "")
else if unit($rfs-breakpoint) == "rem" or unit($rfs-breakpoint) == "em"
  $rfs-breakpoint = unit($rfs-breakpoint * $rfs-rem-value, "")

// Responsive font-size mixin
rfs($fs, $important = false)
  $rfs-suffix = "%s" % ()

  // Add !important suffix if needed
  if $important
    $rfs-suffix = !important

  // 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
  if type-of($fs) != "unit" or (unit($fs) != "px" and unit($fs) != "rem") or $fs == 0
    font-size: "%s%s" % ($fs  $rfs-suffix)
  else
    // Variables for storing static and fluid rescaling
    $rfs-static = null
    $rfs-fluid = null

    // Remove px-unit from $fs for calculations
    if unit($fs) == px
      $fs = unit($fs, "")
    else if unit($fs) == rem
      $fs = unit($fs * $rfs-rem-value, "")

    // Set default font-size.
    if $rfs-font-size-unit == rem
      $rfs-static = "%s%s" % (unit(($fs / $rfs-rem-value), rem)  $rfs-suffix)
    else if $rfs-font-size-unit == px
      $rfs-static = "%s%s" % (unit($fs, px)  $rfs-suffix)
    else
      error("`$rfs-font-size-unit` is not a valid unit for $rfs-font-size-unit. Use `px` or `rem`.")

    if type-of($rfs-factor) != "unit" or $rfs-factor < 1
      error("`$rfs-factor` is not a valid  $rfs-factor, it must be greater or equal to 1.")

    // Only add media query if font-size is bigger as the minimum font-size
    // If $rfs-factor == 1, no rescaling will take place
    if $fs > $rfs-minimum-font-size and $rfs-factor != 1
      $min-width = null
      $variable-unit = null

      // Calculate minimum font-size for given font-size
      $fs-min = $rfs-minimum-font-size + ($fs - $rfs-minimum-font-size) / $rfs-factor

      // Calculate difference between given font-size and minimum font-size for given font-size
      $fs-diff = $fs - $fs-min

      // Minimum font-size formatting
      // No need to check if the unit is valid, because we did that before
      if $rfs-font-size-unit == rem
        $min-width = unit($fs-min / $rfs-rem-value, rem)
      else
        $min-width = unit($fs-min, px)

      // If two-dimensional, use smallest of screen width and height
      if $rfs-two-dimensional
        $variable-unit = vmin
      else
        $variable-unit = vw

      // Calculate the variable width between 0 and $rfs-breakpoint
      $variable-width = "%s%s" % ($fs-diff * 100 / $rfs-breakpoint $variable-unit)

      // Set the calculated font-size
      $rfs-fluid = "calc(%s + %s)%s" % ($min-width $variable-width $rfs-suffix)

    // Rendering
    if $rfs-fluid == null
      // Only render static font-size if no fluid font-size is available
      font-size: $rfs-static
    else
      $mq-value = null

      // RFS breakpoint formatting
      if $rfs-breakpoint-unit == em or $rfs-breakpoint-unit == rem
        $mq-value = unit($rfs-breakpoint / $rfs-rem-value, $rfs-breakpoint-unit)
      else if $rfs-breakpoint-unit == px
        $mq-value = unit($rfs-breakpoint, px)
      else
        error("`$rfs-breakpoint-unit` is not a valid unit for $rfs-breakpoint-unit. Use `px`, `em` or `rem`.")

      if $rfs-classes == "disable"
        // Adding an extra class increases specificity,
        // which prevents the media query to override the font size
        &,
        .disable-responsive-font-size &,
        &.disable-responsive-font-size
          font-size: $rfs-static
      else
        font-size: $rfs-static

      if $rfs-two-dimensional
        @media (max-width: $mq-value), (max-height: $mq-value)
          if $rfs-classes == "enable"
            .enable-responsive-font-size &,
            &.enable-responsive-font-size
              font-size: $rfs-fluid
          else
            font-size: $rfs-fluid

          if $rfs-safari-iframe-resize-bug-fix
            // stylelint-disable-next-line length-zero-no-unit
            min-width: 0vw
      else
        @media (max-width: $mq-value)
          if $rfs-classes == "enable"
            .enable-responsive-font-size &,
            &.enable-responsive-font-size
              font-size: $rfs-fluid
          else
            font-size: $rfs-fluid

          if $rfs-safari-iframe-resize-bug-fix
            // stylelint-disable-next-line length-zero-no-unit
            min-width: 0vw

// The responsive-font-size mixin uses RFS to rescale font sizes
responsive-font-size($fs, $important = false)
  rfs($fs, $important)