Skip to content

Remove stylesheet #40

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

Open
wants to merge 1 commit into
base: main
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
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-use-styles",
"name": "react-use-styles",
"version": "1.3.7",
"description": "React Native useStyles",
"description": "React useStyles",
"main": "index.js",
"module": "src/index.js",
"types": "index.d.ts",
Expand All @@ -25,7 +25,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/rootstrap/react-native-use-styles.git"
"url": "git+https://github.com/rootstrap/react-use-styles.git"
},
"keywords": [
"react",
Expand All @@ -39,9 +39,9 @@
"author": "Agustín Prieto <agustin@rootstrap.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/rootstrap/react-native-use-styles/issues"
"url": "https://github.com/rootstrap/react-use-styles/issues"
},
"homepage": "https://github.com/rootstrap/react-native-use-styles#readme",
"homepage": "https://github.com/rootstrap/react-use-styles#readme",
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.11.6",
Expand Down Expand Up @@ -70,7 +70,6 @@
},
"peerDependencies": {
"react": "^16.8.0",
"react-native": "^0.59.0",
"react-test-renderer": "^16.8.0"
},
"husky": {
Expand Down
6 changes: 5 additions & 1 deletion src/dictionaries/aliases.js → src/aliases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
input: 'fx:dir:col'
output: { flexDirection: 'column' }
*/
export default Object.assign(Object.create(null), {
const aliases = Object.assign(Object.create(null), {
bot: 'bottom',
col: 'column',
dir: 'direction',
Expand All @@ -16,3 +16,7 @@ export default Object.assign(Object.create(null), {
wd: 'width',
hg: 'height',
});

export default (alias) => {
return aliases[alias] || alias;
};
14 changes: 5 additions & 9 deletions src/core/cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { StyleSheet } from 'react-native';
import { GLOBAL_KEY, CONSTANTS_KEY, COMPUTED_KEY } from '../constants';
import { warn } from '../utils';

Expand All @@ -10,7 +9,7 @@ export const clearCache = () => {
};
clearCache();

const processDefinition = (definition) => {
const mutateDefinition = (definition) => {
const constants = definition.constants;
const computed = definition.computed;

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

const styles = StyleSheet.create(definition);

return { styles, constants, computed };
return { constants, computed };
};

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

export const setInCache = (definition, namespace) => {
const { styles, constants, computed } = processDefinition(definition);
const { constants, computed } = mutateDefinition(definition);
let cache = globalCache;

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

Object.assign(cache, StyleSheet.create(styles));
Object.assign(cache, definition);
Object.assign(cache, {
[CONSTANTS_KEY]: constants,
[COMPUTED_KEY]: computed,
Expand Down Expand Up @@ -117,8 +114,7 @@ export const getFromCache = (
return;
}

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

export const getConstant = (name, namespace) => {
Expand Down
29 changes: 8 additions & 21 deletions src/core/transformer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import stylesDictionary from '../dictionaries/styles';
import aliasesDictionary from '../dictionaries/aliases';
import { hasConstant, warn } from '../utils';
import getAlias from '../aliases';
import { hasConstant, capitalize } from '../utils';
import { DEFAULT_SEPARATOR } from '../constants';

export let separator = DEFAULT_SEPARATOR;
Expand All @@ -14,32 +13,20 @@ const getValueFromParts = (parts, getConstant) => {
if (hasConstant(value)) {
value = getConstant(value);
} else {
value = aliasesDictionary[value] || value;
value = getAlias(value);
}

return parseFloat(value) || value;
};

const getKeyFromParts = (parts) => {
let current = stylesDictionary;

for (let x = 0; x < parts.length - 1; x += 1) {
let part = parts[x];
part = aliasesDictionary[part] || part;
current = current[part];

if (current === undefined) {
warn(
current === undefined,
`"${part}" is not a valid key for styles`,
'Invalid-Style-Key',
);
// return to be executed when current is undefined
return;
}
let current = getAlias(parts[0]);

for (let x = 1; x < parts.length - 1; x += 1) {
current += capitalize(getAlias(parts[x]));
}

return current.__propName;
return current;
};

// PRECONDITION: at least one key-value pair exists in the path
Expand Down
151 changes: 0 additions & 151 deletions src/dictionaries/styles.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const getPathFromLiteralTag = (strings, expressions) =>
'',
);

export const capitalize = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
};

export const warn = (conditional, description = '', warningKey = '') => {
if (conditional) {
console.warn(
Expand Down
14 changes: 4 additions & 10 deletions tests/core/transformer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ describe('utils', () => {
},
});
expect(
transform('color:$purple', key => getFromStorage(key, null, null, true)),
transform('color:$purple', (key) =>
getFromStorage(key, null, null, true),
),
).toMatchObject({
color: 'purple',
});
Expand All @@ -75,7 +77,7 @@ describe('utils', () => {
'namespace',
);
expect(
transform('color:@namespace$purple', key =>
transform('color:@namespace$purple', (key) =>
getFromStorage(key, 'namespace', null, true),
),
).toMatchObject({
Expand All @@ -87,12 +89,4 @@ describe('utils', () => {
setSeparator('-');
expect(transform('flex-1')).toMatchObject({ flex: 1 });
});

it('Development mode only: transform produces a console.warn when providing an Invalid-Style-Key', () => {
expect(transform('non-existent:1')).toMatchObject({ undefined: 1 });
expect(console.warn).toBeCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(
'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.',
);
});
});