Skip to content

Commit

Permalink
Duotone: Add Global Styles controls for blocks that support duotone (#…
Browse files Browse the repository at this point in the history
…48255)

* create a panel for filters

* created a duotone panel

* add the duotone picker to the panel, changed filters icon

* add the duotone picker to the panel, changed filters icon

* fixed selectors for the block in the editor, save the value correctly for presets

* move everything to a single panel

* move preview panel to filters component

* disable custom duotone for this PR

* dont output unset

* Simplify conditional nesting

* Fix filter declarations not being unset

* Rename DuotoneScreen to DuotonePanel

* Use default header level for duotone panel

* Rename filter-panel.js to filter-utils.js

* Remove description from Filters screen header

It seemed redundant with the duotone one on the same page now.

---------

Co-authored-by: scruffian <ben@scruffian.com>
Co-authored-by: Alex Lende <alex@lende.xyz>
  • Loading branch information
3 people authored Feb 24, 2023
1 parent cddb8ea commit fc4caf8
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2425,8 +2425,19 @@ function( $pseudo_selector ) use ( $selector ) {
$declarations_duotone = array();
foreach ( $declarations as $index => $declaration ) {
if ( 'filter' === $declaration['name'] ) {
/*
* 'unset' filters happen when a filter is unset
* in the site-editor UI. Because the 'unset' value
* in the user origin overrides the value in the
* theme origin, we can skip rendering anything
* here as no filter needs to be applied anymore.
* So only add declarations to with values other
* than 'unset'.
*/
if ( 'unset' !== $declaration['value'] ) {
$declarations_duotone[] = $declaration;
}
unset( $declarations[ $index ] );
$declarations_duotone[] = $declaration;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,10 @@ export const toStyles = (
if ( duotoneDeclarations.length > 0 ) {
ruleset =
ruleset +
`${ duotoneSelector }{${ duotoneDeclarations.join(
';'
) };}`;
`${ scopeSelector(
selector,
duotoneSelector
) }{${ duotoneDeclarations.join( ';' ) };}`;
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/global-styles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const PRESET_METADATA = [
},
{
path: [ 'color', 'duotone' ],
valueKey: 'colors',
cssVarInfix: 'duotone',
valueFunc: ( { slug } ) => `url( '#wp-duotone-${ slug }' )`,
classes: [],
Expand Down Expand Up @@ -95,6 +96,7 @@ export const PRESET_METADATA = [
export const STYLE_PATH_TO_CSS_VAR_INFIX = {
'color.background': 'color',
'color.text': 'color',
'filter.duotone': 'duotone',
'elements.link.color.text': 'color',
'elements.link.:hover.color.text': 'color',
'elements.link.typography.fontFamily': 'font-family',
Expand Down
12 changes: 12 additions & 0 deletions packages/edit-site/src/components/global-styles/context-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
typography,
border,
filter,
shadow,
color,
layout,
Expand All @@ -30,6 +31,7 @@ import { useMemo } from '@wordpress/element';
import { useHasBorderPanel } from './border-panel';
import { useHasColorPanel } from './color-utils';
import { useHasDimensionsPanel } from './dimensions-panel';
import { useHasFilterPanel } from './filter-utils';
import { useHasVariationsPanel } from './variations-panel';
import { NavigationButtonAsItem } from './navigation-button';
import { IconWithCurrentColor } from './icon-with-current-color';
Expand All @@ -55,6 +57,7 @@ function ContextMenu( { name, parentMenu = '' } ) {
const hasColorPanel = useHasColorPanel( name );
const hasBorderPanel = useHasBorderPanel( name );
const hasEffectsPanel = useHasShadowControl( name );
const hasFilterPanel = useHasFilterPanel( name );
const hasDimensionsPanel = useHasDimensionsPanel( name );
const hasLayoutPanel = hasDimensionsPanel;
const hasVariationsPanel = useHasVariationsPanel( name, parentMenu );
Expand Down Expand Up @@ -117,6 +120,15 @@ function ContextMenu( { name, parentMenu = '' } ) {
{ __( 'Shadow' ) }
</NavigationButtonAsItem>
) }
{ hasFilterPanel && (
<NavigationButtonAsItem
icon={ filter }
path={ parentMenu + '/filters' }
aria-label={ __( 'Filters styles' ) }
>
{ __( 'Filters' ) }
</NavigationButtonAsItem>
) }
{ hasLayoutPanel && (
<NavigationButtonAsItem
icon={ layout }
Expand Down
82 changes: 82 additions & 0 deletions packages/edit-site/src/components/global-styles/duotone-panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
__experimentalToolsPanel as ToolsPanel,
DuotonePicker,
} from '@wordpress/components';
import {
privateApis as blockEditorPrivateApis,
useSetting,
} from '@wordpress/block-editor';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import { unlock } from '../../private-apis';
const { useGlobalStyle } = unlock( blockEditorPrivateApis );

const EMPTY_ARRAY = [];

function useMultiOriginPresets( { presetSetting, defaultSetting } ) {
const disableDefault = ! useSetting( defaultSetting );
const userPresets =
useSetting( `${ presetSetting }.custom` ) || EMPTY_ARRAY;
const themePresets =
useSetting( `${ presetSetting }.theme` ) || EMPTY_ARRAY;
const defaultPresets =
useSetting( `${ presetSetting }.default` ) || EMPTY_ARRAY;
return useMemo(
() => [
...userPresets,
...themePresets,
...( disableDefault ? EMPTY_ARRAY : defaultPresets ),
],
[ disableDefault, userPresets, themePresets, defaultPresets ]
);
}

function DuotonePanel( { name } ) {
const [ themeDuotone, setThemeDuotone ] = useGlobalStyle(
'filter.duotone',
name
);

const duotonePalette = useMultiOriginPresets( {
presetSetting: 'color.duotone',
defaultSetting: 'color.defaultDuotone',
} );
const colorPalette = useMultiOriginPresets( {
presetSetting: 'color.palette',
defaultSetting: 'color.defaultPalette',
} );

if ( duotonePalette?.length === 0 ) {
return null;
}
return (
<>
<ToolsPanel label={ __( 'Duotone' ) }>
<span className="span-columns">
{ __(
'Create a two-tone color effect without losing your original image.'
) }
</span>
<div className="span-columns">
<DuotonePicker
colorPalette={ colorPalette }
duotonePalette={ duotonePalette }
disableCustomColors={ true }
disableCustomDuotone={ true }
value={ themeDuotone }
onChange={ setThemeDuotone }
/>
</div>
</ToolsPanel>
</>
);
}

export default DuotonePanel;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Internal dependencies
*/
import { useSupportedStyles } from './hooks';

export function useHasFilterPanel( name ) {
const supports = useSupportedStyles( name );
return supports.includes( 'filter' );
}
27 changes: 27 additions & 0 deletions packages/edit-site/src/components/global-styles/screen-filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import DuotonePanel from './duotone-panel';
import BlockPreviewPanel from './block-preview-panel';

/**
* Internal dependencies
*/
import ScreenHeader from './header';

function ScreenFilters( { name } ) {
return (
<>
<ScreenHeader title={ __( 'Filters' ) } />
<BlockPreviewPanel name={ name } />
<DuotonePanel name={ name } />
</>
);
}

export default ScreenFilters;
5 changes: 5 additions & 0 deletions packages/edit-site/src/components/global-styles/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import ScreenBlockList from './screen-block-list';
import ScreenBlock from './screen-block';
import ScreenTypography from './screen-typography';
import ScreenTypographyElement from './screen-typography-element';
import ScreenFilters from './screen-filters';
import ScreenColors from './screen-colors';
import ScreenColorPalette from './screen-color-palette';
import ScreenBackgroundColor from './screen-background-color';
Expand Down Expand Up @@ -205,6 +206,10 @@ function ContextScreens( { name, parentMenu = '', variation = '' } ) {
<ScreenBackgroundColor name={ name } variation={ variation } />
</GlobalStylesNavigationScreen>

<GlobalStylesNavigationScreen path={ parentMenu + '/filters' }>
<ScreenFilters name={ name } />
</GlobalStylesNavigationScreen>

<GlobalStylesNavigationScreen path={ parentMenu + '/colors/text' }>
<ScreenTextColor name={ name } variation={ variation } />
</GlobalStylesNavigationScreen>
Expand Down

1 comment on commit fc4caf8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in fc4caf8.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4260581258
📝 Reported issues:

Please sign in to comment.