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

ColorPalette: Improve color name readability #50450

Merged
merged 31 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8b5d427
Move color name and value below picker swatch
chad1008 May 8, 2023
40cf2b3
update component styles
chad1008 May 8, 2023
bd97be7
update CHANGELOG
chad1008 May 9, 2023
f1b8099
update snapshot
chad1008 May 9, 2023
f232053
update unit tests
chad1008 May 9, 2023
d981a96
simplify CSS
chad1008 May 10, 2023
639be04
simplify border styling
chad1008 May 10, 2023
ba7a5a8
replace `Flex` with `VStack`
chad1008 May 10, 2023
b2b05c4
remove `var()` wrapper from css vars
chad1008 May 10, 2023
d4880c5
re-update snapshot
chad1008 May 10, 2023
ffeddd5
display default text for no value
chad1008 May 15, 2023
a64ed18
implement proper BEM notation
chad1008 May 15, 2023
5d41512
update snapshot with BEM notation
chad1008 May 15, 2023
65caf90
Updating block editor snapshots again
chad1008 May 16, 2023
65c33fc
simplify `displayValue` declaration
chad1008 May 17, 2023
b032818
Remove extra curly braces
chad1008 May 17, 2023
1c6774a
Add layout comment
chad1008 May 17, 2023
43507c5
fix color value span min-height
chad1008 May 18, 2023
af8931f
Always show checkered background
chad1008 May 18, 2023
eab5fea
hyphenate hex codes for assistive tech
chad1008 May 22, 2023
300cbf0
snapshot updated to include hex code hyphens
chad1008 May 24, 2023
a674052
Update unit tests
chad1008 May 24, 2023
7e43311
remove hardcoded `px` value for `min-height`
chad1008 May 24, 2023
4e69db7
update snapshots yet again
chad1008 May 24, 2023
5fbb188
rename new test and expose onChange
chad1008 May 25, 2023
28a81df
apply unit test feedback
chad1008 May 25, 2023
59e9492
improve color value text contrast
chad1008 May 25, 2023
42683a8
Merge branch 'trunk' into colorpalette-layout-update
chad1008 May 25, 2023
7435f08
move CHANGELOG entry
chad1008 May 25, 2023
0823d7d
remove duplicate check for cleared color value text
chad1008 May 31, 2023
03140aa
merge `trunk` into `colorpalette-laytout-update`
chad1008 May 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,52 @@ exports[`ColorPaletteControl matches the snapshot 1`] = `
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: flex-start;
-webkit-box-align: flex-start;
-ms-flex-align: flex-start;
align-items: flex-start;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
gap: calc(4px * 2);
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
justify-content: space-between;
width: 100%;
-webkit-align-items: stretch;
-webkit-box-align: stretch;
-ms-flex-align: stretch;
align-items: stretch;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
gap: 0;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
}

.emotion-10>* {
min-width: 0;
min-height: 0;
}

.emotion-13 {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: block;
max-height: 100%;
max-width: 100%;
.emotion-12 {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: stretch;
-webkit-box-align: stretch;
-ms-flex-align: stretch;
align-items: stretch;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
gap: calc(4px * 0.5);
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
}

.emotion-12>* {
min-height: 0;
min-width: 0;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
}

.emotion-15 {
.emotion-14 {
display: block;
max-height: 100%;
max-width: 100%;
min-height: 0;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

<div>
Expand Down Expand Up @@ -157,30 +164,39 @@ exports[`ColorPaletteControl matches the snapshot 1`] = `
class="components-dropdown"
tabindex="-1"
>
<button
aria-expanded="false"
aria-haspopup="true"
aria-label="Custom color picker. The currently selected color is called "red" and has a value of "f00"."
class="components-flex components-color-palette__custom-color emotion-10 emotion-5"
<div
class="components-flex components-h-stack components-v-stack components-color-palette__custom-color-wrapper emotion-10 emotion-5"
data-wp-c16t="true"
data-wp-component="Flex"
style="background: rgb(255, 0, 0); color: rgb(0, 0, 0);"
data-wp-component="VStack"
>
<span
class="components-truncate components-flex-item components-color-palette__custom-color-name emotion-5 emotion-13 emotion-5"
data-wp-c16t="true"
data-wp-component="FlexItem"
>
red
</span>
<span
class="components-flex-item components-color-palette__custom-color-value emotion-15 emotion-5"
<button
aria-expanded="false"
aria-haspopup="true"
aria-label="Custom color picker. The currently selected color is called "red" and has a value of "#-f-0-0"."
class="components-color-palette__custom-color-button"
style="background: rgb(255, 0, 0);"
/>
<div
class="components-flex components-h-stack components-v-stack components-color-palette__custom-color-text-wrapper emotion-12 emotion-5"
data-wp-c16t="true"
data-wp-component="FlexItem"
data-wp-component="VStack"
>
f00
</span>
</button>
<span
class="components-truncate components-color-palette__custom-color-name emotion-14 emotion-5"
data-wp-c16t="true"
data-wp-component="Truncate"
>
red
</span>
<span
class="components-truncate components-color-palette__custom-color-value components-color-palette__custom-color-value--is-hex emotion-14 emotion-5"
data-wp-c16t="true"
data-wp-component="Truncate"
>
#f00
</span>
</div>
</div>
</div>
<div
class="components-circular-option-picker"
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- `ColorPalette`: Improve readability of color name and value, and improve rendering of partially transparent colors ([#50450](https://github.com/WordPress/gutenberg/pull/50450)).

## 25.0.0 (2023-05-24)

### Breaking Changes
Expand Down
93 changes: 47 additions & 46 deletions packages/components/src/color-palette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ForwardedRef } from 'react';
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import a11yPlugin from 'colord/plugins/a11y';
import classnames from 'classnames';

/**
* WordPress dependencies
Expand All @@ -19,7 +20,6 @@ import Dropdown from '../dropdown';
import { ColorPicker } from '../color-picker';
import CircularOptionPicker from '../circular-option-picker';
import { VStack } from '../v-stack';
import { Flex, FlexItem } from '../flex';
import { Truncate } from '../truncate';
import { ColorHeading } from './styles';
import DropdownContentWrapper from '../dropdown/dropdown-content-wrapper';
Expand All @@ -37,7 +37,6 @@ import {
extractColorNameFromCurrentValue,
isMultiplePaletteArray,
normalizeColorValue,
showTransparentBackground,
} from './utils';

extend( [ namesPlugin, a11yPlugin ] );
Expand Down Expand Up @@ -219,21 +218,18 @@ function UnforwardedColorPalette(
/>
</DropdownContentWrapper>
);
const isHex = value?.startsWith( '#' );

const colordColor = colord( normalizedColorValue ?? '' );

const valueWithoutLeadingHash = value?.startsWith( '#' )
? value.substring( 1 )
: value ?? '';

const customColorAccessibleLabel = !! valueWithoutLeadingHash
// Leave hex values as-is. Remove the `var()` wrapper from CSS vars.
const displayValue = value?.replace( /^var\((.+)\)$/, '$1' );
const customColorAccessibleLabel = !! displayValue
chad1008 marked this conversation as resolved.
Show resolved Hide resolved
? sprintf(
// translators: %1$s: The name of the color e.g: "vivid red". %2$s: The color's hex code e.g: "#f00".
// translators: %1$s: The name of the color e.g: "vivid red". %2$s: The color's hex code, with added hyphens e.g: "#-f-0-0".
__(
'Custom color picker. The currently selected color is called "%1$s" and has a value of "%2$s".'
),
buttonLabelName,
valueWithoutLeadingHash
isHex ? displayValue.split( '' ).join( '-' ) : displayValue
)
: __( 'Custom color picker.' );

Expand All @@ -257,43 +253,48 @@ function UnforwardedColorPalette(
isRenderedInSidebar={ __experimentalIsRenderedInSidebar }
renderContent={ renderCustomColorPicker }
renderToggle={ ( { isOpen, onToggle } ) => (
<Flex
as={ 'button' }
ref={ customColorPaletteCallbackRef }
justify="space-between"
align="flex-start"
className="components-color-palette__custom-color"
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
aria-label={ customColorAccessibleLabel }
style={
showTransparentBackground( value )
? { color: '#000' }
: {
background: value,
color:
colordColor.contrast() >
colordColor.contrast( '#000' )
? '#fff'
: '#000',
}
}
<VStack
className="components-color-palette__custom-color-wrapper"
spacing={ 0 }
>
<FlexItem
isBlock
as={ Truncate }
className="components-color-palette__custom-color-name"
>
{ buttonLabelName }
</FlexItem>
<FlexItem
as="span"
className="components-color-palette__custom-color-value"
<button
ref={ customColorPaletteCallbackRef }
className="components-color-palette__custom-color-button"
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
aria-label={ customColorAccessibleLabel }
style={ {
background: value,
} }
/>
<VStack
className="components-color-palette__custom-color-text-wrapper"
spacing={ 0.5 }
>
{ valueWithoutLeadingHash }
</FlexItem>
</Flex>
<Truncate className="components-color-palette__custom-color-name">
{ value
? buttonLabelName
: 'No color selected' }
</Truncate>
{ /*
This `Truncate` is always rendered, even if
there is no `displayValue`, to ensure the layout
does not shift
*/ }
<Truncate
className={ classnames(
'components-color-palette__custom-color-value',
{
'components-color-palette__custom-color-value--is-hex':
isHex,
}
) }
>
{ displayValue }
</Truncate>
</VStack>
</VStack>
) }
/>
) }
Expand Down
76 changes: 57 additions & 19 deletions packages/components/src/color-palette/style.scss
Original file line number Diff line number Diff line change
@@ -1,41 +1,79 @@
.components-color-palette__custom-color {
$border-as-box-shadow: inset 0 0 0 $border-width rgba(0, 0, 0, 0.2);

.components-color-palette__custom-color-wrapper {
position: relative;
z-index: 0;
}
.components-color-palette__custom-color-button {
position: relative;
border: none;
background: none;
border-radius: $radius-block-ui;
height: $grid-unit-80;
padding: $grid-unit-15;
font-family: inherit;
width: 100%;
// The background image creates a checkerboard pattern. Ignore rtlcss to
// make it work both in LTR and RTL.
// See https://github.com/WordPress/gutenberg/pull/42510
/*rtl:begin:ignore*/
background-image:
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200),
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200);
background-position: 0 0, 24px 24px;
/*rtl:end:ignore*/
background-size: calc(2 * 24px) calc(2 * 24px);
box-sizing: border-box;
color: $white;
cursor: pointer;
box-shadow: inset 0 0 0 $border-width rgba(0, 0, 0, 0.2);
// Show a thin outline in Windows high contrast mode.
outline: 1px solid transparent;
border-radius: $radius-block-ui $radius-block-ui 0 0;
box-shadow: $border-as-box-shadow;

&:focus {
box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) $components-color-accent;
// Show a outline in Windows high contrast mode.
outline-width: 2px;
}

&::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
// The background image creates a checkerboard pattern. Ignore rtlcss to
// make it work both in LTR and RTL.
// See https://github.com/WordPress/gutenberg/pull/42510
/*rtl:begin:ignore*/
background-image:
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200),
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200);
background-position: 0 0, 24px 24px;
/*rtl:end:ignore*/
background-size: calc(2 * 24px) calc(2 * 24px);
}
}
ciampo marked this conversation as resolved.
Show resolved Hide resolved

.components-color-palette__custom-color-text-wrapper {
padding: $grid-unit-15 $grid-unit-20;
border-radius: 0 0 $radius-block-ui $radius-block-ui;
position: relative;
font-size: $default-font-size;

// Add a border with the same technique as the button above,
// but only for left, bottom, and right sides.
box-shadow:
inset 0 -1 * $border-width 0 0 rgba(0, 0, 0, 0.2),
inset $border-width 0 0 0 rgba(0, 0, 0, 0.2),
inset -1 * $border-width 0 0 0 rgba(0, 0, 0, 0.2);
}

.components-color-palette__custom-color-name {
text-align: left;
color: $components-color-foreground;
margin: 0 $border-width;
}

.components-color-palette__custom-color-value {
margin-left: $grid-unit-20;
text-transform: uppercase;
color: $gray-700;

&--is-hex {
text-transform: uppercase;
}

// Add a zero-width space when this element is empty to preserve
// a minimum height of 1 line of text, and avoid layout jumps.
&:empty::after {
content: "\200B";
visibility: hidden;
}
}
Loading