Skip to content

Commit

Permalink
[fix] StyleSheet.hairlineWidth on retina screens
Browse files Browse the repository at this point in the history
  • Loading branch information
necolas committed Dec 20, 2017
1 parent 1a20fcf commit 495defd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 5 additions & 1 deletion docs/storybook/2-apis/StyleSheet/StyleSheetScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ StyleSheet.flatten([styles.listItem, styles.selectedListItem]);`
typeInfo="object"
/>

<DocItem name="hairlineWidth" typeInfo="number" />
<DocItem
description="Enables borders of just one physical pixel on retina screens, otherwise it is equal to a CSS value of 1px."
name="hairlineWidth"
typeInfo="number"
/>
</Section>
</UIExplorer>
);
Expand Down
22 changes: 22 additions & 0 deletions src/apis/StyleSheet/getHairlineWidth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Based on http://dieulot.net/css-retina-hairline
* @noflow
*/

import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';

const getHairlineWidth = () => {
let hairlineWidth = 1;
if (canUseDOM && window.devicePixelRatio && window.devicePixelRatio >= 2) {
const node = document.createElement('div');
node.style.border = '.5px solid transparent';
document.body.appendChild(node);
if (node.offsetHeight === 1) {
hairlineWidth = 0.5;
}
document.body.removeChild(node);
}
return hairlineWidth;
};

export default getHairlineWidth;
3 changes: 2 additions & 1 deletion src/apis/StyleSheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

import flattenStyle from './flattenStyle';
import getHairlineWidth from './getHairlineWidth';
import modality from '../../modules/modality';
import StyleRegistry from './registry';

Expand Down Expand Up @@ -58,7 +59,7 @@ const StyleSheet = {
getStyleSheets() {
return StyleRegistry.getStyleSheets();
},
hairlineWidth: 1
hairlineWidth: getHairlineWidth()
};

export default StyleSheet;

0 comments on commit 495defd

Please sign in to comment.