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 🙏

© 2024 – Pkg Stats / Ryan Hefner

testing-responsively

v1.0.36

Published

Easily adjust components with responsive, mobile-frist approach in mind.

Downloads

9

Readme

Responsively

Responsively is created with the intention to easily adjust components with a responsive, mobile-first approach in mind; it may resemble single purpose classes (e.g. .mt-0, or .mt-0--mobile).

This library plays nicely with libraries such 👩‍🎤 Emotion and 💅 Styled Components since the code output is string.

Table of contents

Usage

This library adjust the value of the passed property according to passed breakpoints. Breakpoints can be defined on a component/page basis, but it can also be defined across the whole app, depending on one's needs.

Simple flow

  1. Configure breakpoints
  2. Implement responsively in one of the following ways:
    • Pass props to component with supported functions
    • Call the function with desired property and its responsive values via styles, or
    • Pass property and values props to the component for single purpose usage
  3. Done 😇

1. Configure breakpoints

/**
 * Configure breakpoints in any file which is being called
 * on the top aplication level (e.g. App.js, utils.js, etc.)
 */
import Responsively from 'responsively';

Responsively.configureBreakpoints([480, 768, 990, 1170]);

2. Implementation

Option 1: Pass props to component with supported functions

With this handy approach we can integrate responsively functions which would provide an option to pass multiple responsive props to component. Check the documentation for all available functions.

import React from 'react';
import styled, { css } from 'react-emotion';
import { background, text } from 'responsively';

const SectionStyles = styled('section')(
  ...background,
  ...text,
);

/**
 * Section component
 */
const Section = props => <SectionStyles {...props} />;

export default Section;

then, define as many properties as you need:

import Section from '../ui';

...

<Section
 background={['#f9eeef', '#f5cbce', '#f2afb4', '#ef717c', '#f74b59']}
 color={['black', 'white']}
 textTransform={['uppercase']}
>
 <h1>Section Heading</h1>
</Section>

Option 2: Call the function with desired property and its responsive values in style declaration

import React from 'react';
import { css } from '@emotion/core';
import Responsively from 'responsively';

const baseStyles = css`${Responsively.responsive('margin-top')([10, 20, 30, 40, 50])};`;

// or, add responsively directly to the component

<Component className={css`${Responsively.responsive('margin-top')([10, 20, 30, 40, 50])};`} />

Option 3: Pass property and values props to the component for single purpose usage

import React from 'react';
import PropTypes from 'prop-types';
import styled, { css } from 'react-emotion';
import Responsively from 'responsively';

const baseStyles = ({ property, values }) =>
  css`
    ${Responsively.responsive(property)(values)};
  `;

const SectionStyles = styled('div')(baseStyles);

/**
 * Section component
 */
const Section = ({ property, values }) => <SectionStyles property={property} values={values} {...props} />;

Section.propTypes = {
  property: PropTypes.string.isRequired,
  values: PropTypes.array.isRequired,
};

export default Section;

then, define its property and values as props:

import Section from '../ui';

...

<Section property="margin" values={[10, 20, 30, 40, 50]}>
  <h1>Section Heading</h1>
</Section>

Try It Out

Try the examples on CodeSandbox:

Install

npm i responsively -S

or,

yarn add responsively

API

Responsively.responsive([breakpointsArray])(propertyName)([propertyArrayValues]);

Arguments

  • breakpointsArray — An array of breakpoint values
  • propertyName — CSS property name
  • propertyArrayValues — An array of values which maps to provided breakpoints. By default, the first value is applied without a media query, while the rest of the values are applied as children of media query selectors.

breakpointsArray and propertyArrayValues values

  • Support all unit kinds (px, em, rem, vw, vh, %, etc.). If a unitless value is passed, it will automatically be converted to px. CSS properties with unitless values are processed as is (e.g. line-height: 1.5; or flex: 1;).
Responsively.responsive('margin-top')([10, 20, 30, '4em', '5rem']);

will yield:

margin-top: 10px;
@media screen and (min-width: 480px) {
  margin-top: 20px;
}
@media screen and (min-width: 768px) {
  margin-top: 30px;
}
@media screen and (min-width: 990px) {
  margin-top: 4em;
}
@media screen and (min-width: 1170px) {
  margin-top: 5rem;
}
  • Clean output The number of generated breakpoints depends on the number of passed property values.
const { responsive } = Responsively;

responsive('margin')([10]);
responsive('padding')([10, 20]);

will yield:

margin: 10px;
padding: 10px;
@media screen and (min-width: 480px) {
  margin-top: 20px;
}
  • Shorthand values per breakpoint
Responsively.responsive('margin')(['10px 20px']);

will yield:

margin: 10px 20px;
  • Correction for common typos
const { responsive } = Responsively;

Responsively.responsive('margin')(['10 20']);
Responsively.responsive('padding')(['10px']);

will yield:

margin: 10px 20px;
padding: 10px;

Warning message

In case one passed more property values then there are breakpoint values, responsively provides the warning in the browser's console.

Responsively.configureBreakpoints([480, 768, 990, 1170]);

Responsively.responsive('padding')([10, 20, 30, 40, 50, 60]); // first argument is `default value`

will compile the code, but the last value will be nested in undefined breakpoint:

...

@media screen and (max-width: undefined) {
  padding: 60px;
}

Console warning message:

responsively-warning

Skipping breakpoints

If particular breakpoint needs to be skipped, undefined value should be passed:

Responsively.configureBreakpoints([480, 768, 990, 1170]);
Responsively.responsive('padding')([undefined, 20, undefined, undefined, 60]);

will yield:

...

@media screen and (max-width: 480px) {
  padding: 20px;
}
@media screen and (max-width: 1170px) {
  padding: 60px;
}

TODO

  • [ ] Merge exact shorthand values, e.g. '10 10' → 10px
  • [ ] Add shorthand properties, e.g. m → margin, or p → padding
  • [ ]

Suggestions?

Shoot me an email, or submit an issue 🚀