1
1
import { ReactUnity } from '@reactunity/renderer' ;
2
- import React , { useContext , useMemo , useRef } from " react" ;
2
+ import React , { useContext , useMemo , useRef } from ' react' ;
3
3
4
4
type Cmp = ReactUnity . IReactComponent ;
5
5
type Styles = Record < string , object > ;
@@ -15,7 +15,7 @@ const styleContext = React.createContext<ContextType>(null);
15
15
export const useStyleContext = ( ) => useContext ( styleContext ) ;
16
16
17
17
interface ElementProps {
18
- element : Cmp ,
18
+ element : Cmp ;
19
19
styles : Styles ;
20
20
ind : number ;
21
21
sheet : any ;
@@ -24,13 +24,13 @@ interface ElementProps {
24
24
type State = ElementProps [ ] ;
25
25
26
26
const findElementId = ( state : State , el : Cmp ) => {
27
- let ind = state . findIndex ( x => x . element === el ) ;
27
+ let ind = state . findIndex ( ( x ) => x . element === el ) ;
28
28
29
29
if ( ind < 0 ) {
30
30
ind = state . length ;
31
31
const st = { element : el , styles : { } , ind } as ElementProps ;
32
32
state . push ( st ) ;
33
- el . SetData ( 'devtools-el' , ind + '' ) ;
33
+ el . SetData ( 'devtools-el' , String ( ind ) ) ;
34
34
}
35
35
36
36
return ind ;
@@ -54,8 +54,7 @@ const buildSheet = (state: ElementProps) => {
54
54
}
55
55
}
56
56
57
- if ( values . length )
58
- sheet . AddRules ( selector , valuesDic ) ;
57
+ if ( values . length ) sheet . AddRules ( selector , valuesDic ) ;
59
58
60
59
state . sheet = sheet ;
61
60
@@ -77,33 +76,34 @@ const changed = (state: ElementProps) => {
77
76
export function StyleContext ( { children } ) {
78
77
const state = useRef < State > ( [ ] ) ;
79
78
80
- const ctx = useMemo ( ( ) => ( {
81
- setProp : ( el : Cmp , prop : string , value : any ) => {
82
- const ind = findElementId ( state . current , el ) ;
83
- state . current [ ind ] . styles [ prop ] = value ;
84
- changed ( state . current [ ind ] ) ;
85
- } ,
86
- removeProp : ( el : Cmp , prop : string ) => {
87
- const ind = findElementId ( state . current , el ) ;
88
- Reflect . deleteProperty ( state . current [ ind ] . styles , prop ) ;
89
- changed ( state . current [ ind ] ) ;
90
- } ,
91
- hasProp : ( el : Cmp , prop : string ) => {
92
- const ind = findElementId ( state . current , el ) ;
93
- return Object . prototype . hasOwnProperty . call ( state . current [ ind ] . styles , prop ) ;
94
- } ,
95
- getProp : ( el : Cmp , prop : string ) => {
96
- const ind = findElementId ( state . current , el ) ;
97
- return state . current [ ind ] . styles [ prop ] ;
98
- } ,
99
- getStyles : ( el : Cmp ) => {
100
- const ind = findElementId ( state . current , el ) ;
101
- return state . current [ ind ] . styles ;
102
- } ,
103
- getElementId : ( el : Cmp ) => findElementId ( state . current , el ) ,
104
- } ) , [ ] ) ;
105
-
106
- return < styleContext . Provider value = { ctx } >
107
- { children }
108
- </ styleContext . Provider > ;
79
+ const ctx = useMemo (
80
+ ( ) => ( {
81
+ setProp : ( el : Cmp , prop : string , value : any ) => {
82
+ const ind = findElementId ( state . current , el ) ;
83
+ state . current [ ind ] . styles [ prop ] = value ;
84
+ changed ( state . current [ ind ] ) ;
85
+ } ,
86
+ removeProp : ( el : Cmp , prop : string ) => {
87
+ const ind = findElementId ( state . current , el ) ;
88
+ Reflect . deleteProperty ( state . current [ ind ] . styles , prop ) ;
89
+ changed ( state . current [ ind ] ) ;
90
+ } ,
91
+ hasProp : ( el : Cmp , prop : string ) => {
92
+ const ind = findElementId ( state . current , el ) ;
93
+ return Object . prototype . hasOwnProperty . call ( state . current [ ind ] . styles , prop ) ;
94
+ } ,
95
+ getProp : ( el : Cmp , prop : string ) => {
96
+ const ind = findElementId ( state . current , el ) ;
97
+ return state . current [ ind ] . styles [ prop ] ;
98
+ } ,
99
+ getStyles : ( el : Cmp ) => {
100
+ const ind = findElementId ( state . current , el ) ;
101
+ return state . current [ ind ] . styles ;
102
+ } ,
103
+ getElementId : ( el : Cmp ) => findElementId ( state . current , el ) ,
104
+ } ) ,
105
+ [ ] ,
106
+ ) ;
107
+
108
+ return < styleContext . Provider value = { ctx } > { children } </ styleContext . Provider > ;
109
109
}
0 commit comments