diff --git a/package.json b/package.json index 0517e08..6126311 100755 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -39,9 +39,9 @@ "author": "Agustín Prieto ", "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", @@ -70,7 +70,6 @@ }, "peerDependencies": { "react": "^16.8.0", - "react-native": "^0.59.0", "react-test-renderer": "^16.8.0" }, "husky": { diff --git a/src/dictionaries/aliases.js b/src/aliases/index.js similarity index 70% rename from src/dictionaries/aliases.js rename to src/aliases/index.js index c3d9a8c..1c0cfb5 100644 --- a/src/dictionaries/aliases.js +++ b/src/aliases/index.js @@ -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', @@ -16,3 +16,7 @@ export default Object.assign(Object.create(null), { wd: 'width', hg: 'height', }); + +export default (alias) => { + return aliases[alias] || alias; +}; diff --git a/src/core/cache.js b/src/core/cache.js index e857061..8a70fb0 100644 --- a/src/core/cache.js +++ b/src/core/cache.js @@ -1,4 +1,3 @@ -import { StyleSheet } from 'react-native'; import { GLOBAL_KEY, CONSTANTS_KEY, COMPUTED_KEY } from '../constants'; import { warn } from '../utils'; @@ -10,7 +9,7 @@ export const clearCache = () => { }; clearCache(); -const processDefinition = (definition) => { +const mutateDefinition = (definition) => { const constants = definition.constants; const computed = definition.computed; @@ -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) => { @@ -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) { @@ -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, @@ -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) => { diff --git a/src/core/transformer.js b/src/core/transformer.js index 06ff6c8..f96b62a 100644 --- a/src/core/transformer.js +++ b/src/core/transformer.js @@ -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; @@ -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 diff --git a/src/dictionaries/styles.js b/src/dictionaries/styles.js deleted file mode 100644 index 43c62fb..0000000 --- a/src/dictionaries/styles.js +++ /dev/null @@ -1,151 +0,0 @@ -// SOURCE: https://github.com/facebook/react-native/blob/d2045411f5771a8c7275c1388179fef3892e9f53/Libraries/Components/View/ReactNativeViewViewConfig.js -// TODO: remove prototype from inner objects (?) -export default Object.assign(Object.create(null), { - align: { - content: { __propName: 'alignContent' }, - items: { __propName: 'alignItems' }, - self: { __propName: 'alignSelf' }, - }, - aspect: { ratio: { __propName: 'aspectRatio' } }, - backface: { visibility: { __propName: 'backfaceVisibility' } }, - background: { color: { __propName: 'backgroundColor' } }, - border: { - bottom: { - color: { __propName: 'borderBottomColor' }, - end: { radius: { __propName: 'borderBottomEndRadius' } }, - left: { radius: { __propName: 'borderBottomLeftRadius' } }, - right: { radius: { __propName: 'borderBottomRightRadius' } }, - start: { radius: { __propName: 'borderBottomStartRadius' } }, - width: { __propName: 'borderBottomWidth' }, - }, - color: { __propName: 'borderColor' }, - end: { - color: { __propName: 'borderEndColor' }, - width: { __propName: 'borderEndWidth' }, - }, - left: { - color: { __propName: 'borderLeftColor' }, - width: { __propName: 'borderLeftWidth' }, - }, - radius: { __propName: 'borderRadius' }, - right: { - color: { __propName: 'borderRightColor' }, - width: { __propName: 'borderRightWidth' }, - }, - start: { - color: { __propName: 'borderStartColor' }, - width: { __propName: 'borderStartWidth' }, - }, - style: { __propName: 'borderStyle' }, - top: { - color: { __propName: 'borderTopColor' }, - end: { radius: { __propName: 'borderTopEndRadius' } }, - left: { radius: { __propName: 'borderTopLeftRadius' } }, - right: { radius: { __propName: 'borderTopRightRadius' } }, - start: { radius: { __propName: 'borderTopStartRadius' } }, - width: { __propName: 'borderTopWidth' }, - }, - }, - bottom: { __propName: 'bottom' }, - color: { __propName: 'color' }, - decomposed: { matrix: { __propName: 'decomposedMatrix' } }, - direction: { __propName: 'direction' }, - display: { __propName: 'display' }, - elevation: { __propName: 'elevation' }, - end: { __propName: 'end' }, - flex: { - __propName: 'flex', - basis: { __propName: 'flexBasis' }, - direction: { __propName: 'flexDirection' }, - grow: { __propName: 'flexGrow' }, - shrink: { __propName: 'flexShrink' }, - wrap: { __propName: 'flexWrap' }, - }, - font: { - family: { __propName: 'fontFamily' }, - size: { __propName: 'fontSize' }, - style: { __propName: 'fontStyle' }, - variant: { __propName: 'fontVariant' }, - weight: { __propName: 'fontWeight' }, - }, - height: { __propName: 'height' }, - include: { font: { padding: { __propName: 'includeFontPadding' } } }, - justify: { content: { __propName: 'justifyContent' } }, - left: { __propName: 'left' }, - letter: { spacing: { __propName: 'letterSpacing' } }, - line: { height: { __propName: 'lineHeight' } }, - margin: { - __propName: 'margin', - bottom: { __propName: 'marginBottom' }, - end: { __propName: 'marginEnd' }, - horizontal: { __propName: 'marginHorizontal' }, - left: { __propName: 'marginLeft' }, - right: { __propName: 'marginRight' }, - start: { __propName: 'marginStart' }, - top: { __propName: 'marginTop' }, - vertical: { __propName: 'marginVertical' }, - }, - max: { - height: { __propName: 'maxHeight' }, - width: { __propName: 'maxWidth' }, - }, - min: { - height: { __propName: 'minHeight' }, - width: { __propName: 'minWidth' }, - }, - opacity: { __propName: 'opacity' }, - overflow: { __propName: 'overflow' }, - overlay: { color: { __propName: 'overlayColor' } }, - padding: { - __propName: 'padding', - bottom: { __propName: 'paddingBottom' }, - end: { __propName: 'paddingEnd' }, - horizontal: { __propName: 'paddingHorizontal' }, - left: { __propName: 'paddingLeft' }, - right: { __propName: 'paddingRight' }, - start: { __propName: 'paddingStart' }, - top: { __propName: 'paddingTop' }, - vertical: { __propName: 'paddingVertical' }, - }, - postion: { __propName: 'position' }, - resize: { mode: { __propName: 'resizeMode' } }, - right: { __propName: 'right' }, - rotation: { __propName: 'rotation' }, - scale: { - x: { __propName: 'scaleX' }, - y: { __propName: 'scaleY' }, - }, - shadow: { - color: { __propName: 'shadowColor' }, - offset: { __propName: 'shadowOffset' }, - opacity: { __propName: 'shadowOpacity' }, - radius: { __propName: 'shadowRadius' }, - }, - start: { __propName: 'start' }, - text: { - align: { - __propName: 'textAlign', - vertical: { __propName: 'textAlignVertical' }, - }, - decoration: { - color: { __propName: 'textDecorationColor' }, - line: { __propName: 'textDecorationLine' }, - style: { __propName: 'textDecorationStyle' }, - }, - shadow: { - color: { __propName: 'textShadowColor' }, - offset: { __propName: 'textShadowOffset' }, - radius: { __propName: 'textShadowRadius' }, - }, - transform: { __propName: 'textTransform' }, - }, - tint: { color: { __propName: 'tintColor' } }, - top: { __propName: 'top' }, - transform: { __propName: 'transform' }, //TODO: not supported, not primitive value, this receives an array - translate: { - x: { __propName: 'translateX' }, - y: { __propName: 'translateY' }, - }, - width: { __propName: 'width' }, - z: { index: { __propName: 'zIndex' } }, -}); diff --git a/src/utils/index.js b/src/utils/index.js index 027674b..bd700c7 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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( diff --git a/tests/core/transformer.spec.js b/tests/core/transformer.spec.js index 56baf5a..b07129f 100644 --- a/tests/core/transformer.spec.js +++ b/tests/core/transformer.spec.js @@ -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', }); @@ -75,7 +77,7 @@ describe('utils', () => { 'namespace', ); expect( - transform('color:@namespace$purple', key => + transform('color:@namespace$purple', (key) => getFromStorage(key, 'namespace', null, true), ), ).toMatchObject({ @@ -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.', - ); - }); });