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

@themed-styling/core

v3.0.4

Published

Quick, modular and responsive styling for styled-components with theme support

Downloads

7

Readme

@themed-styling/core 🎁

Build badge npm version minified size

themed-styling is a collection of functions to help you quickly create highly reusable styled-components. It integrates perfectly with themes and gives you enormous control over the responsiveness of your components. Keeping your breakpoints consistent and grouping them in an easy to understand way.

Edit on CodeSandbox

import React from 'react'
import styled, { ThemeProvider } from 'styled-components'

import {
  background,
  color,
  fontSize,
  margin,
  padding,
} from '@themed-styling/core'

const myTheme = {
  colors: {
    primary: 'DarkSlateBlue',
    mySpecialColor: 'DeepSkyBlue',
  },
  breakpoints: {
    mobile: '375px',
    tablet: '768px',
    desktop: '1024px',
    myCoolBreakpoint: '1440px',
  },
  fontSizes: {
    h1: {
      standard: '1rem',
      mobile: '1.2rem',
      tablet: '1.4rem',
      desktop: '1.6rem',
      myCoolBreakpoint: '2rem',
    },
  },
}

const MyHeading = styled.h1`
  ${background({
    standard: 'colors.primary',
    myCoolBreakpoint: 'colors.mySpecialColor',
  })}
  ${color('white')}
  ${fontSize('fontSizes.h1')}
  ${padding({
    standard: 10,
    mobile: '1.5rem',
    desktop: 35,
  })}
`

const MyParagraph = styled.p`
  ${color()}
  ${fontSize({
    standard: 11,
    mobile: 16,
    tablet: 18,
    desktop: 20,
  })}
  ${margin({
    standard: 10,
    mobile: '1.5rem',
    desktop: 35,
  })}
`

export default () => (
  <ThemeProvider theme={myTheme}>
    <MyHeading>Hello CodeSandbox!</MyHeading>
    <MyParagraph color={'gray'}>
      Please play around with the functions and their values by editing App.js!
      Make sure to resize your window to check out responsiveness.
    </MyParagraph>
  </ThemeProvider>
)
.MyHeading {
  background: DarkSlateBlue;
  color: white;
  font-size: 1rem;
  padding: 10px;

  @media screen and (min-width: 375px) {
    font-size: 1.2rem;
    padding: 1.5rem;
  }

  @media screen and (min-width: 768px) {
    font-size: 1.4rem;
  }

  @media screen and (min-width: 1024px) {
    font-size: 1.6rem;
    padding: 35px;
  }

  @media screen and (min-width: 1440px) {
    font-size: 2rem;
    background: DeepSkyBlue;
  }
}

.MyParagraph {
  color: gray;
  font-size: 11px;
  margin: 10px;

  @media screen and (min-width: 375px) {
    font-size: 16px;
    margin: 1.5rem;
  }

  @media screen and (min-width: 768px) {
    font-size: 18px;
  }

  @media screen and (min-width: 1024px) {
    font-size: 20px;
    margin: 35px;
  }
}

Use a different prop

const MyParagraph = styled.p`
  ${fontSize('1rem').propName('differentProp')}
`

const MyApp = () => <MyParagraph differentProp={'2rem'} /> // font-size: 2rem;

Use shorthand p to do the same (fontSize().propName('differentProp') and fontSize().p('differentProp') are equivalent).

Don't use any prop

const MyParagraph = styled.p`
  ${fontSize('1rem').propless()}
`

const MyApp = () => <MyParagraph fontSize={'2rem'} /> // font-size: 1rem;

Use shorthand l to do the same (fontSize().propless() and fontSize().l() are equivalent).

Add !important

const MyParagraph = styled.p`
  ${fontSize('1rem').important()}
`

const MyApp = () => (
  <>
    <MyParagraph /> // font-size: 1rem !important;
    <MyParagraph fontSize={'2rem'} /> // font-size: 2rem !important;
  </>
)

Use shorthand i to do the same (fontSize().important() and fontSize().i() are equivalent).

calc( a value )

const MyParagraph = styled.p`
  ${fontSize('1rem').calc('*2')}
`

const MyApp = () => (
  <>
    <MyParagraph /> // font-size: calc(1rem * 2);
    <MyParagraph fontSize={'2rem'} /> // font-size: calc(2rem * 2);
  </>
)

Use shorthand c to do the same (fontSize().calc('*2') and fontSize().c('*2') are equivalent).

Do it all!

You can chain these functions to your heart's content. The order doesn't matter.

const MyParagraph = styled.p`
  ${fontSize('1rem').calc('*2').important()}
  ${height('20px').p('myHeight').calc('*4')}
  ${width('100px').i().calc('/3').propless()}
`

const MyApp = () => (
  <>
    /* font-size: calc(1rem * 2) !important; height: calc(20px * 4); width:
    calc(100px / 3) !important; */
    <MyParagraph height={'300px'} width={'400px'} />
    /* font-size: calc(2rem * 2) !important; height: calc(100px * 4); width:
    calc(100px / 3) !important; */
    <MyParagraph fontSize={'2rem'} myHeight={'100px'} width={'400px'} />
  </>
)

Get started

Install with npm

$ npm i @themed-styling/core

and check out the changelog and API reference.

Usage

All functions work the same way. They add a prop with the same name, that stands for a specific CSS property. The function fontSize adds a fontSize prop to your component and sets font-size: <value>; in your component's template literal.

Props and functions are camelCase, as is convention in JS. grid-template-rows becomes gridTemplateRows, for instance.

You can pass a fallback value to the function when you use it, which is used when no prop is set. The prop value takes priority over the fallback value.

You can specify objects or arrays as values, too. If you do this, you need to specify a breakpoints property in your theme. themed-styling tries to match the keys in your value to the keys in your breakpoints. So your breakpoints has to have the same or more keys as the values you use.

The keys in breakpoints are arbitrary. You're only required to use the same keys as in your values. If you want more control, you can specify mediaQueries instead (theme = { mediaQueries: { mobile: '@media screen and (max-width: 300px) and (min-width: 200px)'} }).

Notes

IMPORTANT: Keys are matched in order and media queries are in your component's style in that order. You have to add your values from smallest breakpoint to largest due to how CSS priorities work. Doing it otherwise leads to weird behaviour.

❌:

<MyParagraph
  fontSize={{
    desktop: '4rem',
    myCoolBreakpoint: '6rem',
    tablet: '2rem',
    mobile: '1rem',
  }}
>
  Hello readme!
</MyParagraph>

✔️:

<MyParagraph
  fontSize={{
    mobile: '1rem',
    tablet: '2rem',
    desktop: '4rem',
    myCoolBreakpoint: '6rem',
  }}
>
  Hello readme!
</MyParagraph>