Skip to content

Commit

Permalink
Merge 83a7f0c into ccea7a3
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahm committed Jul 28, 2016
2 parents ccea7a3 + 83a7f0c commit 05fada9
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"node": true,
"jest": true
},

"globals": {
"window": true
},

"plugins": [
"react",
Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,43 @@ const styles = EStyleSheet.create({
}
});
```

An idea for the orientation change support, it requires using another package for detecting the orientation change event, like react-native-orientation

```js
var Orientation = require('react-native-orientation');
var styles = EStyleSheet.create({
card: {
height: 200,
},
'@media all and (orientation: portrait)': {
card: {
width: '42%'
}
},
'@media all and (orientation: landscape)': {
card: {
width: '23%'
}
}
});
```
inside your component use:
```js
componentDidMount() {
Orientation.addOrientationListener(this._orientationDidChange.bind(this));
}

componentWillUnmount() {
Orientation.removeOrientationListener(this._orientationDidChange);
}

_orientationDidChange(orientation) {
styles = EStyleSheet.orientationUpdate(orientation, styles);
this.forceUpdate();
}
```

See full example [here](examples/media-queries).
\[[top](#)\]

Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,19 @@ describe('EStyleSheet API', function () {
const fn = () => api.subscribe('build', null);
expect(fn).toThrowError('Listener should be a function.');
});

it('should update styles in orientation change', function () {
api.build({});
let defaultStyles = api.create({
card: {
width: '50%',
}
});

let portraitStyles = api.orientationUpdate('portrait', defaultStyles);
expect(defaultStyles).toEqual(portraitStyles);

let landscapeStyles = api.orientationUpdate('landscape', defaultStyles);
expect(defaultStyles).not.toEqual(landscapeStyles);
});
});
29 changes: 29 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ export default class {
} else {
this.sheets.push(sheet);
}
this._cacheSheetSource(sheet.getResult(), sheet);

return sheet.getResult();
}

/**
* Updates a specific stylesheet when the orientation changes
* @param {orientation} string
* @param {originalObj} obj
* @returns {Object}
*/
orientationUpdate(orientation, originalObj) {
window.orientation = orientation;
var source = this._cacheSheetSource(originalObj);
let sheet = new Sheet(source);
sheet.calc(this.globalVars);
return sheet.getResult();
}

Expand Down Expand Up @@ -99,4 +115,17 @@ export default class {
this.listeners[event].forEach(listener => listener());
}
}

_cacheSheetSource(key, sheet){
key = JSON.stringify(Object.keys(key));
if (!memoize.cache) {
memoize.cache = {};
}
if (!memoize.cache[key]) {
if(sheet){
memoize.cache[key] = sheet.source;
}
}
return memoize.cache[key];
}
}
6 changes: 3 additions & 3 deletions src/replacers/media-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* - aspect-ratio
*/

import {Dimensions, Platform} from 'react-native';
import {Platform} from 'react-native';
import mediaQuery from 'css-mediaquery';
import utils from '../utils';

Expand Down Expand Up @@ -64,11 +64,11 @@ function process(obj) {
* @returns {Object}
*/
function getMatchObject() {
const win = Dimensions.get('window');
const win = utils.calcOrientation(window.orientation);
return {
width: win.width,
height: win.height,
orientation: win.width > win.height ? 'landscape' : 'portrait',
orientation: win.orientation,
'aspect-ratio': win.width / win.height,
type: Platform.OS,
};
Expand Down
8 changes: 4 additions & 4 deletions src/replacers/percent.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
* Calculation of percent strings
*/
import utils from '../utils';

import {Dimensions} from 'react-native';

const {width, height} = Dimensions.get('window');
const V_PROPS = [
'height',
'top',
Expand Down Expand Up @@ -44,8 +42,10 @@ function isPercent(str) {
* @returns {number}
*/
function calc(str, prop) {
const win = utils.calcOrientation(window.orientation);

let percent = parseInt(str.substring(0, str.length - 1), 10);
let base = isVertical(prop) ? height : width;
let base = isVertical(prop) ? win.height : win.width;
return base * percent / 100;
}

Expand Down
27 changes: 27 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/**
* Utils
*/
import {Dimensions} from 'react-native';

export default {
excludeKeys,
isObject,
calcOrientation
};

/**
Expand All @@ -31,3 +33,28 @@ function excludeKeys(obj, keys) {
function isObject(obj) {
return typeof obj === 'object' && obj !== null;
}

function calcOrientation(orientation){
var {width, height} = Dimensions.get('window');
if(orientation){
orientation = orientation.toLowerCase();
}
var newWidth = width;
var newHeight = height;

if (orientation == 'landscape') {
if(height > width){
newWidth = height;
newHeight = width;
}
}else if (orientation == 'portrait') {
if(width > height){
newWidth = height;
newHeight = width;
}
}
if(!orientation){
orientation = newWidth > newHeight ? 'landscape' : 'portrait'
}
return {width: newWidth, height: newHeight, orientation: orientation};
}

0 comments on commit 05fada9

Please sign in to comment.