npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

reactive-styles

v0.1.6

Published

Utils for creating responsive web apps with JavaScript stylesheets

Readme

Reactive Styles

Device Group

The DeviceGroup class represents the upper and lower size boundries for a group of devices such as phones or tablets. Creating a DeviceGroup is simple:

const {DeviceGroup} = require('reactive-styles');
const DeviceGroups = {
    mobile: new DeviceGroup('mobile', {maxWidth: 749}),
    tablet: new DeviceGroup('tablet', {minWidth: 650, maxWidth: 991})
}

Device

The Device class represents a real world device with specified screen size. The device object creates and listens window.matchMedia queries for the height and width of the device screen. It also creates and listens to queries with the specifiece height and width reversed to determine if a device is in landscape orientation so there is no need to create device objects for both portrait and landscape orientations. To define Landscape specific styles in a StyleSheet define another set of styles with 'Landscape' appended to the name of the device. (See StyleSheet section for example)

const {Device} = require('reactive-styles');
const DeviceGroups = require('reactive-styles').DefaultSizes.DeviceGroups;
let galaxyS5 = new Device('galaxyS5', DeviceGroups.mobile, 360, 640);

Reactive Media

The reactive media class is a util that essentially wraps window.matchMedia to provide a bit of extra functionality to make responding to changes in window size or orientation easier.


const {ReactiveMedia, DefaultSizes} = require('reactive-styles');
let reactiveMedia =  new ReactiveMedia(DefaultSizes.Devices, DefaultSizes.DeviceGroups)

Adding or removing groups and devices is easy


// add new group and device
let mobile = new DeviceGroup('mobile', {maxWidth: 749});
let galaxyS5 = new Device('galaxyS5', DeviceGroups.mobile, 360, 640);

reactiveMedia.addGroup(mobile);
reactiveMedia.addDevice(galaxyS5);

//remove group and device
reactiveMedia.removeGroup(mobile);
reactiveMedia.removeDevice(galaxyS5);

Getting currently matched media


let media = reactiveMedia.getMatchedMedia();
/*
media: {
    matches: [],   //an array of Device objects that match the current window size
    group: {}      // a DeviceGroup object that matches the current window size
}
 */

Add a listener to handle window size changes

let listener = {handleMediaChange: (media)=> {
    //do something
}}
reactiveMedia.addListener(listener)

Creating StyleSheets

StyleSheets are defined as nested JavaScript objects with the top level properties representing the device-groups and a default set of styles. Each device group can have multiple devices defined containing their own set of styles as well as a default set of styles specific to that device-group. When styles are computed for a device the device specific styles, default styles and device-group default styles are all combined with more specific matches overriding any duplicate styles (device > device-group-default > default) This behavior makes it possible to define common styles in one place while allowing styles to be added or overridden for specific devices that need to be handled differently.

{
    deviceGroup: {
        specificDevice: {
            styles:{}
        },
        default: {
            styles: {}
        }
    },
    default: {
        styles: {}
    }
}

Example StyleSheet


const ExampleStyles = {
    desktop:{
        default: {
            title:{
                color: 'blue'
            },
        }
    },
    phone:{
        galaxyS5:{
            title: {
                color: 'yellow'
            },
            subtitle: {
                color: 'yellow'
            }
        },
        galaxyS5Landscape:{
            title: {
                color: 'yellow',
                width: '80%'
            },
            subtitle: {
                color: 'yellow',
                width: '80%'
            }
        },
        default: {
            title: {
                width: '100%'
            },
            subtitle: {
                width: '100%'
            }
        }
    },
    default: {
        title: {
            color: 'red',
            width: '50%'
        },
        subtitle: {
            color: 'red',
            width: '50%'
        }
    }
}

module.exports = new StyleSheet(ExampleStyles);

Get Styles for Current Media

const exampleStyles = require('./ExampleStyles');
let styles = exampleStyles.getStyles(reactiveMedia.getMatchedMedia());

styles (desktop)

{
    title:{
        color: 'blue',
        width: '50%'
    },
    subtitle: {
        color: 'red',
        width: '50%'
    }
}

styles (galaxyS5)

{
    title: {
        color: 'yellow',
        width: '100%'
    },
    subtitle: {
        color: 'yellow',
        width: '100%'
    }    
}

styles (galaxyS5Landscape)

{
    title: {
        color: 'yellow',
        width: '80%'
    },
    subtitle: {
        color: 'yellow',
        width: '80%'
    }    
}

styles (mobile: no matching device)

{
    title: {
       width: '100%',
       color: 'red'
    },
    subtitle: {
       width: '100%',
       color: 'red'
    } 
}

Using Reactive Styles with React Components


class SomeComponent extends React.Component {
    constructor(props){
        super(props)
        this.state = {styles: this.props.styleSheet.getStyles(this.props.reactiveMedia.getMatchedMedia())}
    }
    
    componentDidMount(){
        this.props.reactiveMedia.addListener(this)
    }
    
    componentWillUnmount(){
        this.props.reactiveMedia.removeListener(this)
    }
    
    handleMediaChange(media){
        this.setState({styles: this.props.styleSheet.getStyles(media)});
    }
    
    render(){
        return (
            <div>
                <h1 style={this.state.styles.title}>Styled Title</h1>
                <h2 style={this.state.styles.subtitle}>Styled Sub-Title</h2>
            </div>
        )
    }
}