All Projects → shatstack → stylenames

shatstack / stylenames

Licence: MIT license
A simple JavaScript utility for conditionally joining inline styles together

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to stylenames

inline.js
Live CSS Helpers with Javascript
Stars: ✭ 36 (+100%)
Mutual labels:  inline-css, inline-styles
InlineCssParser
A Visual Studio Extension that helps to extract inline styles into a seperate css file.
Stars: ✭ 23 (+27.78%)
Mutual labels:  inline-css, inline-styles
critical-plugin
⚙️ Critical plugin for webpack (https://webpack.js.org/)
Stars: ✭ 17 (-5.56%)
Mutual labels:  inline-css, inline-styles
create-class-names
A utility to extend the values of a classNames object.
Stars: ✭ 16 (-11.11%)
Mutual labels:  classnames, styles
Critical
Extract & Inline Critical-path CSS in HTML pages
Stars: ✭ 9,364 (+51922.22%)
Mutual labels:  inline-css, inline-styles
ta-gallery
You can use the light-weight, responsive and mobile first gallery, carousel, slide show or rotator for images, texts and every kind of content.
Stars: ✭ 87 (+383.33%)
Mutual labels:  alpinejs
Giotto
Theme manager for your app: apply styles to anything you want through a plist file
Stars: ✭ 18 (+0%)
Mutual labels:  styles
react-native-use-styles
A classy approach to manage your react native styles.
Stars: ✭ 66 (+266.67%)
Mutual labels:  styles
CustomWebRadioButton
An example of a make radio-button design on the web.
Stars: ✭ 15 (-16.67%)
Mutual labels:  styles
leeks.js
Simple ANSI styling for your terminal
Stars: ✭ 12 (-33.33%)
Mutual labels:  styles
alter.email
Transform your HTML emails.
Stars: ✭ 66 (+266.67%)
Mutual labels:  inline-css
axiom
Axiom - A Hugo Theme. GitTip: https://gitcoin.co/tip?username=jhauraw
Stars: ✭ 67 (+272.22%)
Mutual labels:  alpinejs
white-cursor
Provides a white I-bar cursor in the Atom editor for use with dark backgrounds
Stars: ✭ 13 (-27.78%)
Mutual labels:  styles
stylez
dark css style overrides for sites i use
Stars: ✭ 38 (+111.11%)
Mutual labels:  styles
CustomWebCheckbox
An example of a make checkbox design on the web.
Stars: ✭ 12 (-33.33%)
Mutual labels:  styles
prop-styles
Utility to create flexible React components which accept props to enable/disable certain styles.
Stars: ✭ 31 (+72.22%)
Mutual labels:  styles
less-mix
LESS-Mix - is a functional, powerful and convenient library LESS-mixins.
Stars: ✭ 22 (+22.22%)
Mutual labels:  styles
tailwind-layouts
Collection of Tailwind Layouts
Stars: ✭ 53 (+194.44%)
Mutual labels:  alpinejs
faven
A web tool to help you generate favicons
Stars: ✭ 126 (+600%)
Mutual labels:  alpinejs
mini-styled
Small subset of styled-components 💅
Stars: ✭ 16 (-11.11%)
Mutual labels:  styles

build npm version npm bundle size (scoped)

@shat/stylenames

A simple JavaScript utility for conditionally joining inline styles together.

This is a fork of the unmaintained stylenames package

What does stylenames do?

In one short sentence: "stylenames converts an object to a css style string."

Think classNames but for inline styles.

Install

From CDN: Add the following script to the end of your <head> section.

<script src="https://cdn.jsdelivr.net/npm/@shat/[email protected]/lib/index.umd.js"></script>

That's it. It will initialize itself as styleNames().

From NPM: Install the package from NPM.

npm install @shat/stylenames

Include it in your script.

import styleNames from '@shat/stylenames';

Quick Start

Standalone:

styleNames({
    backgroundColor: 'red',
    width: '120px',
    
    'height':{
        // If the condition is false the style does not get used.
        '200px': false,
        // Only the first value with true condition is used.
        '300px': true,
        '400px': true
    },
});

With Alpine.js:

<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>

<div x-data="{}">
  <button :style="styleNames({ backgroundColor: 'red', padding: '20px' })">
    A red button
  </button>
</div>

<script src="https://cdn.jsdelivr.net/npm/@shat/[email protected]/lib/index.umd.js"></script>

Examples

Without conditions

let styles1 = styleNames({
    height: '120px',
    width: '100px'
});
console.log(styles1); //--> 'height:120px;width:100px;'

With one condition using a boolean toggle

let styles1 = styleNames({
    height: '120px',
    width: {
        '200px': false
    }
});
console.log(styles1); //--> 'height:120px '

let styles2 = styleNames({
    height: '120px',
    width: {
        '200px': true
    }
});
console.log(styles2); //--> 'height:120px;width:200px;'

With multiple rules with 1 boolean toggle

const styles = styleNames({
    'height:120px;width:100px;': true
});
console.log(styles); //--> 'height:120px;width:100px;'

With camelcased rules

const styles = styleNames({backgroundColor: 'red'});
console.log(styles); //--> 'background-color:red;'

With array input

const styles = styleNames(['height:120px', 'width:100px']);
console.log(styles); //--> 'height:120px;width:100px;'

With more than one condition using a function toggle

let itemCount = 0;

let styleNamesConfig = {
    display: {
        'none': () => itemCount === 0
    },
    height: '120px',
    width: {
        '100px': () => itemCount <= 1,
        '200px': () => itemCount <= 2,
        '400px': () => itemCount <= 4,
        '100%': () => itemCount > 4
    }
};

console.log(styleNames(styleNamesConfig)); //--> 'display:none;height:120px;width:100px;'

itemCount++; //1
console.log(styleNames(styleNamesConfig)); //--> 'height:120px;width:100px;'

itemCount++; //2
console.log(styleNames(styleNamesConfig)); //--> 'height:120px;width:200px;'

itemCount++; //3
console.log(styleNames(styleNamesConfig)); //--> 'height:120px;width:400px;'

itemCount += 12; //15
console.log(styleNames(styleNamesConfig)); //--> 'height:120px;width:100%;'

Contributing

Requirements

  • Node 10
  • Yarn 1.x or npm

Setup

  1. Clone the repository
  2. Run yarn or npm install installs all required dependencies.

npm scripts

Equivalent npm run <script> should also work

  • yarn build will bundle/transpile JavaScript for all targets using microbundle.
  • yarn test will run tests using Jest.
  • yarn lint will lint all of the files with xo
  • yarn format will run lint with --fix option on all the examples files (and tests).

LICENSE

Code is licensed under the MIT License.

Acknowledgments

This package is maintained by Hugo from Code with Hugo and Alpine.js Weekly.

Special thanks to:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].