-
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.
Add: Option to select the style that is automatically applied (#16465)
Squashed commits: [ffd16b5] Add unit tests [51dd9dc] Refactor & changed the design [3b9ac24] Add: Option to allow user to select the style that is automatically applied.
- Loading branch information
1 parent
c8ed262
commit 7bc32cc
Showing
10 changed files
with
372 additions
and
30 deletions.
There are no files selected for viewing
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
60 changes: 60 additions & 0 deletions
60
packages/block-editor/src/components/default-style-picker/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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useCallback } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { SelectControl } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
export default function DefaultStylePicker( { blockName } ) { | ||
const { | ||
preferredStyle, | ||
onUpdatePreferredStyleVariations, | ||
styles, | ||
} = useSelect( | ||
( select ) => { | ||
const settings = select( 'core/block-editor' ).getSettings(); | ||
const preferredStyleVariations = settings.__experimentalPreferredStyleVariations; | ||
return { | ||
preferredStyle: get( | ||
preferredStyleVariations, | ||
[ 'value', blockName ] | ||
), | ||
onUpdatePreferredStyleVariations: get( | ||
preferredStyleVariations, | ||
[ 'onChange' ], | ||
null | ||
), | ||
styles: select( 'core/blocks' ).getBlockStyles( blockName ), | ||
}; | ||
}, | ||
[ blockName ] | ||
); | ||
const selectOptions = useMemo( | ||
() => ( [ | ||
{ label: __( 'Not set' ), value: '' }, | ||
...styles.map( ( { label, name } ) => ( { label, value: name } ) ), | ||
] ), | ||
[ styles ], | ||
); | ||
const selectOnChange = useCallback( | ||
( blockStyle ) => { | ||
onUpdatePreferredStyleVariations( blockName, blockStyle ); | ||
}, | ||
[ blockName, onUpdatePreferredStyleVariations ] | ||
); | ||
|
||
return onUpdatePreferredStyleVariations && ( | ||
<SelectControl | ||
options={ selectOptions } | ||
value={ preferredStyle || '' } | ||
label={ __( 'Default Style' ) } | ||
onChange={ selectOnChange } | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.