Skip to content

Commit

Permalink
fix: apply custom styles even if styling tab in sidebar is closed (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek authored Jun 26, 2020
1 parent 5db6b97 commit 20b08bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/newsletter-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import InitModal from '../components/init-modal';
import Layout from './layout/';
import Sidebar from './sidebar/';
import Testing from './testing/';
import Styling from './styling/';
import { Styling, ApplyStyling } from './styling/';
import registerEditorPlugin from './editor/';

registerEditorPlugin();
Expand Down Expand Up @@ -55,6 +55,8 @@ const NewsletterEdit = ( { layoutId } ) => {
>
<Layout />
</PluginDocumentSettingPanel>

<ApplyStyling />
</Fragment>
);
};
Expand Down
45 changes: 29 additions & 16 deletions src/newsletter-editor/styling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,42 @@ const fontOptgroups = [
},
];

export default compose( [
const customStylesSelector = select => {
const { getEditedPostAttribute } = select( 'core/editor' );
const meta = getEditedPostAttribute( 'meta' );
return {
fontBody: meta.font_body || '',
fontHeader: meta.font_header || '',
backgroundColor: meta.background_color || '',
};
};

export const ApplyStyling = withSelect( customStylesSelector )(
( { fontBody, fontHeader, backgroundColor } ) => {
useEffect(() => {
document.documentElement.style.setProperty( '--body-font', fontBody );
}, [ fontBody ]);
useEffect(() => {
document.documentElement.style.setProperty( '--header-font', fontHeader );
}, [ fontHeader ]);
useEffect(() => {
document.querySelector( '.edit-post-visual-editor' ).style.backgroundColor = backgroundColor;
}, [ backgroundColor ]);

return null;
}
);

export const Styling = compose( [
withDispatch( dispatch => {
const { editPost } = dispatch( 'core/editor' );
return { editPost };
} ),
withSelect( select => {
const { getEditedPostAttribute, getCurrentPostId } = select( 'core/editor' );
const meta = getEditedPostAttribute( 'meta' );
const { getCurrentPostId } = select( 'core/editor' );
return {
postId: getCurrentPostId(),
fontBody: meta.font_body || '',
fontHeader: meta.font_header || '',
backgroundColor: meta.background_color || '',
...customStylesSelector( select ),
};
} ),
] )( ( { editPost, fontBody, fontHeader, backgroundColor, postId } ) => {
Expand All @@ -86,16 +109,6 @@ export default compose( [
} );
};

useEffect(() => {
document.documentElement.style.setProperty( '--body-font', fontBody );
}, [ fontBody ]);
useEffect(() => {
document.documentElement.style.setProperty( '--header-font', fontHeader );
}, [ fontHeader ]);
useEffect(() => {
document.querySelector( '.edit-post-visual-editor' ).style.backgroundColor = backgroundColor;
}, [ backgroundColor ]);

const instanceId = useInstanceId( SelectControlWithOptGroup );
const id = `inspector-select-control-${ instanceId }`;

Expand Down

0 comments on commit 20b08bc

Please sign in to comment.