Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F101398567
styleFunctionSx.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Feb 10, 02:03
Size
3 KB
Mime Type
text/x-java
Expires
Wed, Feb 12, 02:03 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24151943
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
styleFunctionSx.js
View Options
import
{
unstable_capitalize
as
capitalize
}
from
'@mui/utils'
;
import
merge
from
'../merge'
;
import
{
getPath
,
getStyleValue
as
getValue
}
from
'../style'
;
import
{
handleBreakpoints
,
createEmptyBreakpointObject
,
removeUnusedBreakpoints
}
from
'../breakpoints'
;
import
defaultSxConfig
from
'./defaultSxConfig'
;
function
objectsHaveSameKeys
(...
objects
)
{
const
allKeys
=
objects
.
reduce
((
keys
,
object
)
=>
keys
.
concat
(
Object
.
keys
(
object
)),
[]);
const
union
=
new
Set
(
allKeys
);
return
objects
.
every
(
object
=>
union
.
size
===
Object
.
keys
(
object
).
length
);
}
function
callIfFn
(
maybeFn
,
arg
)
{
return
typeof
maybeFn
===
'function'
?
maybeFn
(
arg
)
:
maybeFn
;
}
// eslint-disable-next-line @typescript-eslint/naming-convention
export
function
unstable_createStyleFunctionSx
()
{
function
getThemeValue
(
prop
,
val
,
theme
,
config
)
{
const
props
=
{
[
prop
]
:
val
,
theme
};
const
options
=
config
[
prop
];
if
(
!
options
)
{
return
{
[
prop
]
:
val
};
}
const
{
cssProperty
=
prop
,
themeKey
,
transform
,
style
}
=
options
;
if
(
val
==
null
)
{
return
null
;
}
const
themeMapping
=
getPath
(
theme
,
themeKey
)
||
{};
if
(
style
)
{
return
style
(
props
);
}
const
styleFromPropValue
=
propValueFinal
=>
{
let
value
=
getValue
(
themeMapping
,
transform
,
propValueFinal
);
if
(
propValueFinal
===
value
&&
typeof
propValueFinal
===
'string'
)
{
// Haven't found value
value
=
getValue
(
themeMapping
,
transform
,
`
$
{
prop
}
$
{
propValueFinal
===
'default'
?
''
:
capitalize
(
propValueFinal
)}
`
,
propValueFinal
);
}
if
(
cssProperty
===
false
)
{
return
value
;
}
return
{
[
cssProperty
]
:
value
};
};
return
handleBreakpoints
(
props
,
val
,
styleFromPropValue
);
}
function
styleFunctionSx
(
props
)
{
var
_theme$unstable_sxCon
;
const
{
sx
,
theme
=
{}
}
=
props
||
{};
if
(
!
sx
)
{
return
null
;
// Emotion & styled-components will neglect null
}
const
config
=
(
_theme$unstable_sxCon
=
theme
.
unstable_sxConfig
)
!=
null
?
_theme$unstable_sxCon
:
defaultSxConfig
;
/*
* Receive `sxInput` as object or callback
* and then recursively check keys & values to create media query object styles.
* (the result will be used in `styled`)
*/
function
traverse
(
sxInput
)
{
let
sxObject
=
sxInput
;
if
(
typeof
sxInput
===
'function'
)
{
sxObject
=
sxInput
(
theme
);
}
else
if
(
typeof
sxInput
!==
'object'
)
{
// value
return
sxInput
;
}
if
(
!
sxObject
)
{
return
null
;
}
const
emptyBreakpoints
=
createEmptyBreakpointObject
(
theme
.
breakpoints
);
const
breakpointsKeys
=
Object
.
keys
(
emptyBreakpoints
);
let
css
=
emptyBreakpoints
;
Object
.
keys
(
sxObject
).
forEach
(
styleKey
=>
{
const
value
=
callIfFn
(
sxObject
[
styleKey
],
theme
);
if
(
value
!==
null
&&
value
!==
undefined
)
{
if
(
typeof
value
===
'object'
)
{
if
(
config
[
styleKey
])
{
css
=
merge
(
css
,
getThemeValue
(
styleKey
,
value
,
theme
,
config
));
}
else
{
const
breakpointsValues
=
handleBreakpoints
({
theme
},
value
,
x
=>
({
[
styleKey
]
:
x
}));
if
(
objectsHaveSameKeys
(
breakpointsValues
,
value
))
{
css
[
styleKey
]
=
styleFunctionSx
({
sx
:
value
,
theme
});
}
else
{
css
=
merge
(
css
,
breakpointsValues
);
}
}
}
else
{
css
=
merge
(
css
,
getThemeValue
(
styleKey
,
value
,
theme
,
config
));
}
}
});
return
removeUnusedBreakpoints
(
breakpointsKeys
,
css
);
}
return
Array
.
isArray
(
sx
)
?
sx
.
map
(
traverse
)
:
traverse
(
sx
);
}
return
styleFunctionSx
;
}
const
styleFunctionSx
=
unstable_createStyleFunctionSx
();
styleFunctionSx
.
filterProps
=
[
'sx'
];
export
default
styleFunctionSx
;
Event Timeline
Log In to Comment