-
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
Duotone: add block controls on the inspector #49838
Changes from 7 commits
8da1330
cf970bc
af984c0
057be54
f96d202
799269e
b8f60bc
aabd119
6e7e6ec
3b5bb9a
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 |
---|---|---|
@@ -1,11 +1,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalToolsPanel as ToolsPanel, | ||
__experimentalToolsPanelItem as ToolsPanelItem, | ||
__experimentalItemGroup as ItemGroup, | ||
__experimentalHStack as HStack, | ||
__experimentalZStack as ZStack, | ||
__experimentalVStack as VStack, | ||
__experimentalDropdownContentWrapper as DropdownContentWrapper, | ||
Button, | ||
ColorIndicator, | ||
DuotonePicker, | ||
DuotoneSwatch, | ||
Dropdown, | ||
Flex, | ||
FlexItem, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useCallback, useMemo } from '@wordpress/element'; | ||
|
@@ -75,6 +90,37 @@ const DEFAULT_CONTROLS = { | |
duotone: true, | ||
}; | ||
|
||
const popoverProps = { | ||
placement: 'left-start', | ||
offset: 36, | ||
shift: true, | ||
className: 'block-editor-duotone-control__popover', | ||
headerTitle: __( 'Duotone' ), | ||
variant: 'toolbar', | ||
}; | ||
|
||
const LabeledColorIndicators = ( { indicators, label } ) => ( | ||
MaggieCabrera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<HStack justify="flex-start"> | ||
<ZStack isLayered={ false } offset={ -8 }> | ||
{ indicators.map( ( indicator, index ) => ( | ||
<Flex key={ index } expanded={ false }> | ||
{ indicator === 'unset' || ! indicator ? ( | ||
<ColorIndicator className="block-editor-duotone-control__unset-indicator" /> | ||
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 saw this check and this class in a couple places, It seems to me that the 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 will tackle this one when refactoring the |
||
) : ( | ||
<DuotoneSwatch values={ indicator } /> | ||
) } | ||
</Flex> | ||
) ) } | ||
</ZStack> | ||
<FlexItem | ||
className="block-editor-panel-color-gradient-settings__color-name" | ||
MaggieCabrera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
title={ label } | ||
> | ||
{ label } | ||
</FlexItem> | ||
</HStack> | ||
); | ||
|
||
export default function FiltersPanel( { | ||
as: Wrapper = FiltersToolsPanel, | ||
value, | ||
|
@@ -110,6 +156,11 @@ export default function FiltersPanel( { | |
const hasDuotone = () => !! value?.filter?.duotone; | ||
const resetDuotone = () => setDuotone( undefined ); | ||
|
||
const disableCustomColors = ! settings?.color?.custom; | ||
const disableCustomDuotone = | ||
! settings?.color?.customDuotone || | ||
( colorPalette?.length === 0 && disableCustomColors ); | ||
|
||
const resetAllFilter = useCallback( ( previousValue ) => { | ||
return { | ||
...previousValue, | ||
|
@@ -135,21 +186,54 @@ export default function FiltersPanel( { | |
isShownByDefault={ defaultControls.duotone } | ||
panelId={ panelId } | ||
> | ||
<VStack> | ||
<p> | ||
{ __( | ||
'Create a two-tone color effect without losing your original image.' | ||
) } | ||
</p> | ||
<DuotonePicker | ||
colorPalette={ colorPalette } | ||
duotonePalette={ duotonePalette } | ||
disableCustomColors={ true } | ||
disableCustomDuotone={ true } | ||
value={ duotone } | ||
onChange={ setDuotone } | ||
/> | ||
</VStack> | ||
<Dropdown | ||
popoverProps={ popoverProps } | ||
className="block-editor-tools-panel-color-gradient-settings__dropdown" | ||
MaggieCabrera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
renderToggle={ ( { onToggle, isOpen } ) => { | ||
const toggleProps = { | ||
onClick: onToggle, | ||
className: classnames( | ||
'block-editor-panel-color-gradient-settings__dropdown', | ||
{ 'is-open': isOpen } | ||
), | ||
'aria-expanded': isOpen, | ||
}; | ||
|
||
return ( | ||
<ItemGroup isBordered isSeparated> | ||
<Button { ...toggleProps }> | ||
<LabeledColorIndicators | ||
indicators={ [ duotone ] } | ||
label={ __( 'Duotone' ) } | ||
/> | ||
</Button> | ||
</ItemGroup> | ||
); | ||
} } | ||
renderContent={ () => ( | ||
<DropdownContentWrapper paddingSize="medium"> | ||
<VStack> | ||
<p> | ||
{ __( | ||
'Create a two-tone color effect without losing your original image.' | ||
) } | ||
</p> | ||
<DuotonePicker | ||
colorPalette={ colorPalette } | ||
duotonePalette={ duotonePalette } | ||
disableCustomColors={ | ||
disableCustomColors | ||
} | ||
disableCustomDuotone={ | ||
disableCustomDuotone | ||
} | ||
value={ duotone } | ||
onChange={ setDuotone } | ||
/> | ||
</VStack> | ||
</DropdownContentWrapper> | ||
) } | ||
/> | ||
</ToolsPanelItem> | ||
) } | ||
</Wrapper> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ const StylesTab = ( { blockName, clientId, hasBlockStyles } ) => { | |
label={ __( 'Color' ) } | ||
className="color-block-support-panel__inner-wrapper" | ||
/> | ||
<InspectorControls.Slot group="filter" /> | ||
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. @jasmussen is this a proper order for the filters? I put it after color because it's what made sense to me |
||
<InspectorControls.Slot | ||
group="typography" | ||
label={ __( 'Typography' ) } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import { useSelect } from '@wordpress/data'; | |
*/ | ||
import { | ||
BlockControls, | ||
InspectorControls, | ||
__experimentalDuotoneControl as DuotoneControl, | ||
useSetting, | ||
} from '../components'; | ||
|
@@ -34,7 +35,9 @@ import { | |
} from '../components/duotone'; | ||
import { getBlockCSSSelector } from '../components/global-styles/get-block-css-selector'; | ||
import { scopeSelector } from '../components/global-styles/utils'; | ||
import { useBlockSettings } from './utils'; | ||
import { store as blockEditorStore } from '../store'; | ||
import { default as StylesFiltersPanel } from '../components/global-styles/filters-panel'; | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
|
@@ -106,9 +109,10 @@ export function getDuotonePresetFromColors( colors, duotonePalette ) { | |
return preset ? `var:preset|duotone|${ preset.slug }` : undefined; | ||
} | ||
|
||
function DuotonePanel( { attributes, setAttributes } ) { | ||
function DuotonePanel( { attributes, setAttributes, name } ) { | ||
const style = attributes?.style; | ||
const duotoneStyle = style?.color?.duotone; | ||
const settings = useBlockSettings( name ); | ||
|
||
const duotonePalette = useMultiOriginPresets( { | ||
presetSetting: 'color.duotone', | ||
|
@@ -132,30 +136,47 @@ function DuotonePanel( { attributes, setAttributes } ) { | |
: duotoneStyle; | ||
|
||
return ( | ||
<BlockControls group="block" __experimentalShareWithChildBlocks> | ||
<DuotoneControl | ||
duotonePalette={ duotonePalette } | ||
colorPalette={ colorPalette } | ||
disableCustomDuotone={ disableCustomDuotone } | ||
disableCustomColors={ disableCustomColors } | ||
value={ duotonePresetOrColors } | ||
onChange={ ( newDuotone ) => { | ||
const maybePreset = getDuotonePresetFromColors( | ||
newDuotone, | ||
duotonePalette | ||
); | ||
|
||
const newStyle = { | ||
...style, | ||
color: { | ||
...style?.color, | ||
duotone: maybePreset ?? newDuotone, // use preset or fallback to custom colors. | ||
}, | ||
}; | ||
setAttributes( { style: newStyle } ); | ||
} } | ||
/> | ||
</BlockControls> | ||
<> | ||
<InspectorControls group="filter"> | ||
<StylesFiltersPanel | ||
value={ { filter: { duotone: duotonePresetOrColors } } } | ||
onChange={ ( newDuotone ) => { | ||
const newStyle = { | ||
color: { | ||
...newDuotone?.filter, | ||
}, | ||
}; | ||
setAttributes( { style: newStyle } ); | ||
} } | ||
settings={ settings } | ||
/> | ||
</InspectorControls> | ||
<BlockControls group="block" __experimentalShareWithChildBlocks> | ||
<DuotoneControl | ||
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. @youknowriad we tried to use the 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. yes, this is a new use case (block toolbar). Maybe there's a way to make these controllers agnostic here too by providing a specific Wrapper prop, did we try that? 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. yes, we did that, and the 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. /cc @ajlende 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 guess it's because of a missing |
||
duotonePalette={ duotonePalette } | ||
colorPalette={ colorPalette } | ||
disableCustomDuotone={ disableCustomDuotone } | ||
disableCustomColors={ disableCustomColors } | ||
value={ duotonePresetOrColors } | ||
onChange={ ( newDuotone ) => { | ||
const maybePreset = getDuotonePresetFromColors( | ||
newDuotone, | ||
duotonePalette | ||
); | ||
|
||
const newStyle = { | ||
...style, | ||
color: { | ||
...style?.color, | ||
duotone: maybePreset ?? newDuotone, // use preset or fallback to custom colors. | ||
}, | ||
}; | ||
setAttributes( { style: newStyle } ); | ||
} } | ||
settings={ settings } | ||
/> | ||
</BlockControls> | ||
</> | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,13 @@ export default function FiltersPanel( { name } ) { | |
inheritedValue={ inheritedStyle } | ||
value={ style } | ||
onChange={ setStyle } | ||
settings={ settings } | ||
settings={ { | ||
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 wonder if we should memoize this object (depends on how it's used internally) |
||
...settings, | ||
color: { | ||
...settings.color, | ||
customDuotone: false, //TO FIX: Custom duotone only works on the block level right now | ||
}, | ||
} } | ||
/> | ||
); | ||
} |
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.
It's not great when we use "classnames" as reusable components. In the ideal scenario, the styles should be defined in the same place where the classname is used.
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.
yes! this I think can be refactored when we work on using the filter panel for the toolbar controls, I will make a note to do just that