Skip to content

Commit 8ec9d98

Browse files
committed
Remove stylesheet
1 parent 79d953d commit 8ec9d98

File tree

7 files changed

+31
-198
lines changed

7 files changed

+31
-198
lines changed

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "react-native-use-styles",
2+
"name": "react-use-styles",
33
"version": "1.3.7",
4-
"description": "React Native useStyles",
4+
"description": "React useStyles",
55
"main": "index.js",
66
"module": "src/index.js",
77
"types": "index.d.ts",
@@ -25,7 +25,7 @@
2525
},
2626
"repository": {
2727
"type": "git",
28-
"url": "git+https://github.com/rootstrap/react-native-use-styles.git"
28+
"url": "git+https://github.com/rootstrap/react-use-styles.git"
2929
},
3030
"keywords": [
3131
"react",
@@ -39,9 +39,9 @@
3939
"author": "Agustín Prieto <agustin@rootstrap.com>",
4040
"license": "MIT",
4141
"bugs": {
42-
"url": "https://github.com/rootstrap/react-native-use-styles/issues"
42+
"url": "https://github.com/rootstrap/react-use-styles/issues"
4343
},
44-
"homepage": "https://github.com/rootstrap/react-native-use-styles#readme",
44+
"homepage": "https://github.com/rootstrap/react-use-styles#readme",
4545
"dependencies": {},
4646
"devDependencies": {
4747
"@babel/core": "^7.11.6",
@@ -70,7 +70,6 @@
7070
},
7171
"peerDependencies": {
7272
"react": "^16.8.0",
73-
"react-native": "^0.59.0",
7473
"react-test-renderer": "^16.8.0"
7574
},
7675
"husky": {

src/dictionaries/aliases.js renamed to src/aliases/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
input: 'fx:dir:col'
33
output: { flexDirection: 'column' }
44
*/
5-
export default Object.assign(Object.create(null), {
5+
const aliases = Object.assign(Object.create(null), {
66
bot: 'bottom',
77
col: 'column',
88
dir: 'direction',
@@ -16,3 +16,7 @@ export default Object.assign(Object.create(null), {
1616
wd: 'width',
1717
hg: 'height',
1818
});
19+
20+
export default (alias) => {
21+
return aliases[alias] || alias;
22+
};

src/core/cache.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { StyleSheet } from 'react-native';
21
import { GLOBAL_KEY, CONSTANTS_KEY, COMPUTED_KEY } from '../constants';
32
import { warn } from '../utils';
43

@@ -10,7 +9,7 @@ export const clearCache = () => {
109
};
1110
clearCache();
1211

13-
const processDefinition = (definition) => {
12+
const mutateDefinition = (definition) => {
1413
const constants = definition.constants;
1514
const computed = definition.computed;
1615

@@ -27,9 +26,7 @@ const processDefinition = (definition) => {
2726
definition.constants = null;
2827
definition.computed = null;
2928

30-
const styles = StyleSheet.create(definition);
31-
32-
return { styles, constants, computed };
29+
return { constants, computed };
3330
};
3431

3532
const getValueFromStorageObject = (key, object, isConstant, isComputed) => {
@@ -45,7 +42,7 @@ const getValueFromStorageObject = (key, object, isConstant, isComputed) => {
4542
};
4643

4744
export const setInCache = (definition, namespace) => {
48-
const { styles, constants, computed } = processDefinition(definition);
45+
const { constants, computed } = mutateDefinition(definition);
4946
let cache = globalCache;
5047

5148
if (namespace) {
@@ -55,7 +52,7 @@ export const setInCache = (definition, namespace) => {
5552
cache = cache[GLOBAL_KEY];
5653
}
5754

58-
Object.assign(cache, StyleSheet.create(styles));
55+
Object.assign(cache, definition);
5956
Object.assign(cache, {
6057
[CONSTANTS_KEY]: constants,
6158
[COMPUTED_KEY]: computed,
@@ -117,8 +114,7 @@ export const getFromCache = (
117114
return;
118115
}
119116

120-
// if it's a style, get native style from cached id with flatten
121-
return isConstant || isComputed ? value : StyleSheet.flatten(value);
117+
return value;
122118
};
123119

124120
export const getConstant = (name, namespace) => {

src/core/transformer.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import stylesDictionary from '../dictionaries/styles';
2-
import aliasesDictionary from '../dictionaries/aliases';
3-
import { hasConstant, warn } from '../utils';
1+
import getAlias from '../aliases';
2+
import { hasConstant, capitalize } from '../utils';
43
import { DEFAULT_SEPARATOR } from '../constants';
54

65
export let separator = DEFAULT_SEPARATOR;
@@ -14,32 +13,20 @@ const getValueFromParts = (parts, getConstant) => {
1413
if (hasConstant(value)) {
1514
value = getConstant(value);
1615
} else {
17-
value = aliasesDictionary[value] || value;
16+
value = getAlias(value);
1817
}
1918

2019
return parseFloat(value) || value;
2120
};
2221

2322
const getKeyFromParts = (parts) => {
24-
let current = stylesDictionary;
25-
26-
for (let x = 0; x < parts.length - 1; x += 1) {
27-
let part = parts[x];
28-
part = aliasesDictionary[part] || part;
29-
current = current[part];
30-
31-
if (current === undefined) {
32-
warn(
33-
current === undefined,
34-
`"${part}" is not a valid key for styles`,
35-
'Invalid-Style-Key',
36-
);
37-
// return to be executed when current is undefined
38-
return;
39-
}
23+
let current = getAlias(parts[0]);
24+
25+
for (let x = 1; x < parts.length - 1; x += 1) {
26+
current += capitalize(getAlias(parts[x]));
4027
}
4128

42-
return current.__propName;
29+
return current;
4330
};
4431

4532
// PRECONDITION: at least one key-value pair exists in the path

src/dictionaries/styles.js

Lines changed: 0 additions & 151 deletions
This file was deleted.

src/utils/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export const getPathFromLiteralTag = (strings, expressions) =>
2929
'',
3030
);
3131

32+
export const capitalize = (string) => {
33+
return string.charAt(0).toUpperCase() + string.slice(1);
34+
};
35+
3236
export const warn = (conditional, description = '', warningKey = '') => {
3337
if (conditional) {
3438
console.warn(

tests/core/transformer.spec.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ describe('utils', () => {
5858
},
5959
});
6060
expect(
61-
transform('color:$purple', key => getFromStorage(key, null, null, true)),
61+
transform('color:$purple', (key) =>
62+
getFromStorage(key, null, null, true),
63+
),
6264
).toMatchObject({
6365
color: 'purple',
6466
});
@@ -75,7 +77,7 @@ describe('utils', () => {
7577
'namespace',
7678
);
7779
expect(
78-
transform('color:@namespace$purple', key =>
80+
transform('color:@namespace$purple', (key) =>
7981
getFromStorage(key, 'namespace', null, true),
8082
),
8183
).toMatchObject({
@@ -87,12 +89,4 @@ describe('utils', () => {
8789
setSeparator('-');
8890
expect(transform('flex-1')).toMatchObject({ flex: 1 });
8991
});
90-
91-
it('Development mode only: transform produces a console.warn when providing an Invalid-Style-Key', () => {
92-
expect(transform('non-existent:1')).toMatchObject({ undefined: 1 });
93-
expect(console.warn).toBeCalledTimes(1);
94-
expect(console.warn).toHaveBeenLastCalledWith(
95-
'useStyles Invalid-Style-Key: "non-existent" is not a valid key for styles. You are seeing this warning because you are in development mode. In a production build, there will be no warning.',
96-
);
97-
});
9892
});

0 commit comments

Comments
 (0)