-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Refactor the filters (duotone) panel as a generic UI Styles component #49577
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
packages/block-editor/src/components/global-styles/filters-panel.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,157 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalToolsPanel as ToolsPanel, | ||
__experimentalToolsPanelItem as ToolsPanelItem, | ||
__experimentalVStack as VStack, | ||
DuotonePicker, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useCallback, useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getValueFromVariable } from './utils'; | ||
import { immutableSet } from '../../utils/object'; | ||
|
||
const EMPTY_ARRAY = []; | ||
function useMultiOriginColorPresets( | ||
settings, | ||
{ presetSetting, defaultSetting } | ||
) { | ||
const disableDefault = ! settings?.color?.[ defaultSetting ]; | ||
const userPresets = | ||
settings?.color?.[ presetSetting ]?.custom || EMPTY_ARRAY; | ||
const themePresets = | ||
settings?.color?.[ presetSetting ]?.theme || EMPTY_ARRAY; | ||
const defaultPresets = | ||
settings?.color?.[ presetSetting ]?.default || EMPTY_ARRAY; | ||
return useMemo( | ||
() => [ | ||
...userPresets, | ||
...themePresets, | ||
...( disableDefault ? EMPTY_ARRAY : defaultPresets ), | ||
], | ||
[ disableDefault, userPresets, themePresets, defaultPresets ] | ||
); | ||
} | ||
|
||
export function useHasFiltersPanel( settings ) { | ||
const hasDuotone = useHasDuotoneControl( settings ); | ||
|
||
return hasDuotone; | ||
} | ||
|
||
function useHasDuotoneControl( settings ) { | ||
return settings.color.customDuotone || settings.color.defaultDuotone; | ||
} | ||
|
||
function FiltersToolsPanel( { | ||
resetAllFilter, | ||
onChange, | ||
value, | ||
panelId, | ||
children, | ||
} ) { | ||
const resetAll = () => { | ||
const updatedValue = resetAllFilter( value ); | ||
onChange( updatedValue ); | ||
}; | ||
|
||
return ( | ||
<ToolsPanel | ||
label={ __( 'Filters' ) } | ||
resetAll={ resetAll } | ||
panelId={ panelId } | ||
> | ||
{ children } | ||
</ToolsPanel> | ||
); | ||
} | ||
|
||
const DEFAULT_CONTROLS = { | ||
duotone: true, | ||
}; | ||
|
||
export default function FiltersPanel( { | ||
as: Wrapper = FiltersToolsPanel, | ||
value, | ||
onChange, | ||
inheritedValue = value, | ||
settings, | ||
panelId, | ||
defaultControls = DEFAULT_CONTROLS, | ||
} ) { | ||
const decodeValue = ( rawValue ) => | ||
getValueFromVariable( { settings }, '', rawValue ); | ||
|
||
// Duotone | ||
const hasDuotoneEnabled = useHasDuotoneControl( settings ); | ||
const duotonePalette = useMultiOriginColorPresets( settings, { | ||
presetSetting: 'duotone', | ||
defaultSetting: 'defaultDuotone', | ||
} ); | ||
const colorPalette = useMultiOriginColorPresets( settings, { | ||
presetSetting: 'palette', | ||
defaultSetting: 'defaultPalette', | ||
} ); | ||
const duotone = decodeValue( inheritedValue?.filter?.duotone ); | ||
const setDuotone = ( newValue ) => { | ||
const duotonePreset = duotonePalette.find( ( { colors } ) => { | ||
return colors === newValue; | ||
} ); | ||
const settedValue = duotonePreset | ||
? `var:preset|duotone|${ duotonePreset.slug }` | ||
: newValue; | ||
onChange( immutableSet( value, [ 'filter', 'duotone' ], settedValue ) ); | ||
}; | ||
const hasDuotone = () => !! value?.filter?.duotone; | ||
const resetDuotone = () => setDuotone( undefined ); | ||
|
||
const resetAllFilter = useCallback( ( previousValue ) => { | ||
return { | ||
...previousValue, | ||
filter: { | ||
...previousValue.filter, | ||
duotone: undefined, | ||
}, | ||
}; | ||
}, [] ); | ||
|
||
return ( | ||
<Wrapper | ||
resetAllFilter={ resetAllFilter } | ||
value={ value } | ||
onChange={ onChange } | ||
panelId={ panelId } | ||
> | ||
{ hasDuotoneEnabled && ( | ||
<ToolsPanelItem | ||
label={ __( 'Duotone' ) } | ||
hasValue={ hasDuotone } | ||
onDeselect={ resetDuotone } | ||
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 } | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value={ duotone } | ||
onChange={ setDuotone } | ||
/> | ||
</VStack> | ||
</ToolsPanelItem> | ||
) } | ||
</Wrapper> | ||
); | ||
} |
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
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
82 changes: 0 additions & 82 deletions
82
packages/edit-site/src/components/global-styles/duotone-panel.js
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
packages/edit-site/src/components/global-styles/filter-utils.js
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
packages/edit-site/src/components/global-styles/filters-panel.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,33 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../private-apis'; | ||
const { | ||
useGlobalStyle, | ||
useGlobalSetting, | ||
useSettingsForBlockElement, | ||
FiltersPanel: StylesFiltersPanel, | ||
} = unlock( blockEditorPrivateApis ); | ||
|
||
export default function FiltersPanel( { name } ) { | ||
const [ style ] = useGlobalStyle( '', name, 'user', false ); | ||
const [ inheritedStyle, setStyle ] = useGlobalStyle( '', name, 'all', { | ||
shouldDecodeEncode: false, | ||
} ); | ||
const [ rawSettings ] = useGlobalSetting( '', name ); | ||
const settings = useSettingsForBlockElement( rawSettings, name ); | ||
|
||
return ( | ||
<StylesFiltersPanel | ||
inheritedValue={ inheritedStyle } | ||
value={ style } | ||
onChange={ setStyle } | ||
settings={ settings } | ||
/> | ||
); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the duotone doesn't come from a preset, I'm storing an array of colors. While this doesn't work yet (not available in the UI) in global styles, it should work when you "enable the flag"