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

functional-styles

v0.0.1

Published

replace CSS with functional style modules

Downloads

1

Readme

Functional Styles

Create functional ES2015 based style modules as an alternative to CSS. Putting component's style logic into vanilla JS modules allows them to be shared among different ecosystems (e.g. React, Angular or vanilla JS).

Style props can be functions:

const elementStyle = {
  color: 'red',
  padding: ({ padding = 2 }) => padding + 5
  backgroundColor: ({ bgColor = 'transparent' }) => bgColor
}

export default elementStyle

Render with toStyle

Styles can be updated at render time using a vars object:

import { toStyle } from 'functional-styles'
import elementStyle from './elementStyle'

const vars = {
  padding: 5,
  bgColor: '#fee'
}

toStyle(elementStyle, vars)

// {
//   color: 'red',
//   padding: 7,
//   backgroundColor: '#fee'
// }

Installation

npm install --save functional-styles

Vars

Think of vars as being like Bootstrap's Less variables. They can be used for:

  • key colors
  • white space
  • fonts
  • margins and padding

Vars objects can be stored in a database or a module:

// ./myTheme

const myTheme = {
  spacing: 5,
  mainFont: 'Arial',
  keyColor: '#A2B51A'
}

export default myTheme

Functions

Props which have style functions as values use ES2015 default values to make sure they render without errors when vars are not supplied.

(keyColor = 'green') => keyColor

Component Style Modules

Functional style objects can be nested to provide all the styles needed for multiple elements in a component in their various states:


import {toStyle} from 'functional-styles'
import myTheme from './myTheme'

// convention export `vars` to help with tooling
// maybe in future export `varTypes` too
export const vars = {
  spacing: ({spacing = 10}) => spacing,
  mainFont: (mainFont = 'Arial') => mainFont,
  keyColor: ({keyColor: 'orange'}) => keyColor
}

// you may want to export style blocks too
// to allow others to recompose styles
const base = {
  display: 'inline-block',
  paddingLeft: vars.spacing,
  paddingRight: vars.spacing,
  fontFamily: vars.mainFont,
  backgroundColor: '#ccc'
}

const selected = {
  backgroundColor: vars.keyColor
}

//use object spread, Object.assign or merge for cascade
const buttonStyle = {
  base,
  selected: {...base, ...selected}
}

export default buttonStyle

toStyle(buttonStyle, myTheme)

// {
//   base: {
//     display: 'inline-block',
//     paddingLeft: 5,
//     paddingRight: 5,
//     fontFamily: 'Arial',
//     backgroundColor: '#ccc'
//   },
//   selected: {
//     display: 'inline-block',
//     paddingLeft: 5,
//     paddingRight: 5,
//     fontFamily: 'Arial',
//     backgroundColor: '#A2B51A'
//   },
// }

Merging Styles

vars will only get you so far when it comes to customizing a component. If more power is needed then use deep merges to override props you would like to change:

import { toStyle, merge } from 'functional-styles'

const borderRadiusFn ({ borderRadius: 3 }) => borderRadius

const overrides = {
  base: {
    borderRadiusFn
  },
  selected: {
    borderRadiusFn,
    backgroundColor: 'pink'
  }
}

const myButtonStyle = merge(buttonStyle, overrides)
toStyle(myButtonStyle, myTheme)

// {
//   base: {
//     display: 'inline-block',
//     paddingLeft: 5,
//     paddingRight: 5,
//     fontFamily: 'Arial',
//     backgroundColor: '#ccc',
//     borderRadius: 3
//   },
//   selected: {
//     display: 'inline-block',
//     paddingLeft: 5,
//     paddingRight: 5,
//     fontFamily: 'Arial',
//     backgroundColor: 'pink',
//     borderRadius: 3
//   },
// }

Function Styles Get Involved

Conventions and names are work in progress please submit issues with comments, ideas and JSBins.

Thanks

  • NCR Edinburgh - letting me work on this
  • FormidableLabs - inspiration from Radium, Victory etc