Socket
Socket
Sign inDemoInstall

jss-compose

Package Overview
Dependencies
6
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.0.1

changelog.md

145

dist/jss-compose.js

@@ -55,3 +55,3 @@ (function webpackUniversalModuleDefinition(root, factory) {

/* 0 */
/***/ function(module, exports) {
/***/ function(module, exports, __webpack_require__) {

@@ -64,22 +64,11 @@ 'use strict';

exports.default = jssCompose;
var consoleWarn = console.warn.bind(console); // eslint-disable-line no-console
/**
* Set additional unnamed class.
*
* @param {Object} original rule
* @param {String} compostion class string
* @return {Boolean} flag, indicating function was successfull or not
*/
var setClass = function setClass(rule, composition) {
var container = rule.options.parent;
var _warning = __webpack_require__(1);
rule.className += ' ' + composition;
container.classes[rule.name] = rule.className;
var _warning2 = _interopRequireDefault(_warning);
return true;
};
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Set additional named class.
* Set class name.
*

@@ -90,17 +79,23 @@ * @param {Object} original rule

*/
var setNamedClass = function setNamedClass(rule, composition, warn) {
var refRule = rule.options.sheet.getRule(composition.substr(1));
function setClass(rule, composition) {
if (composition[0] === '$') {
var refRule = rule.options.sheet.getRule(composition.substr(1));
if (!refRule) {
warn('[JSS] A rule tries to compose with non-existant internal selector \r\n' + rule.toString());
return false;
if (!refRule) {
(0, _warning2.default)(false, '[JSS] Referenced rule is not defined. \r\n%s', rule);
return false;
}
if (refRule === rule) {
(0, _warning2.default)(false, '[JSS] Cyclic composition detected. \r\n%s', rule);
return false;
}
setClass(rule, refRule.className);
return true;
}
if (refRule === rule) {
warn('[JSS] A rule tries to compose itself \r\n' + rule.toString());
return false;
}
setClass(rule, refRule.className);
var container = rule.options.parent;
rule.className += ' ' + composition;
container.classes[rule.name] = rule.className;
return true;
};
}

@@ -114,33 +109,87 @@ /**

function jssCompose() {
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
return function (rule) {
var style = rule.style;
var _ref$warn = _ref.warn;
var warn = _ref$warn === undefined ? consoleWarn : _ref$warn;
return function (rule) {
if (!rule.style || !rule.style.composes) return;
if (!style || !style.composes) return;
if (rule.options.named) {
var style = rule.style;
if (typeof style.composes == 'string') {
if (style.composes[0] === '$') {
setNamedClass(rule, style.composes, warn);
} else {
setClass(rule, style.composes);
}
} else if (Array.isArray(style.composes)) {
if (Array.isArray(style.composes)) {
for (var index = 0; index < style.composes.length; index++) {
if (style.composes[index][0] === '$') {
setNamedClass(rule, style.composes[index], warn);
} else {
setClass(rule, style.composes[index]);
}
setClass(rule, style.composes[index]);
}
} else setClass(rule, style.composes);
}
// Remove composes property to prevent infinite loop.
delete style.composes;
};
}
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
/**
* Copyright 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
/**
* Similar to invariant but only logs a warning if the condition is not met.
* This can be used to log issues in development environments in critical
* paths. Removing the logging code for production environments will keep the
* same logic and follow the same code paths.
*/
var warning = function() {};
if (true) {
warning = function(condition, format, args) {
var len = arguments.length;
args = new Array(len > 2 ? len - 2 : 0);
for (var key = 2; key < len; key++) {
args[key - 2] = arguments[key];
}
if (format === undefined) {
throw new Error(
'`warning(condition, format, ...args)` requires a warning ' +
'message argument'
);
}
if (format.length < 10 || (/^[s\W]*$/).test(format)) {
throw new Error(
'The warning format should be able to uniquely identify this ' +
'warning. Please, use a more descriptive format than: ' + format
);
}
if (!condition) {
var argIndex = 0;
var message = 'Warning: ' +
format.replace(/%s/g, function() {
return args[argIndex++];
});
if (typeof console !== 'undefined') {
console.error(message);
}
try {
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch(x) {}
}
// Remove composes property before attaching to prevent infinite call stack.
delete rule.style.composes;
};
}
module.exports = warning;
/***/ }

@@ -147,0 +196,0 @@ /******/ ])

@@ -1,1 +0,1 @@

!function(e,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define([],o):"object"==typeof exports?exports.jssCompose=o():e.jssCompose=o()}(this,function(){return function(e){function o(t){if(s[t])return s[t].exports;var n=s[t]={exports:{},id:t,loaded:!1};return e[t].call(n.exports,n,n.exports,o),n.loaded=!0,n.exports}var s={};return o.m=e,o.c=s,o.p="",o(0)}([function(e,o){"use strict";function s(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0],o=e.warn,s=void 0===o?t:o;return function(e){if(e.style&&e.style.composes){if(e.options.named){var o=e.style;if("string"==typeof o.composes)"$"===o.composes[0]?r(e,o.composes,s):n(e,o.composes);else if(Array.isArray(o.composes))for(var t=0;t<o.composes.length;t++)"$"===o.composes[t][0]?r(e,o.composes[t],s):n(e,o.composes[t])}delete e.style.composes}}}Object.defineProperty(o,"__esModule",{value:!0}),o["default"]=s;var t=console.warn.bind(console),n=function(e,o){var s=e.options.parent;return e.className+=" "+o,s.classes[e.name]=e.className,!0},r=function(e,o,s){var t=e.options.sheet.getRule(o.substr(1));return t?t===e?(s("[JSS] A rule tries to compose itself \r\n"+e.toString()),!1):(n(e,t.className),!0):(s("[JSS] A rule tries to compose with non-existant internal selector \r\n"+e.toString()),!1)}}])});
!function(e,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define([],o):"object"==typeof exports?exports.jssCompose=o():e.jssCompose=o()}(this,function(){return function(e){function o(s){if(t[s])return t[s].exports;var n=t[s]={exports:{},id:s,loaded:!1};return e[s].call(n.exports,n,n.exports,o),n.loaded=!0,n.exports}var t={};return o.m=e,o.c=t,o.p="",o(0)}([function(e,o,t){"use strict";function s(e){return e&&e.__esModule?e:{"default":e}}function n(e,o){if("$"===o[0]){var t=e.options.sheet.getRule(o.substr(1));return t?t===e?((0,i["default"])(!1,"[JSS] Cyclic composition detected. \r\n%s",e),!1):(n(e,t.className),!0):((0,i["default"])(!1,"[JSS] Referenced rule is not defined. \r\n%s",e),!1)}var s=e.options.parent;return e.className+=" "+o,s.classes[e.name]=e.className,!0}function r(){return function(e){var o=e.style;if(o&&o.composes){if(e.options.named)if(Array.isArray(o.composes))for(var t=0;t<o.composes.length;t++)n(e,o.composes[t]);else n(e,o.composes);delete o.composes}}}Object.defineProperty(o,"__esModule",{value:!0}),o["default"]=r;var c=t(1),i=s(c)},function(e,o,t){"use strict";var s=function(){};e.exports=s}])});

@@ -6,8 +6,7 @@ const assign = require('lodash.assign')

config.set({
browsers: ['Firefox'],
// browsers: ['Chrome', 'Firefox', 'Safari'],
browsers: ['Chrome'],
frameworks: ['mocha'],
files: ['tests-src.webpack.js'],
files: ['tests.webpack.js'],
preprocessors: {
'tests-src.webpack.js': ['webpack', 'sourcemap']
'tests.webpack.js': ['webpack', 'sourcemap']
},

@@ -14,0 +13,0 @@ webpack: assign(webpackConfig, {

@@ -7,22 +7,11 @@ 'use strict';

exports.default = jssCompose;
var consoleWarn = console.warn.bind(console); // eslint-disable-line no-console
/**
* Set additional unnamed class.
*
* @param {Object} original rule
* @param {String} compostion class string
* @return {Boolean} flag, indicating function was successfull or not
*/
var setClass = function setClass(rule, composition) {
var container = rule.options.parent;
var _warning = require('warning');
rule.className += ' ' + composition;
container.classes[rule.name] = rule.className;
var _warning2 = _interopRequireDefault(_warning);
return true;
};
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Set additional named class.
* Set class name.
*

@@ -33,17 +22,23 @@ * @param {Object} original rule

*/
var setNamedClass = function setNamedClass(rule, composition, warn) {
var refRule = rule.options.sheet.getRule(composition.substr(1));
function setClass(rule, composition) {
if (composition[0] === '$') {
var refRule = rule.options.sheet.getRule(composition.substr(1));
if (!refRule) {
warn('[JSS] A rule tries to compose with non-existant internal selector \r\n' + rule.toString());
return false;
if (!refRule) {
(0, _warning2.default)(false, '[JSS] Referenced rule is not defined. \r\n%s', rule);
return false;
}
if (refRule === rule) {
(0, _warning2.default)(false, '[JSS] Cyclic composition detected. \r\n%s', rule);
return false;
}
setClass(rule, refRule.className);
return true;
}
if (refRule === rule) {
warn('[JSS] A rule tries to compose itself \r\n' + rule.toString());
return false;
}
setClass(rule, refRule.className);
var container = rule.options.parent;
rule.className += ' ' + composition;
container.classes[rule.name] = rule.className;
return true;
};
}

@@ -57,31 +52,19 @@ /**

function jssCompose() {
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
return function (rule) {
var style = rule.style;
var _ref$warn = _ref.warn;
var warn = _ref$warn === undefined ? consoleWarn : _ref$warn;
return function (rule) {
if (!rule.style || !rule.style.composes) return;
if (!style || !style.composes) return;
if (rule.options.named) {
var style = rule.style;
if (typeof style.composes == 'string') {
if (style.composes[0] === '$') {
setNamedClass(rule, style.composes, warn);
} else {
setClass(rule, style.composes);
}
} else if (Array.isArray(style.composes)) {
if (Array.isArray(style.composes)) {
for (var index = 0; index < style.composes.length; index++) {
if (style.composes[index][0] === '$') {
setNamedClass(rule, style.composes[index], warn);
} else {
setClass(rule, style.composes[index]);
}
setClass(rule, style.composes[index]);
}
}
} else setClass(rule, style.composes);
}
// Remove composes property before attaching to prevent infinite call stack.
delete rule.style.composes;
// Remove composes property to prevent infinite loop.
delete style.composes;
};
}

@@ -7,2 +7,4 @@ 'use strict';

var _jss = require('jss');
var _index = require('./index');

@@ -12,17 +14,20 @@

var _jss = require('jss');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var noWarn = function noWarn(message) {
return (0, _expect2.default)(message).to.be(undefined);
};
describe('jss-compose', function () {
var jss = void 0;
var warning = void 0;
beforeEach(function () {
jss = (0, _jss.create)().use((0, _index2.default)({ warn: noWarn }));
_index2.default.__Rewire__('warning', function (condition, message) {
warning = message;
});
jss = (0, _jss.create)().use((0, _index2.default)());
});
afterEach(function () {
_index2.default.__ResetDependency__('warning');
warning = undefined;
});
describe('Unnamed rules', function () {

@@ -43,2 +48,6 @@ var sheet = void 0;

afterEach(function () {
(0, _expect2.default)(warning).to.be(undefined);
});
it('should add rules', function () {

@@ -73,2 +82,6 @@ (0, _expect2.default)(sheet.getRule('a')).to.not.be(undefined);

afterEach(function () {
(0, _expect2.default)(warning).to.be(undefined);
});
it('should add rules', function () {

@@ -88,3 +101,3 @@ (0, _expect2.default)(sheet.getRule('a')).to.not.be(undefined);

describe('Named selector composed with unnamed one', function () {
describe('Named selector composed with an unnamed one', function () {
var sheet = void 0;

@@ -101,2 +114,6 @@

afterEach(function () {
(0, _expect2.default)(warning).to.be(undefined);
});
it('should add rule', function () {

@@ -136,2 +153,6 @@ (0, _expect2.default)(sheet.getRule('a')).to.not.be(undefined);

afterEach(function () {
(0, _expect2.default)(warning).to.be(undefined);
});
it('should add rules', function () {

@@ -168,2 +189,6 @@ (0, _expect2.default)(sheet.getRule('a')).to.not.be(undefined);

afterEach(function () {
(0, _expect2.default)(warning).to.be(undefined);
});
it('should add rules', function () {

@@ -202,2 +227,6 @@ (0, _expect2.default)(sheet.getRule('a')).to.not.be(undefined);

afterEach(function () {
(0, _expect2.default)(warning).to.be(undefined);
});
it('should add rules', function () {

@@ -220,18 +249,4 @@ (0, _expect2.default)(sheet.getRule('a')).to.not.be(undefined);

describe('Warnings', function () {
var localJss = void 0;
var warning = void 0;
beforeEach(function () {
var warn = function warn(message) {
warning = message;
};
localJss = (0, _jss.create)().use((0, _index2.default)({ warn: warn }));
});
afterEach(function () {
warning = null;
});
it('should warn when rule try to compose itself', function () {
localJss.createStyleSheet({
jss.createStyleSheet({
a: {

@@ -242,7 +257,7 @@ composes: ['$a'],

});
(0, _expect2.default)(warning).to.be('[JSS] A rule tries to compose itself \r\n.a-3512327961 {\n composes: $a;\n color: red;\n}'); // eslint-disable-line max-len
(0, _expect2.default)(warning).to.be('[JSS] Cyclic composition detected. \r\n%s');
});
it('should warn when try to compose non-existant named selector', function () {
localJss.createStyleSheet({
jss.createStyleSheet({
a: {

@@ -253,5 +268,5 @@ composes: ['$b'],

});
(0, _expect2.default)(warning).to.be('[JSS] A rule tries to compose with non-existant internal selector \r\n.a-1790758707 {\n composes: $b;\n color: red;\n}'); // eslint-disable-line max-len
(0, _expect2.default)(warning).to.be('[JSS] Referenced rule is not defined. \r\n%s');
});
});
});
}); /* eslint-disable no-underscore-dangle */
{
"name": "jss-compose",
"description": "JSS plugin that enables selector composition",
"version": "1.0.0",
"description": "JSS plugin for classes composition",
"version": "1.0.1",
"author": {

@@ -31,4 +31,4 @@ "name": "Pavel Davydov",

"lint:staged": "lint-staged",
"test": "karma start --single-run ",
"test:watch": "karma start",
"test": "cross-env NODE_ENV=test karma start --single-run ",
"test:watch": "cross-env NODE_ENV=test karma start",
"prepublish": "npm run all"

@@ -41,25 +41,21 @@ },

"babel-core": "^6.5.1",
"babel-eslint": "^6.1.2",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.2.4",
"babel-plugin-rewire": "^1.0.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"cross-env": "^2.0.1",
"eslint": "^2.8.0",
"eslint-config-airbnb": "^8.0.0",
"eslint-config-jss": "^1.1.0",
"cross-env": "^3.0.0",
"eslint": "^3.6.1",
"eslint-config-airbnb": "^12.0.0",
"eslint-config-jss": "^2.4.0",
"eslint-plugin-import": "^1.12.0",
"eslint-plugin-jsx-a11y": "^1.5.5",
"eslint-plugin-react": "^5.0.1",
"eslint-plugin-jsx-a11y": "^2.2.2",
"eslint-plugin-react": "^6.3.0",
"expect.js": "^0.3.1",
"jss": "^5.5.0",
"jss": "^5.5.5",
"karma": "^1.1.1",
"karma-benchmark": "^0.6.0",
"karma-benchmark-reporter": "^0.1.1",
"karma-browserstack-launcher": "^1.0.0",
"karma-chrome-launcher": "^1.0.1",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^1.1.0",
"karma-firefox-launcher": "^1.0.0",
"karma-mocha": "^1.0.1",
"karma-mocha-reporter": "^2.0.2",
"karma-safari-launcher": "^1.0.0",
"karma-sourcemap-loader": "^0.3.5",

@@ -75,11 +71,14 @@ "karma-webpack": "^1.7.0",

"peerDependencies": {
"jss": "^5.5.0"
"jss": "^5.5.5"
},
"lint-staged": {
"./src/*.js": [
"eslint --fix",
"eslint",
"git add"
]
},
"pre-commit": "lint:staged"
"pre-commit": "lint:staged",
"dependencies": {
"warning": "^3.0.0"
}
}
![JSS logo](https://avatars1.githubusercontent.com/u/9503099?v=3&s=60)
## JSS plugin that enables selector composition
## JSS plugin for classes composition.
There are few things that you must consider before use *jss-compose*:
* Composition doesn't work with [named stylesheets](https://github.com/cssinjs/jss/blob/master/docs/json-api.md#writing-global-selectors).
* `composes` property accepts strings or arrays.
* Composition works only if the resulting selector is a single class name (don't try to put `composes` property inside nested selector if you use [jss-nested](https://github.com/cssinjs/jss-nested)).
* Composition works only if you compose rule, that is defined **AFTER** rule, where you write `composes` (Otherwise you get wrong css selector order and priority).
This plugin allows you to use CSS frameworks and legacy code together with JSS as well as reuse Rules more granularly.

@@ -15,8 +11,9 @@ Make sure you read [how to use

## Usage example
[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/cssinjs/lobby)
#### 1. Composition with unnamed (global) selector
It's usefull if you want to combine jss with style frameworks such [Material Design Lite](https://getmdl.io/) or [Bootstrap](http://getbootstrap.com/) or any other.
### Compose with global classes.
To combine JSS with CSS framework like [Material Design Lite](https://getmdl.io/) or [Bootstrap](http://getbootstrap.com/) and others.
```javascript

@@ -35,2 +32,3 @@ const sheet = jss.createStyleSheet({

Compiles to:
```css

@@ -45,5 +43,6 @@ .button-123456 {

When you use it:
```javascript
<button className={sheet.classes.button}>Button</button>
<button className={sheet.classes.buttonActive}>Active Button</button>
<button className={classes.button}>Button</button>
<button className={classes.buttonActive}>Active Button</button>
```

@@ -56,6 +55,6 @@ It renders to:

#### 2. Composition with named selector
### Compose with local classes.
This approach is usefull if you want manage elements states without additinal styles duplication (e.g. button with active, disabled states).
To use it - simple add `$` symbol before rule, that you want to compose.
Manage element states without rules duplication.
To reference a local rule, prefix the rule name with `$` symbol.

@@ -90,3 +89,5 @@ ```javascript

```
Compiles to:
```css

@@ -108,8 +109,10 @@ .button-123456 {

}
/* .buttonDisabled is not printed here, because it have only compositon and no styles inside */
/* Rule `buttonDisabled` is not compiled to CSS, because it has no own properties. */
```
When you use it:
```javascript
<button className={sheet.classes.buttonActiveDisabled}>Active Disabled Button</button>
<button className={sheet.classes.buttonDisabled}>Disabled Button with active state</button>
<button className={classes.buttonActiveDisabled}>Active Disabled Button</button>
<button className={classes.buttonDisabled}>Disabled Button with active state</button>
```

@@ -122,5 +125,5 @@ It renders to:

#### 3. Mixed composition
### Mix global and local classes.
You can compose both internal named selectors and external global (unnamed) selectors
You can compose both local and global classes at the same time.

@@ -131,5 +134,5 @@ ```javascript

color: 'red'
}
},
button: {
composes: ['$active', 'btn', 'btn-primary'], // You can use arrays too
composes: ['$active', 'btn', 'btn-primary'],
color: 'blue'

@@ -139,3 +142,5 @@ }

```
Compiles to:
```css

@@ -149,5 +154,7 @@ .active-123456 {

```
When you use it:
```javascript
<button className={sheet.classes.button}>Button</button>
<button className={classes.button}>Button</button>
```

@@ -159,3 +166,8 @@ It renders to:

## Gotchas
- Composition doesn't work with option `{named: false}` - [global stylesheets](https://github.com/cssinjs/jss/blob/master/docs/json-api.md#writing-global-selectors).
- Does not work inside of [nested rules](https://github.com/cssinjs/jss-nested).
- When composing local rules, they need to be defined first. Otherwise you get wrong css selector order and specificity.
## Issues

@@ -169,3 +181,3 @@

npm i
npm run test
npm test
```

@@ -172,0 +184,0 @@

@@ -1,3 +0,2 @@

// This file for jss main tests runner.
var context = require.context('./lib', true, /\.test\.js$/)
const context = require.context('./src', true, /\.test\.js$/)
context.keys().forEach(context)

@@ -0,0 +0,0 @@ var webpack = require('webpack')

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc