Skip to content

Commit 495defd

Browse files
committed
[fix] StyleSheet.hairlineWidth on retina screens
1 parent 1a20fcf commit 495defd

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

docs/storybook/2-apis/StyleSheet/StyleSheetScreen.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ StyleSheet.flatten([styles.listItem, styles.selectedListItem]);`
104104
typeInfo="object"
105105
/>
106106

107-
<DocItem name="hairlineWidth" typeInfo="number" />
107+
<DocItem
108+
description="Enables borders of just one physical pixel on retina screens, otherwise it is equal to a CSS value of 1px."
109+
name="hairlineWidth"
110+
typeInfo="number"
111+
/>
108112
</Section>
109113
</UIExplorer>
110114
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Based on http://dieulot.net/css-retina-hairline
3+
* @noflow
4+
*/
5+
6+
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
7+
8+
const getHairlineWidth = () => {
9+
let hairlineWidth = 1;
10+
if (canUseDOM && window.devicePixelRatio && window.devicePixelRatio >= 2) {
11+
const node = document.createElement('div');
12+
node.style.border = '.5px solid transparent';
13+
document.body.appendChild(node);
14+
if (node.offsetHeight === 1) {
15+
hairlineWidth = 0.5;
16+
}
17+
document.body.removeChild(node);
18+
}
19+
return hairlineWidth;
20+
};
21+
22+
export default getHairlineWidth;

src/apis/StyleSheet/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
import flattenStyle from './flattenStyle';
13+
import getHairlineWidth from './getHairlineWidth';
1314
import modality from '../../modules/modality';
1415
import StyleRegistry from './registry';
1516

@@ -58,7 +59,7 @@ const StyleSheet = {
5859
getStyleSheets() {
5960
return StyleRegistry.getStyleSheets();
6061
},
61-
hairlineWidth: 1
62+
hairlineWidth: getHairlineWidth()
6263
};
6364

6465
export default StyleSheet;

0 commit comments

Comments
 (0)