-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove duplication of editor styles (#28837)
* Revert "Fix global styles in iframed site editor (#28731)" This reverts commit 9c206dd. * Iframe editor styles: remove duplication * Polish * Use useMergeRefs * Use React for rendering style elements * Fix package.json
- Loading branch information
Showing
7 changed files
with
66 additions
and
111 deletions.
There are no files selected for viewing
67 changes: 31 additions & 36 deletions
67
packages/block-editor/src/components/editor-styles/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,57 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { compact, map } from 'lodash'; | ||
import tinycolor from 'tinycolor2'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback, useRef } from '@wordpress/element'; | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import transformStyles from '../../utils/transform-styles'; | ||
|
||
function syncDarkThemeBodyClassname( node ) { | ||
const backgroundColor = window | ||
.getComputedStyle( node, null ) | ||
.getPropertyValue( 'background-color' ); | ||
|
||
const { ownerDocument } = node; | ||
const body = ownerDocument.getElementsByTagName( 'body' )[ 0 ]; | ||
|
||
if ( tinycolor( backgroundColor ).getLuminance() > 0.5 ) { | ||
body.classList.remove( 'is-dark-theme' ); | ||
} else { | ||
body.classList.add( 'is-dark-theme' ); | ||
} | ||
} | ||
|
||
export default function useEditorStyles( styles ) { | ||
const nodes = useRef( [] ); | ||
const EDITOR_STYLES_SELECTOR = '.editor-styles-wrapper'; | ||
|
||
function useDarkThemeBodyClassName( styles ) { | ||
return useCallback( | ||
( node ) => { | ||
if ( ! node ) { | ||
nodes.current.forEach( ( styleElement ) => | ||
styleElement.ownerDocument.body.removeChild( styleElement ) | ||
); | ||
return; | ||
} | ||
|
||
const updatedStyles = transformStyles( | ||
styles, | ||
'.editor-styles-wrapper' | ||
); | ||
|
||
const { ownerDocument } = node; | ||
nodes.current = map( compact( updatedStyles ), ( updatedCSS ) => { | ||
const styleElement = ownerDocument.createElement( 'style' ); | ||
styleElement.innerHTML = updatedCSS; | ||
ownerDocument.body.appendChild( styleElement ); | ||
|
||
return styleElement; | ||
} ); | ||
|
||
syncDarkThemeBodyClassname( node ); | ||
const { defaultView, body } = ownerDocument; | ||
const canvas = ownerDocument.querySelector( | ||
EDITOR_STYLES_SELECTOR | ||
); | ||
const backgroundColor = defaultView | ||
.getComputedStyle( canvas, null ) | ||
.getPropertyValue( 'background-color' ); | ||
|
||
if ( tinycolor( backgroundColor ).getLuminance() > 0.5 ) { | ||
body.classList.remove( 'is-dark-theme' ); | ||
} else { | ||
body.classList.add( 'is-dark-theme' ); | ||
} | ||
}, | ||
[ styles ] | ||
); | ||
} | ||
|
||
export default function EditorStyles( { styles } ) { | ||
return ( | ||
<> | ||
{ /* Use an empty style element to have a document reference, | ||
but this could be any element. */ } | ||
<style ref={ useDarkThemeBodyClassName( styles ) } /> | ||
{ transformStyles( styles, EDITOR_STYLES_SELECTOR ).map( | ||
( css, index ) => ( | ||
<style key={ index }>{ css }</style> | ||
) | ||
) } | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters