-
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
WIP: [Block Library - Social Icons]: Make the block use the new flex
layout
#33987
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 |
---|---|---|
|
@@ -12,10 +12,11 @@ import { addFilter } from '@wordpress/hooks'; | |
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { | ||
Button, | ||
ButtonGroup, | ||
BaseControl, | ||
ToggleControl, | ||
PanelBody, | ||
__experimentalSegmentedControl as SegmentedControl, | ||
__experimentalSegmentedControlOption as SegmentedControlOption, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useContext, createPortal } from '@wordpress/element'; | ||
|
@@ -32,40 +33,40 @@ import { getLayoutType, getLayoutTypes } from '../layouts'; | |
|
||
const layoutBlockSupportKey = '__experimentalLayout'; | ||
|
||
const canBlockSwitchLayout = ( blockTypeOrName ) => { | ||
const layoutBlockSupportConfig = getBlockSupport( | ||
blockTypeOrName, | ||
layoutBlockSupportKey | ||
); | ||
|
||
return layoutBlockSupportConfig?.allowSwitching; | ||
}; | ||
|
||
function LayoutPanel( { setAttributes, attributes, name: blockName } ) { | ||
const { layout = {} } = attributes; | ||
const defaultLayout = useSetting( 'layout' ); | ||
const { layout } = attributes; | ||
// TODO: check if a theme should provide default values per `layoutType`. | ||
// Fow now we use the values from `flow` (content, width). | ||
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. Not sure what you mean here? 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. For now the default layout value in 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. In my mind, the theme provides a single default layout and chooses its type and doesn't provide defaults for all layouts. The default layout is automatically used in the post editor canvas. 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. So the provided by the theme default should be only about 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. maybe a theme would want to use a "grid" layout by default or some other layout we didn't build yet but yeah in most cases, it will be a "flow" one I think. |
||
const defaultThemeLayout = useSetting( 'layout' ); | ||
const themeSupportsLayout = useSelect( ( select ) => { | ||
const { getSettings } = select( blockEditorStore ); | ||
return getSettings().supportsLayout; | ||
}, [] ); | ||
|
||
// TODO: If we implement layout for blocks and replace | ||
// current display of them like this PR explores, shouldn't this check change? | ||
if ( ! themeSupportsLayout ) { | ||
return null; | ||
} | ||
|
||
const allowLayoutSwitching = canBlockSwitchLayout( blockName ); | ||
const { inherit = false, type = 'default' } = layout; | ||
const { | ||
allowSwitching: canBlockSwitchLayout, | ||
default: defaultBlockLayout, | ||
} = getBlockSupport( blockName, layoutBlockSupportKey ) || {}; | ||
|
||
const usedLayout = layout ? layout : defaultBlockLayout || {}; | ||
const { inherit = false, type = 'default' } = usedLayout; | ||
const layoutType = getLayoutType( type ); | ||
|
||
const onChangeType = ( newType ) => | ||
setAttributes( { layout: { type: newType } } ); | ||
setAttributes( { layout: { type: newType } } ); // TODO needs checking with block defaults. | ||
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. Can you clarify this comment? 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'm not sure yet 😄 . I'm adding some comments to check it more when the time comes. I just thought I should check if we would need to add the |
||
const onChangeLayout = ( newLayout ) => | ||
setAttributes( { layout: newLayout } ); | ||
|
||
return ( | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Layout' ) }> | ||
{ !! defaultLayout && ( | ||
{ inherit && !! defaultThemeLayout && ( | ||
<ToggleControl | ||
label={ __( 'Inherit default layout' ) } | ||
checked={ !! inherit } | ||
|
@@ -74,8 +75,7 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) { | |
} | ||
/> | ||
) } | ||
|
||
{ ! inherit && allowLayoutSwitching && ( | ||
{ ! inherit && canBlockSwitchLayout && ( | ||
<LayoutTypeSwitcher | ||
type={ type } | ||
onChange={ onChangeType } | ||
|
@@ -84,7 +84,7 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) { | |
|
||
{ ! inherit && layoutType && ( | ||
<layoutType.edit | ||
layout={ layout } | ||
layout={ usedLayout } | ||
onChange={ onChangeLayout } | ||
/> | ||
) } | ||
|
@@ -95,19 +95,21 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) { | |
|
||
function LayoutTypeSwitcher( { type, onChange } ) { | ||
return ( | ||
<ButtonGroup> | ||
{ getLayoutTypes().map( ( { name, label } ) => { | ||
return ( | ||
<Button | ||
<BaseControl> | ||
ntsekouras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<SegmentedControl | ||
value={ type } | ||
onChange={ onChange } | ||
isBlock={ true } | ||
> | ||
{ getLayoutTypes().map( ( { name, label } ) => ( | ||
<SegmentedControlOption | ||
key={ name } | ||
isPressed={ type === name } | ||
onClick={ () => onChange( name ) } | ||
> | ||
{ label } | ||
</Button> | ||
); | ||
} ) } | ||
</ButtonGroup> | ||
value={ name } | ||
label={ label } | ||
></SegmentedControlOption> | ||
) ) } | ||
</SegmentedControl> | ||
</BaseControl> | ||
); | ||
} | ||
|
||
|
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 was expecting more changes here to retrieve the "default layout" from the block support config and apply it when the "layout" attribute is empty.