-
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
Global Styles: Adds site background color controls component in global styles #66736
Open
amitraj2203
wants to merge
4
commits into
trunk
Choose a base branch
from
adds/background-color-component-in-global-styles
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a9c1ed5
Adds upload icon for background image and change it's shape to rounde…
amitraj2203 6847e93
Integrated background-color component for global styles
amitraj2203 eb64f02
Refactor background panel to include color control
amitraj2203 aa8e308
Merge branch 'trunk' of github.com:WordPress/gutenberg into adds/back…
amitraj2203 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
265 changes: 265 additions & 0 deletions
265
packages/block-editor/src/components/background-color-control/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,265 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import clsx from 'clsx'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Flex, | ||
FlexItem, | ||
__experimentalItem as Item, | ||
__experimentalHStack as HStack, | ||
__experimentalZStack as ZStack, | ||
Dropdown, | ||
Button, | ||
ColorIndicator, | ||
__experimentalDropdownContentWrapper as DropdownContentWrapper, | ||
privateApis as componentsPrivateApis, | ||
} from '@wordpress/components'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ColorGradientControl from '../colors-gradients/control'; | ||
import { unlock } from '../../lock-unlock'; | ||
import { | ||
useColorsPerOrigin, | ||
useGradientsPerOrigin, | ||
} from '../global-styles/hooks'; | ||
import { getValueFromVariable } from '../global-styles/utils'; | ||
import { setImmutably } from '../../utils/object'; | ||
|
||
const BACKGROUND_POPOVER_PROPS = { | ||
placement: 'left-start', | ||
offset: 36, | ||
shift: true, | ||
className: 'block-editor-global-styles-background-panel__popover', | ||
}; | ||
|
||
function ColorPanelTab( { | ||
isGradient, | ||
inheritedValue, | ||
userValue, | ||
setValue, | ||
colorGradientControlSettings, | ||
} ) { | ||
return ( | ||
<ColorGradientControl | ||
{ ...colorGradientControlSettings } | ||
showTitle={ false } | ||
enableAlpha | ||
__experimentalIsRenderedInSidebar | ||
colorValue={ isGradient ? undefined : inheritedValue } | ||
gradientValue={ isGradient ? inheritedValue : undefined } | ||
onColorChange={ isGradient ? undefined : setValue } | ||
onGradientChange={ isGradient ? setValue : undefined } | ||
clearable={ inheritedValue === userValue } | ||
headingLevel={ 3 } | ||
/> | ||
); | ||
} | ||
|
||
const LabeledColorIndicators = ( { indicators, label } ) => ( | ||
<Item> | ||
<HStack justify="flex-start"> | ||
<ZStack isLayered={ false } offset={ -8 }> | ||
{ indicators.map( ( indicator, index ) => ( | ||
<Flex key={ index } expanded={ false }> | ||
<ColorIndicator colorValue={ indicator } /> | ||
</Flex> | ||
) ) } | ||
</ZStack> | ||
<FlexItem | ||
className="block-editor-panel-color-gradient-settings__color-name" | ||
title={ label } | ||
> | ||
{ label } | ||
</FlexItem> | ||
</HStack> | ||
</Item> | ||
); | ||
|
||
const { Tabs } = unlock( componentsPrivateApis ); | ||
|
||
export default function BackgroundColorPanel( { | ||
value, | ||
onChange, | ||
inheritedValue = value, | ||
settings, | ||
label = __( 'Color' ), | ||
} ) { | ||
const colors = useColorsPerOrigin( settings ); | ||
const gradients = useGradientsPerOrigin( settings ); | ||
const areCustomSolidsEnabled = settings?.color?.custom; | ||
const areCustomGradientsEnabled = settings?.color?.customGradient; | ||
|
||
const decodeValue = ( rawValue ) => | ||
getValueFromVariable( { settings }, '', rawValue ); | ||
|
||
const backgroundColor = decodeValue( inheritedValue?.color?.background ); | ||
const backgroundGradient = decodeValue( inheritedValue?.color?.gradient ); | ||
|
||
const userBackgroundColor = decodeValue( value?.color?.background ); | ||
const userGradient = decodeValue( value?.color?.gradient ); | ||
|
||
const setBackgroundColor = ( newColor ) => { | ||
const newValue = setImmutably( | ||
value, | ||
[ 'color', 'background' ], | ||
encodeColorValue( newColor ) | ||
); | ||
newValue.color.gradient = undefined; | ||
onChange( newValue ); | ||
}; | ||
|
||
const setGradient = ( newGradient ) => { | ||
const newValue = setImmutably( | ||
value, | ||
[ 'color', 'gradient' ], | ||
encodeGradientValue( newGradient ) | ||
); | ||
newValue.color.background = undefined; | ||
onChange( newValue ); | ||
}; | ||
|
||
const encodeColorValue = ( colorValue ) => { | ||
const allColors = colors.flatMap( | ||
( { colors: originColors } ) => originColors | ||
); | ||
const colorObject = allColors.find( | ||
( { color } ) => color === colorValue | ||
); | ||
return colorObject | ||
? 'var:preset|color|' + colorObject.slug | ||
: colorValue; | ||
}; | ||
|
||
const encodeGradientValue = ( gradientValue ) => { | ||
const allGradients = gradients.flatMap( | ||
( { gradients: originGradients } ) => originGradients | ||
); | ||
const gradientObject = allGradients.find( | ||
( { gradient } ) => gradient === gradientValue | ||
); | ||
return gradientObject | ||
? 'var:preset|gradient|' + gradientObject.slug | ||
: gradientValue; | ||
}; | ||
|
||
const hasSolidColors = colors.length > 0 || areCustomSolidsEnabled; | ||
const hasGradientColors = gradients.length > 0 || areCustomGradientsEnabled; | ||
|
||
const tabs = [ | ||
hasSolidColors && { | ||
key: 'background', | ||
label: __( 'Background' ), | ||
inheritedValue: backgroundColor, | ||
setValue: setBackgroundColor, | ||
userValue: userBackgroundColor, | ||
}, | ||
hasGradientColors && { | ||
key: 'gradient', | ||
label: __( 'Gradient' ), | ||
inheritedValue: backgroundGradient, | ||
setValue: setGradient, | ||
userValue: userGradient, | ||
isGradient: true, | ||
}, | ||
].filter( Boolean ); | ||
|
||
const colorGradientControlSettings = { | ||
colors, | ||
disableCustomColors: ! areCustomSolidsEnabled, | ||
gradients, | ||
disableCustomGradients: ! areCustomGradientsEnabled, | ||
}; | ||
|
||
const indicators = [ backgroundGradient ?? backgroundColor ]; | ||
const currentTab = tabs.find( ( tab ) => tab.userValue !== undefined ); | ||
const { key: firstTabKey, ...firstTab } = tabs[ 0 ] ?? {}; | ||
|
||
return ( | ||
<Item className="block-editor-global-styles-background-panel__color-tools-panel-item"> | ||
<Dropdown | ||
popoverProps={ BACKGROUND_POPOVER_PROPS } | ||
className="block-editor-tools-panel-color-gradient-settings__dropdown" | ||
renderToggle={ ( { onToggle, isOpen } ) => { | ||
const toggleProps = { | ||
onClick: onToggle, | ||
className: clsx( | ||
'block-editor-panel-color-gradient-settings__dropdown', | ||
{ 'is-open': isOpen } | ||
), | ||
'aria-expanded': isOpen, | ||
'aria-label': sprintf( | ||
/* translators: %s is the type of color property, e.g., "background" */ | ||
__( 'Color %s styles' ), | ||
label | ||
), | ||
}; | ||
|
||
return ( | ||
<Button __next40pxDefaultSize { ...toggleProps }> | ||
<LabeledColorIndicators | ||
indicators={ indicators } | ||
label={ label } | ||
/> | ||
</Button> | ||
); | ||
} } | ||
renderContent={ () => ( | ||
<DropdownContentWrapper paddingSize="none"> | ||
<div className="block-editor-panel-color-gradient-settings__dropdown-content"> | ||
{ tabs.length === 1 && ( | ||
<ColorPanelTab | ||
key={ firstTabKey } | ||
{ ...firstTab } | ||
colorGradientControlSettings={ | ||
colorGradientControlSettings | ||
} | ||
/> | ||
) } | ||
{ tabs.length > 1 && ( | ||
<Tabs defaultTabId={ currentTab?.key }> | ||
<Tabs.TabList> | ||
{ tabs.map( ( tab ) => ( | ||
<Tabs.Tab | ||
key={ tab.key } | ||
tabId={ tab.key } | ||
> | ||
{ tab.label } | ||
</Tabs.Tab> | ||
) ) } | ||
</Tabs.TabList> | ||
|
||
{ tabs.map( ( tab ) => { | ||
const { key: tabKey, ...restTabProps } = | ||
tab; | ||
return ( | ||
<Tabs.TabPanel | ||
key={ tabKey } | ||
tabId={ tabKey } | ||
focusable={ false } | ||
> | ||
<ColorPanelTab | ||
key={ tabKey } | ||
{ ...restTabProps } | ||
colorGradientControlSettings={ | ||
colorGradientControlSettings | ||
} | ||
/> | ||
</Tabs.TabPanel> | ||
); | ||
} ) } | ||
</Tabs> | ||
) } | ||
</div> | ||
</DropdownContentWrapper> | ||
) } | ||
/> | ||
</Item> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/block-editor/src/components/background-color-control/style.scss
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,16 @@ | ||
|
||
.block-editor-global-styles-background-panel__inspector-media-replace-container { | ||
.block-editor-global-styles-background-panel__color-tools-panel-item { | ||
padding: 0; | ||
|
||
button { | ||
padding: 0; | ||
} | ||
} | ||
} | ||
|
||
.block-editor-global-styles-background-panel__color-tools-panel-item { | ||
button { | ||
padding: 0; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
The height added by
__next40pxDefaultSize
causes a mismatch in height, which is evident when the button has focus:__next40pxDefaultSize
I know the linter wants us to have it there, but I don't see much value here.
Maybe it needs some overriding CSS to make it work.
Setting the height to
height: 36px;
appears to force it to match the other items.