Skip to content

tomhodgins/slinky

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slinky

Add JavaScript tests to <link> tags to toggle stylesheets

Similar to how you can add media queries to <link> tags with the media="" attribute, Slinky lets you define a custom data-selector="" and data-test="" attributes to any <link> tag to define an element to watch, and a JavaScript test to evaluate from the context of the matching element.

A simple usage might toggle the display of a stylesheet when the #sidebar element is wider than 500px:

<link
  rel="stylesheet"
  href="sidebar.css"
  media="none"
  data-selector="#sidebar"
  data-test="this.offsetWidth > 500"
>

By default Slinky will add event listeners for window.load, window.resize, window.input and window.click, but you can override this by optionally specifying a data-event="" attribute as well. This stylesheet would listen to window.scroll alone:

<link
  rel="stylesheet"
  href="scroll.css"
  media="none"
  data-selector="body"
  data-test="this.scrollTop > innerHeight"
  data-event="scroll"
>

Note that in these examples I've included media="none" so the stylesheets default to being hidden - for progressive enhancement if you wish to default to the stylesheets applying unless they need to be hidden by JS, you can leave the media="none" off and the stylesheet will load by default, instead of remain hidden by default.

Links