Skip to content

Commit 1b3c1d0

Browse files
authored
fix (button block): autofocus when adding buttons (#3339)
1 parent de4cb54 commit 1b3c1d0

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/block/button-group/edit.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
FlexGapControls,
3434
Transform,
3535
} from '~stackable/block-components'
36-
import { useBlockContext, useDeviceType } from '~stackable/hooks'
36+
import { useDeviceType } from '~stackable/hooks'
3737
import {
3838
withBlockAttributeContext,
3939
withBlockWrapperIsHovered,
@@ -49,6 +49,7 @@ import {
4949
} from '@wordpress/block-editor'
5050
import { sprintf, __ } from '@wordpress/i18n'
5151
import { memo } from '@wordpress/element'
52+
import { useSelect } from '@wordpress/data'
5253

5354
const ALLOWED_INNER_BLOCKS = [ 'stackable/button', 'stackable/icon-button' ]
5455

@@ -109,7 +110,12 @@ const Edit = props => {
109110
const deviceType = useDeviceType()
110111
const rowClass = getRowClasses( attributes )
111112
const blockAlignmentClass = getAlignmentClasses( attributes )
112-
const { hasInnerBlocks } = useBlockContext()
113+
const { hasInnerBlocks } = useSelect( select => {
114+
const { getBlockOrder } = select( 'core/block-editor' )
115+
return {
116+
hasInnerBlocks: getBlockOrder( props.clientId ).length > 0,
117+
}
118+
}, [ props.clientId ] )
113119

114120
const blockClassNames = classnames( [
115121
className,
@@ -176,16 +182,15 @@ const Edit = props => {
176182
<CustomCSS mainBlockClass="stk-block-button-group" />
177183

178184
{ ! hasInnerBlocks && <GroupPlaceholder blockName="stackable/button" /> }
179-
{ hasInnerBlocks &&
180-
<div className={ contentClassNames }>
181-
<InnerBlocks
182-
orientation="horizontal"
183-
allowedBlocks={ ALLOWED_INNER_BLOCKS }
184-
template={ TEMPLATE }
185-
templateInsertUpdatesSelection={ true }
186-
/>
187-
</div>
188-
}
185+
<div className={ contentClassNames }>
186+
<InnerBlocks
187+
orientation="horizontal"
188+
allowedBlocks={ ALLOWED_INNER_BLOCKS }
189+
template={ TEMPLATE }
190+
templateInsertUpdatesSelection={ true }
191+
renderAppender={ hasInnerBlocks ? undefined : false }
192+
/>
193+
</div>
189194
</BlockDiv>
190195
{ props.isHovered && hasInnerBlocks && <MarginBottom /> }
191196
</>

src/block/button-group/variations.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ const variations = [
3131
'stk-type': 'essential',
3232
'stk-demo': 'https://wpstackable.com/button-block/?utm_source=welcome&utm_medium=settings&utm_campaign=view_demo&utm_content=demolink',
3333
isDefault: true,
34-
innerBlocks: [
35-
[
36-
'stackable/button',
37-
{},
38-
],
39-
],
34+
innerBlocks: [],
4035
example: buttonExample,
4136
},
4237
{

0 commit comments

Comments
 (0)