Skip to content

Commit f3515d6

Browse files
Keep the -ms- prefix lowercase when camelCasing style keys
React style object keys uppercase the first letter of every vendor prefix except -ms-, which stays lowercase (msFlexAlign, not MsFlexAlign). Move the conversion into a helper so the exception has somewhere to live.
1 parent 44b48c1 commit f3515d6

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

‎packages/block-editor/src/hooks/style.jsx‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,34 @@ const styleSupportKeys = [
7272
const hasStyleSupport = ( nameOrType ) =>
7373
styleSupportKeys.some( ( key ) => hasBlockSupport( nameOrType, key ) );
7474

75+
/**
76+
* Converts a CSS property name into the key a React style object expects.
77+
*
78+
* Vendor-prefixed properties are camelCased without their leading dash, so
79+
* `-webkit-background-clip` becomes `WebkitBackgroundClip`.
80+
*
81+
* @param {string} key CSS property name.
82+
*
83+
* @return {string} React style object key.
84+
*/
85+
function getReactStyleKey( key ) {
86+
// Custom properties start with two dashes. React writes them verbatim.
87+
if ( ! key.startsWith( '-' ) || key.startsWith( '--' ) ) {
88+
return key;
89+
}
90+
91+
const camelCased = key
92+
.slice( 1 )
93+
.replace( /-([a-z])/g, ( _, character ) => character.toUpperCase() );
94+
95+
// `-ms-` is the one prefix React keeps lowercase, e.g. `msFlexAlign`.
96+
return key.startsWith( '-ms-' )
97+
? camelCased
98+
: camelCased.replace( /^[a-z]/, ( character ) =>
99+
character.toUpperCase()
100+
);
101+
}
102+
75103
/**
76104
* Returns the inline styles to add depending on the style object
77105
*
@@ -84,20 +112,7 @@ export function getInlineStyles( styles = {} ) {
84112
// The goal is to move everything to server side generated engine styles
85113
// This is temporary as we absorb more and more styles into the engine.
86114
getCSSRules( styles ).forEach( ( rule ) => {
87-
// Vendor-prefixed CSS properties start with a single dash (e.g.
88-
// `-webkit-background-clip`). React style objects require camelCase
89-
// keys without the leading dash (e.g. `WebkitBackgroundClip`), so
90-
// convert those. Custom properties start with two and are passed
91-
// through, since React writes them verbatim.
92-
const isVendorPrefixed =
93-
rule.key.startsWith( '-' ) && ! rule.key.startsWith( '--' );
94-
const key = isVendorPrefixed
95-
? rule.key
96-
.slice( 1 )
97-
.replace( /-([a-z])/g, ( _, c ) => c.toUpperCase() )
98-
.replace( /^[a-z]/, ( c ) => c.toUpperCase() )
99-
: rule.key;
100-
output[ key ] = rule.value;
115+
output[ getReactStyleKey( rule.key ) ] = rule.value;
101116
} );
102117

103118
return output;

0 commit comments

Comments
 (0)