Skip to content

Commit

Permalink
Global Styles: Add theme origin to getStyle (#31267)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw authored Apr 29, 2021
1 parent 6fd72cd commit 12480aa
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions packages/edit-site/src/components/editor/global-styles-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export default function GlobalStylesProvider( { children, baseStyles } ) {

const contexts = useMemo( () => getContexts( blockTypes ), [ blockTypes ] );

const { __experimentalGlobalStylesBaseStyles: themeStyles } = settings;
const { userStyles, mergedStyles } = useMemo( () => {
let newUserStyles;
try {
Expand Down Expand Up @@ -191,11 +192,28 @@ export default function GlobalStylesProvider( { children, baseStyles } ) {
setContent( JSON.stringify( newContent ) );
},
getStyle: ( context, propertyName, origin = 'merged' ) => {
const styleOrigin =
'user' === origin ? userStyles : mergedStyles;
if ( origin === 'theme' ) {
const value = get(
themeStyles?.styles?.[ context ],
STYLE_PROPERTY[ propertyName ].value
);
return getValueFromVariable( themeStyles, context, value );
}

if ( origin === 'user' ) {
const value = get(
userStyles?.styles?.[ context ],
STYLE_PROPERTY[ propertyName ].value
);

// We still need to use merged styles here because the
// presets used to resolve user variable may be defined a
// layer down ( core, theme, or user ).
return getValueFromVariable( mergedStyles, context, value );
}

const value = get(
styleOrigin?.styles?.[ context ],
mergedStyles?.styles?.[ context ],
STYLE_PROPERTY[ propertyName ].value
);
return getValueFromVariable( mergedStyles, context, value );
Expand All @@ -220,7 +238,7 @@ export default function GlobalStylesProvider( { children, baseStyles } ) {
setContent( JSON.stringify( newContent ) );
},
} ),
[ content, mergedStyles ]
[ content, mergedStyles, themeStyles ]
);

useEffect( () => {
Expand Down

0 comments on commit 12480aa

Please sign in to comment.