Skip to content
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 Global Styles controls for blocks that support duotone #48255

Merged
merged 15 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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