Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Janak
committed
Mar 31, 2015
1 parent
638db43
commit 11bc70c
Showing
5 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |