-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove duplication of editor styles #28837
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b10eb08
Revert "Fix global styles in iframed site editor (#28731)"
ellatrix 7aefe19
Iframe editor styles: remove duplication
ellatrix 1eab6fb
Polish
ellatrix 97956c3
Use useMergeRefs
ellatrix 5e39da8
Use React for rendering style elements
ellatrix 5c981c8
Fix package.json
ellatrix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is wrong? Should be
[ styles ]
. Or am I missing something?In practice, passing dependencies variable around is rarely a good practice, as it's very easy to miss these kinds of bugs. That's also why #24914 disable this usage. Writing them all explicitly is not a bad idea IMO.
useDarkThemeBodyClassName
could probably just be written as:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
styles
is an array so it would only update if one of the values changes. Not sure when the array reference itself changes. I'll change it back to what it was.