Skip to content

Commit 62b0884

Browse files
authored
Merge pull request #13 from mohit23x/type-enhancemets
fix issue #10
2 parents f55b129 + a64940b commit 62b0884

9 files changed

Lines changed: 44 additions & 60 deletions

File tree

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
example/
2+
lib/
3+
assets/
4+
.github/

build/Sheet.d.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import type { ConstantsType, Fn, NamedStyles } from './type';
2-
export default class Sheet<T, S extends NamedStyles<S> | NamedStyles<any>, O = S> {
3-
result: O;
4-
source: Fn<T, S>;
5-
nativeSheet: O;
6-
constructor(sourceFn: Fn<T, S>);
7-
calc(globalVars: T, constants: ConstantsType, activeIndex: number): O;
8-
getResult(): O;
1+
import type { ConstantsType, Fn, NamedStyles, StyleSheetType } from './type';
2+
export default class Sheet<T, P extends NamedStyles<P> | NamedStyles<any>> {
3+
result: StyleSheetType<P>;
4+
source: Fn<T, P>;
5+
constructor(sourceFn: Fn<T, P>);
6+
calc(globalVars: T, constants: ConstantsType, activeIndex: number): StyleSheetType<P>;
7+
getResult(): StyleSheetType<P>;
98
clearResult(): void;
109
calcStyles(globalVars: T, constants: ConstantsType, activeIndex: number): void;
11-
calcStyle(key: string, styleProps: any): void;
12-
calcNative(): void;
1310
}

build/Sheet.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
const react_native_1 = require("react-native");
43
class Sheet {
54
constructor(sourceFn) {
6-
this.nativeSheet = {};
75
this.source = sourceFn;
86
this.result = {};
97
}
108
calc(globalVars, constants, activeIndex) {
119
this.clearResult();
1210
this.calcStyles(globalVars, constants, activeIndex);
13-
this.calcNative();
1411
return this.getResult();
1512
}
1613
getResult() {
@@ -45,15 +42,5 @@ class Sheet {
4542
});
4643
}
4744
}
48-
calcStyle(key, styleProps) {
49-
// @ts-ignore
50-
this.nativeSheet[key] = styleProps;
51-
}
52-
calcNative() {
53-
if (Object.keys(this.result).length) {
54-
const rnStyleSheet = react_native_1.StyleSheet.create(this.nativeSheet);
55-
Object.assign(this.result, rnStyleSheet);
56-
}
57-
}
5845
}
5946
exports.default = Sheet;

build/Sugar.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StyleSheet } from 'react-native';
2-
import { Fn, buildEventType, NamedStyles, ConstantsType } from './type';
2+
import { Fn, buildEventType, NamedStyles, ConstantsType, StyleSheetType } from './type';
33
import Sheet from './Sheet';
44
export default class Sugar<T> {
55
builded: boolean;
@@ -99,7 +99,7 @@ export default class Sugar<T> {
9999
_refresh(): void;
100100
build(themeObj: T): void;
101101
configure(newConstants: Partial<ConstantsType>): void;
102-
create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): P;
102+
create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): StyleSheetType<P>;
103103
_calculateActiveIndex(): void;
104104
_calcSheets(): void;
105105
_callListeners(event: buildEventType): void;

build/type.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export declare type NamedStyles<T> = {
1717
};
1818
export declare type S = NamedStyles<any>;
1919
export declare type Fn<T, P> = (theme: T, constants: ConstantsType) => P extends NamedStyles<P> ? NamedStyles<P> : P;
20-
export declare type StyleSheetType<T> = {
21-
[P in keyof T]: ViewStyle | TextStyle | ImageStyle;
20+
export declare type StyleSheetType<P> = {
21+
[K in keyof P]: {
22+
[J in keyof P[K]]: P[K][J] extends Array<any> ? J extends 'transform' | 'transformMatrix' ? P[K][J] : P[K][J][number] : P[K][J];
23+
};
2224
};
2325
export declare type buildEventType = 'build';
2426
export declare type ThemeProp<T> = {

lib/Sheet.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
import { StyleSheet } from 'react-native';
21
import type { ConstantsType, Fn, NamedStyles, StyleSheetType } from './type';
32

43
export default class Sheet<
54
T,
6-
S extends NamedStyles<S> | NamedStyles<any>,
7-
O = S
8-
> {
9-
public result: O;
10-
public source: Fn<T, S>;
11-
public nativeSheet: O = {} as O;
5+
P extends NamedStyles<P> | NamedStyles<any>> {
6+
public result: StyleSheetType<P>;
7+
public source: Fn<T, P>;
128

13-
constructor(sourceFn: Fn<T, S>) {
9+
constructor(sourceFn: Fn<T, P>) {
1410
this.source = sourceFn;
15-
this.result = {} as O;
11+
this.result = {} as StyleSheetType<P>;
1612
}
1713

18-
calc(globalVars: T, constants: ConstantsType, activeIndex: number): O {
14+
calc(globalVars: T, constants: ConstantsType, activeIndex: number): StyleSheetType<P> {
1915
this.clearResult();
2016
this.calcStyles(globalVars, constants, activeIndex);
21-
this.calcNative();
2217
return this.getResult();
2318
}
2419

25-
getResult(): O {
20+
getResult(): StyleSheetType<P> {
2621
return this.result;
2722
}
2823

@@ -59,16 +54,4 @@ export default class Sheet<
5954
});
6055
}
6156
}
62-
63-
calcStyle(key: string, styleProps: any): void {
64-
// @ts-ignore
65-
this.nativeSheet[key] = styleProps;
66-
}
67-
68-
calcNative(): void {
69-
if (Object.keys(this.result).length) {
70-
const rnStyleSheet = StyleSheet.create(this.nativeSheet);
71-
Object.assign(this.result, rnStyleSheet);
72-
}
73-
}
7457
}

lib/Sugar.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,18 @@ export default class Sugar<T> {
145145
this._refresh();
146146
}
147147

148-
create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): P {
148+
create<P extends NamedStyles<P> | NamedStyles<any>>(
149+
objFn: Fn<T, P>
150+
): StyleSheetType<P> {
149151
if (typeof objFn === 'function') {
150152
const sheet = new Sheet(objFn);
151153
this.sheets.push(sheet);
152154
if (this.builded) {
153155
sheet.calc(this.theme, this.constants, this.activeIndex);
154156
}
155-
return sheet.getResult() as P;
157+
return sheet.getResult() as StyleSheetType<P>;
156158
}
157-
return objFn as P;
159+
return objFn as StyleSheetType<P>;
158160
}
159161

160162
_calculateActiveIndex(): void {

lib/type.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Sugar from './Sugar';
44

55
export type ConstantsType = typeof constants;
66

7-
87
export type SugarViewStyle = {
98
[P in keyof ViewStyle]: ViewStyle[P] | Array<ViewStyle[P]>;
109
};
@@ -22,11 +21,21 @@ export type NamedStyles<T> = {
2221

2322
export type S = NamedStyles<any>;
2423

25-
export type Fn<T, P> = (theme: T, constants: ConstantsType) => P extends NamedStyles<P> ? NamedStyles<P> : P;
26-
27-
export type StyleSheetType<T> = {
28-
[P in keyof T]: ViewStyle | TextStyle | ImageStyle;
24+
export type Fn<T, P> = (
25+
theme: T,
26+
constants: ConstantsType
27+
) => P extends NamedStyles<P> ? NamedStyles<P> : P;
28+
29+
export type StyleSheetType<P> = {
30+
[K in keyof P]: {
31+
[J in keyof P[K]]: P[K][J] extends Array<any>
32+
? J extends 'transform' | 'transformMatrix'
33+
? P[K][J]
34+
: P[K][J][number]
35+
: P[K][J];
36+
};
2937
};
38+
3039
export type buildEventType = 'build';
3140

3241
export type ThemeProp<T> = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-sugar-style",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "React Native Stylesheet alternative with theme support",
55
"author": "mohit23x",
66
"license": "MIT",

0 commit comments

Comments
 (0)