Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe that injecting SASS variables via CSS <code>content</code> property is a very hackish way to do things.</p> <p>Instead, you can store the variables in a separate location and have them read both by SASS and JS.</p> <p>First, store a list of breakpoints in a <code>breakpoints.json</code> file: </p> <pre><code>["0", "300px", "500px", "700px", "900px", "1100px"] </code></pre> <p>Then use Ruby to read this JSON file and make its contents available as a SASS list via a SASS function. Put this into your Compass <code>config.rb</code>: </p> <pre><code>sass_options = { :custom =&gt; {'breakpoint_file' =&gt; 'breakpoints.json'} } # This creates a SASS function debug() that returns $debug into SASS module Sass::Script::Functions def breakpoints # Reading an array of breakpoints into a file unless breakpoints_array_raw = JSON.load( IO.read( options[:custom]['breakpoint_file'] )) raise Sass::SyntaxError.new("Error: Breakpoints file '#{options[:custom]['breakpoint_file']}' does not exist.") end # Converting strings in the array to SASS String literals breakpoints_array_sassy = breakpoints_array_raw.map { |s| Sass::Script::String.new(s) } # Returning the list into SASS Sass::Script::List.new( breakpoints_array_sassy, :space ) end end </code></pre> <p>In your SASS code, read breakpoints like this:</p> <pre><code>$breakpoints: breakpoints() </code></pre> <p>In JS, use jQuery's <code>.get</code> method to request the JSON file like this: </p> <pre><code>var breakpoints = [], requestBreakpoints = $.get('breakpoints.json'); requestBreakpoints.done(function (response, textStatus, jqXHR){ breakpoints = response; // You might want to remove "px" here }); </code></pre> <p>When i was assembling this setup, I found an existing solution <a href="https://gist.github.com/jackie/5976398" rel="nofollow">here</a>, but i decided to reimplement it using my favorite SASS tools: <a href="https://github.com/Team-Sass/Singularity" rel="nofollow">Singularity</a> and <a href="https://github.com/lolmaus/breakpoint-slicer" rel="nofollow">Breakpoint Slicer</a>.</p> <p>For your convenience, i've built a proof-of-concept <a href="https://github.com/lolmaus/sharing-variables-between-sass-and-js" rel="nofollow">GitHub project</a> with everything set up nicely, featuring some ugly JS code. :)</p> <p>And here's a <a href="http://lolmaus.github.io/sharing-variables-between-sass-and-js/" rel="nofollow">live demo</a>!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload