Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Janak committed Mar 31, 2015
1 parent 638db43 commit 11bc70c
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
68 changes: 66 additions & 2 deletions README.md
@@ -1,2 +1,66 @@
# react-native-nested-stylesheets
Extends React Native's StyleSheet Object to allow for nesting of styles
# react-native-nested-stylesheet

Based off of the `react-native` `StyleSheet`, the `NestedStyleSheet` allows for nesting of styles.

## Installation
`npm install react-native-nested-stylesheet`

## Usage notes
This plugin will allow you to create styles of the following format:

```
...
namespace: {
styleA: {...},
styleB: {...},
},
...
```

With the plain `StyleSheet` API you can only create stylesheets with one level of nesting. It should noted that this does not create cascading selectors. This is merely to allow namespacing of styles (e.g. containing the styles for all items in a specific `ListView`.

Note: You are only allowed to include other objects within a nested style. You cannot define rules at the namespace level.

## Usage Example
```
var React = require('react-native');
var NestedStyleSheet = require('react-native-nested-stylesheet');
var {
View,
Text,
Image,
} = React;
var styles = NestedStyleSheet.create({
cells: {
container: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
},
thumbnail: {
width: 53,
height: 81,
},
content: {
flex: 1,
},
},
});
var Demo = React.createClass({
render: function() {
return (
<View style={styles.cells.container}>
<Image source={{...}} style={styles.cells.thumbnail}
<View style={styles.content}>
<Text>...</Text>
</View>
</View>
);
},
});
module.exports = Demo;
```
19 changes: 19 additions & 0 deletions package.json
@@ -0,0 +1,19 @@
{
"name": "react-native-nested-stylesheet",
"version": "0.0.1",
"description": "Based off of the react-native StyleSheet, the NestedStyleSheet allows for nesting of styles.",
"keywords": ["react", "react native", "style", "stylesheet"],
"bugs": "https://github.com/pjjanak/react-native-nested-stylesheets/issues",
"license": "GPL-2.0",
"author": {
"name": "Peter Janak"
},
"main": "./src/NestedStyleSheet.js",
"repository": {
"type": "git",
"url": "https://github.com/pjjanak/react-native-nested-stylesheets.git"
},
"dependencies": {
"react-native": "^0.3.1"
}
}
28 changes: 28 additions & 0 deletions src/NestedStyleSheet.js
@@ -0,0 +1,28 @@
'use strict';

var StyleSheetRegistry = require('StyleSheetRegistry');
var StyleSheetValidation = require('StyleSheetValidation');

var NestedStyleSheetValidation = require('./NestedStyleSheetValidation');

class NestedStyleSheet {
static create(obj: {[key: string]: any}): {[key: string]: number} {
var result = {};
for (var key in obj) {
var styleObj = obj[key];
var styleObjKeys = Object.keys(styleObj);

if (Object.prototype.toString.call(styleObj[styleObjKeys[0]]) === '[object Object]') {
NestedStyleSheetValidation.validateIsNestedStyle(styleObj);
result[key] = NestedStyleSheet.create(styleObj);
} else {
StyleSheetValidation.validateStyle(key, obj);
result[key] = StyleSheetRegistry.registerStyle(styleObj);
}
}
return result;
}
}


module.exports = NestedStyleSheet;
30 changes: 30 additions & 0 deletions src/NestedStyleSheetValidation.js
@@ -0,0 +1,30 @@
var invariant = require('invariant');

class NestedStyleSheetValidation {
static validateIsNestedStyle(nestedStyles) {
if (!__DEV__) {
return;
}

// if you are nesting styles no parent element may have anything but
// objects ({}) as children
for (var prop in nestedStyles) {
var styleObj = nestedStyles[prop];
if (Object.prototype.toString.call(styleObj) !== '[object Object]') {
styleError('"' + styleObj + '" is not a plain Javascript object', prop,
'StyleSheet ' + prop, 'Parents of nested styles can only have plain '
+ 'Javascript objects ({...}) as children');
}
}
}
}

var styleError = function(message1, style, caller?, message2?) {
invariant(
false,
message1 + '\n' + (caller || '<<unknown>>') + ': ' +
JSON.stringify(style, null, ' ') + (message2 || '')
);
};

module.exports = NestedStyleSheetValidation;

0 comments on commit 11bc70c

Please sign in to comment.