-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Layout: use override editor style API #54466
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,29 +9,28 @@ import classnames from 'classnames'; | |
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
Button, | ||
ButtonGroup, | ||
ToggleControl, | ||
PanelBody, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useContext, createPortal } from '@wordpress/element'; | ||
import { useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../store'; | ||
import { InspectorControls } from '../components'; | ||
import useSetting from '../components/use-setting'; | ||
import { LayoutStyle } from '../components/block-list/layout'; | ||
import BlockList from '../components/block-list'; | ||
import { getLayoutType, getLayoutTypes } from '../layouts'; | ||
import { useBlockEditingMode } from '../components/block-editing-mode'; | ||
import { LAYOUT_DEFINITIONS } from '../layouts/definitions'; | ||
import { kebabCase } from '../utils/object'; | ||
import { useBlockSettings } from './utils'; | ||
import { unlock } from '../lock-unlock'; | ||
|
||
const layoutBlockSupportKey = 'layout'; | ||
|
||
|
@@ -364,7 +363,6 @@ export const withLayoutStyles = createHigherOrderComponent( | |
const shouldRenderLayoutStyles = | ||
blockSupportsLayout && ! disableLayoutStyles; | ||
const id = useInstanceId( BlockListBlock ); | ||
const element = useContext( BlockList.__unstableElementContext ); | ||
const { layout } = attributes; | ||
const { default: defaultBlockLayout } = | ||
getBlockSupport( name, layoutBlockSupportKey ) || {}; | ||
|
@@ -404,26 +402,23 @@ export const withLayoutStyles = createHigherOrderComponent( | |
layoutClasses | ||
); | ||
|
||
const { setStyleOverride, deleteStyleOverride } = unlock( | ||
useDispatch( blockEditorStore ) | ||
); | ||
|
||
useEffect( () => { | ||
if ( ! css ) return; | ||
setStyleOverride( id, { css } ); | ||
return () => { | ||
deleteStyleOverride( id ); | ||
}; | ||
}, [ id, css, setStyleOverride, deleteStyleOverride ] ); | ||
|
||
return ( | ||
<> | ||
{ shouldRenderLayoutStyles && | ||
element && | ||
!! css && | ||
createPortal( | ||
<LayoutStyle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what replaced this component? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh looking at the component itself, it does nothing if it has a "css" prop, it just renders that css. Makes me think we should remove that css prop from the LayoutStyle component and just use |
||
blockName={ name } | ||
selector={ selector } | ||
css={ css } | ||
layout={ usedLayout } | ||
style={ attributes?.style } | ||
/>, | ||
element | ||
) } | ||
<BlockListBlock | ||
{ ...props } | ||
__unstableLayoutClassNames={ layoutClassNames } | ||
/> | ||
</> | ||
<BlockListBlock | ||
{ ...props } | ||
__unstableLayoutClassNames={ layoutClassNames } | ||
/> | ||
); | ||
}, | ||
'withLayoutStyles' | ||
|
@@ -449,7 +444,6 @@ export const withChildLayoutStyles = createHigherOrderComponent( | |
const shouldRenderChildLayoutStyles = | ||
hasChildLayout && ! disableLayoutStyles; | ||
|
||
const element = useContext( BlockList.__unstableElementContext ); | ||
const id = useInstanceId( BlockListBlock ); | ||
const selector = `.wp-container-content-${ id }`; | ||
|
||
|
@@ -472,15 +466,19 @@ export const withChildLayoutStyles = createHigherOrderComponent( | |
shouldRenderChildLayoutStyles && !! css, // Only attach a container class if there is generated CSS to be attached. | ||
} ); | ||
|
||
return ( | ||
<> | ||
{ shouldRenderChildLayoutStyles && | ||
element && | ||
!! css && | ||
createPortal( <style>{ css }</style>, element ) } | ||
<BlockListBlock { ...props } className={ className } /> | ||
</> | ||
const { setStyleOverride, deleteStyleOverride } = unlock( | ||
useDispatch( blockEditorStore ) | ||
); | ||
|
||
useEffect( () => { | ||
if ( ! css ) return; | ||
setStyleOverride( id, { css } ); | ||
return () => { | ||
deleteStyleOverride( id ); | ||
}; | ||
}, [ id, css, setStyleOverride, deleteStyleOverride ] ); | ||
|
||
return <BlockListBlock { ...props } className={ className } />; | ||
}, | ||
'withChildLayoutStyles' | ||
); | ||
|
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 wonder if it makes sense to have a
useStyleOverride
hook as this seems to be common right? At least an internal one.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.
Maybe!