11import { ReactUnity } from '@reactunity/renderer' ;
2- import React , { useContext , useMemo , useRef } from " react" ;
2+ import React , { useContext , useMemo , useRef } from ' react' ;
33
44type Cmp = ReactUnity . IReactComponent ;
55type Styles = Record < string , object > ;
@@ -15,7 +15,7 @@ const styleContext = React.createContext<ContextType>(null);
1515export const useStyleContext = ( ) => useContext ( styleContext ) ;
1616
1717interface ElementProps {
18- element : Cmp ,
18+ element : Cmp ;
1919 styles : Styles ;
2020 ind : number ;
2121 sheet : any ;
@@ -24,13 +24,13 @@ interface ElementProps {
2424type State = ElementProps [ ] ;
2525
2626const findElementId = ( state : State , el : Cmp ) => {
27- let ind = state . findIndex ( x => x . element === el ) ;
27+ let ind = state . findIndex ( ( x ) => x . element === el ) ;
2828
2929 if ( ind < 0 ) {
3030 ind = state . length ;
3131 const st = { element : el , styles : { } , ind } as ElementProps ;
3232 state . push ( st ) ;
33- el . SetData ( 'devtools-el' , ind + '' ) ;
33+ el . SetData ( 'devtools-el' , String ( ind ) ) ;
3434 }
3535
3636 return ind ;
@@ -54,8 +54,7 @@ const buildSheet = (state: ElementProps) => {
5454 }
5555 }
5656
57- if ( values . length )
58- sheet . AddRules ( selector , valuesDic ) ;
57+ if ( values . length ) sheet . AddRules ( selector , valuesDic ) ;
5958
6059 state . sheet = sheet ;
6160
@@ -77,33 +76,34 @@ const changed = (state: ElementProps) => {
7776export function StyleContext ( { children } ) {
7877 const state = useRef < State > ( [ ] ) ;
7978
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 > ;
109109}
0 commit comments