Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea for orientation change support #9 #21

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,45 @@ const styles = EStyleSheet.create({
}
});
```

Here is an example of the orientation change:

```js
var styles = EStyleSheet.create({
card: {
height: 200,
backgroundColor: 'green'
},
'@media all and (orientation: portrait)': {
card: {
width: '42%'
}
},
'@media all and (orientation: landscape)': {
card: {
width: '23%'
}
}
});

class example extends React.Component {
onLayoutChange(event){
var updatedStyles = EStyleSheet.orientationUpdate(event, styles);
if(updatedStyles){
styles = updatedStyles;
this.forceUpdate();
}
}

render() {
console.warn('render');
return (
<View onLayout={ this.onLayoutChange.bind(this) } style={styles.card}>...</View>
)
}
}
```

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

Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,24 @@ 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%',
}
});

var layout = {nativeEvent:{layout:{width:50, height: 100}}};
let landscapeStyles = api.orientationUpdate(layout, defaultStyles);
expect(defaultStyles).not.toEqual(landscapeStyles);

layout = {nativeEvent:{layout:{width:100, height: 50}}};
let portraitStyles = api.orientationUpdate(layout, defaultStyles);
expect(defaultStyles).toEqual(portraitStyles);

let portraitStyles2 = api.orientationUpdate(layout, landscapeStyles);
expect(portraitStyles2).toEqual(false);
});
});
33 changes: 33 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Value from './value';
import vars from './replacers/vars';
import memoize from './memoize';
import child from './child';
import utils from './utils';


const BUILD_EVENT = 'build';

Expand Down Expand Up @@ -37,9 +39,27 @@ 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(event, originalObj) {
var dimensions = utils.setDimensions(event.nativeEvent.layout);
if(dimensions.updated == false){
return false;
}
var source = this._cacheSheetSource(originalObj);
let sheet = new Sheet(source);
sheet.calc(this.globalVars);
return sheet.getResult();
}
/**
* Builds all created stylesheets with passed variables
* @param {Object} [gVars]
Expand Down Expand Up @@ -99,4 +119,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.getDimensions();
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
7 changes: 3 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,9 @@ function isPercent(str) {
* @returns {number}
*/
function calc(str, prop) {
const win = utils.getDimensions();
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,13 @@
/**
* Utils
*/
import {Dimensions} from 'react-native';

export default {
excludeKeys,
isObject,
setDimensions,
getDimensions
};

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

function setDimensions(layout, defaultLayout){
defaultLayout = defaultLayout || false;
var updated = false;
if(!defaultLayout){
if(window.dimensions.width !== layout.width){
updated = true;
}
}
return window.dimensions = {
orientation: (layout.width < layout.height ? 'portrait' : 'landscape'),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this can be a false positive on iPad when using split view after this bug was fixed:
facebook/react-native#16152

width: layout.width,
height: layout.height,
default: defaultLayout,
updated: updated
}
}

function getDimensions(){
if(!window.dimensions){
setDimensions(Dimensions.get('window'), true);
}
return window.dimensions;
}