From f140ffed6cb62994a4fd60815dcddd3f396578bb Mon Sep 17 00:00:00 2001 From: Jerry Jones Date: Thu, 3 Dec 2026 09:40:40 -0600 Subject: [PATCH 1/2] Global Styles UI: Use design tokens for the variation card frames The color, typography and style variation cards hardcoded their frame colors, so under a dark theme the selected ring, border and hover outline were invisible, the focus ring used wp-admin's accent color, and the typography card was white on white. Follow the pattern preview frames from #87426: the border and hover use the neutral interactive stroke tokens, the selected ring the neutral interactive foreground, and the focus ring the focus stroke, both at the focus width, with a surface band between ring and preview. The selected and focus rules come after hover, so hovering no longer hides them. The card element no longer draws its own focus outline. Without the site's own colors, the preview uses the surface and content tokens `Card` uses, so the typography card and the Browse styles label stay readable in any theme. Co-Authored-By: Claude Opus 5.5 --- package-lock.json | 1 + packages/global-styles-ui/CHANGELOG.md | 4 + packages/global-styles-ui/package.json | 1 + .../global-styles-ui/src/preview-styles.tsx | 2 +- .../global-styles-ui/src/preview-wrapper.tsx | 10 +- .../src/variations/style.scss | 31 +- .../test/variation.browser.test.tsx | 450 ++++++++++++++++++ packages/global-styles-ui/tsconfig.json | 1 + 8 files changed, 487 insertions(+), 13 deletions(-) create mode 100644 packages/global-styles-ui/src/variations/test/variation.browser.test.tsx diff --git a/package-lock.json b/package-lock.json index 9dab61d5a585dd..f38a6d940e1035 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42933,6 +42933,7 @@ "@testing-library/dom": "^10.4.2", "@testing-library/react": "^16.3.3", "@testing-library/user-event": "^14.6.13", + "@wordpress/theme": "file:../theme", "vitest": "^5.1.1", "vitest-browser-react": "^2.3.0" }, diff --git a/packages/global-styles-ui/CHANGELOG.md b/packages/global-styles-ui/CHANGELOG.md index a393e4b197c7a2..fb7c4d6df0f1c6 100644 --- a/packages/global-styles-ui/CHANGELOG.md +++ b/packages/global-styles-ui/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Enhancements + +- Use theme colors for the outlines of the color, typography, and style variation cards, and for their preview background and text when the site defines no colors ([#TBD](https://github.com/WordPress/gutenberg/pull/TBD)). + ## 2.3.0 (2026-12-02) ### Enhancements diff --git a/packages/global-styles-ui/package.json b/packages/global-styles-ui/package.json index 6279583a7d04d8..af6927c4373c48 100644 --- a/packages/global-styles-ui/package.json +++ b/packages/global-styles-ui/package.json @@ -67,6 +67,7 @@ "@testing-library/dom": "^10.4.2", "@testing-library/react": "^16.3.3", "@testing-library/user-event": "^14.6.13", + "@wordpress/theme": "file:../theme", "vitest": "^5.1.1", "vitest-browser-react": "^2.3.0" }, diff --git a/packages/global-styles-ui/src/preview-styles.tsx b/packages/global-styles-ui/src/preview-styles.tsx index 55169f3fb60238..e11daf426db087 100644 --- a/packages/global-styles-ui/src/preview-styles.tsx +++ b/packages/global-styles-ui/src/preview-styles.tsx @@ -63,7 +63,7 @@ function PreviewStyles( { const [ headingFontWeight = fontWeight ] = useStyle< string >( 'elements.h1.typography.fontWeight' ); - const [ textColor = 'black' ] = useStyle< string >( 'color.text' ); + const [ textColor ] = useStyle< string >( 'color.text' ); const [ headingColor = textColor ] = useStyle< string >( 'elements.h1.color.text' ); diff --git a/packages/global-styles-ui/src/preview-wrapper.tsx b/packages/global-styles-ui/src/preview-wrapper.tsx index c63c54c99a59ab..eed1b7960db09a 100644 --- a/packages/global-styles-ui/src/preview-wrapper.tsx +++ b/packages/global-styles-ui/src/preview-wrapper.tsx @@ -34,8 +34,13 @@ function PreviewWrapper( { isFocused, withHoverView, }: PreviewWrapperProps ) { - const [ backgroundColor = 'white' ] = - useStyle< string >( 'color.background' ); + // Without the site's own colors, the preview uses the host's surface and + // text colors, so it stays readable in any theme. + const [ + backgroundColor = 'var(--wpds-color-background-surface-neutral-strong)', + ] = useStyle< string >( 'color.background' ); + const [ textColor = 'var(--wpds-color-foreground-content-neutral)' ] = + useStyle< string >( 'color.text' ); const [ gradientValue ] = useStyle< string >( 'color.gradient' ); const disableMotion = useReducedMotion(); const [ isHovered, setIsHovered ] = useState( false ); @@ -117,6 +122,7 @@ function PreviewWrapper( { height: normalizedHeight * ratio, width: '100%', background: gradientValue ?? backgroundColor, + color: textColor, } } initial="start" animate={ diff --git a/packages/global-styles-ui/src/variations/style.scss b/packages/global-styles-ui/src/variations/style.scss index be675312cad654..15966fe8b1f5ba 100644 --- a/packages/global-styles-ui/src/variations/style.scss +++ b/packages/global-styles-ui/src/variations/style.scss @@ -1,14 +1,15 @@ -@use "@wordpress/base-styles/colors" as *; @use "@wordpress/base-styles/variables" as *; .global-styles-ui-variations_item { box-sizing: border-box; // To round the outline in Windows 10 high contrast mode. cursor: var(--wpds-cursor-control); + // The preview draws the focus ring, so the item doesn't draw its own. + outline: none; .global-styles-ui-variations_item-preview { border-radius: var(--wpds-border-radius-sm); - outline: $border-width solid rgba($black, 0.1); + outline: $border-width solid var(--wpds-color-stroke-interactive-neutral); outline-offset: -$border-width; overflow: hidden; position: relative; @@ -26,18 +27,28 @@ } } - &:not(.is-active):hover .global-styles-ui-variations_item-preview { - outline-color: rgba($black, 0.3); + &:hover .global-styles-ui-variations_item-preview { + outline-color: var(--wpds-color-stroke-interactive-neutral-active); } - &.is-active .global-styles-ui-variations_item-preview, - &:focus-visible .global-styles-ui-variations_item-preview { - outline-color: $gray-900; - outline-offset: $border-width; - outline-width: var(--wp-admin-border-width-focus); + // The selected and focus rings are drawn outside the preview. A + // surface-colored band fills the gap between them and the preview, so they + // keep their contrast whatever colors the preview shows. + &.is-active, + &:focus-visible { + .global-styles-ui-variations_item-preview { + outline-offset: $border-width; + outline-width: var(--wpds-border-width-focus); + box-shadow: 0 0 0 $border-width var(--wpds-color-background-surface-neutral-strong); + } + } + + // After hover, so hovering keeps the selected and focus rings. + &.is-active .global-styles-ui-variations_item-preview { + outline-color: var(--wpds-color-foreground-interactive-neutral); } &:focus-visible .global-styles-ui-variations_item-preview { - outline-color: var(--wp-admin-theme-color); + outline-color: var(--wpds-color-stroke-focus); } } diff --git a/packages/global-styles-ui/src/variations/test/variation.browser.test.tsx b/packages/global-styles-ui/src/variations/test/variation.browser.test.tsx new file mode 100644 index 00000000000000..b46b5609ea9a65 --- /dev/null +++ b/packages/global-styles-ui/src/variations/test/variation.browser.test.tsx @@ -0,0 +1,450 @@ +import { describe, expect, it } from 'vitest'; +import { userEvent } from 'vitest/browser'; +import { render } from 'vitest-browser-react'; +import type { ComponentProps, CSSProperties, ReactNode } from 'react'; +import { ThemeProvider } from '@wordpress/theme'; +// Load the token defaults and the package styles, as WordPress does. +import '@wordpress/theme/design-tokens.css'; +import '../../style.scss'; +import { GlobalStylesContext } from '../../context'; +import StylesPreviewColors from '../../preview-colors'; +import StylesPreviewTypography from '../../preview-typography'; +import PreviewStyles from '../../preview-styles'; +import Variation from '../variation'; + +type ThemeColor = ComponentProps< typeof ThemeProvider >[ 'color' ]; + +const DARK_THEME: ThemeColor = { background: '#1e1e1e' }; +const CUSTOM_PRIMARY_THEME: ThemeColor = { primary: '#8e44ad' }; + +const STROKE = 'var(--wpds-color-stroke-interactive-neutral)'; +const STROKE_ACTIVE = 'var(--wpds-color-stroke-interactive-neutral-active)'; +const SELECTED = 'var(--wpds-color-foreground-interactive-neutral)'; +const FOCUS = 'var(--wpds-color-stroke-focus)'; +const FOCUS_WIDTH = 'var(--wpds-border-width-focus)'; +const SURFACE = 'var(--wpds-color-background-surface-neutral)'; +const SURFACE_STRONG = 'var(--wpds-color-background-surface-neutral-strong)'; +const CONTENT = 'var(--wpds-color-foreground-content-neutral)'; + +const EMPTY_CONFIG = { settings: {}, styles: {} }; + +const PALETTE = [ '#f9f2e7', '#111111', '#d4a017', '#2a6f97' ]; + +const colorVariation = { + title: 'Evening', + settings: { + color: { + palette: { + theme: PALETTE.map( ( color, index ) => ( { + slug: `color-${ index }`, + name: `Color ${ index }`, + color, + } ) ), + }, + }, + }, + styles: { color: { background: PALETTE[ 0 ], text: PALETTE[ 1 ] } }, +}; + +const typographyVariation = { + title: 'Serif headings', + settings: { + typography: { + fontFamilies: { + theme: [ + { + slug: 'heading', + name: 'Heading', + fontFamily: 'Georgia, serif', + }, + { + slug: 'body', + name: 'Body', + fontFamily: '"Courier New", monospace', + }, + ], + }, + }, + }, + styles: { + typography: { fontFamily: 'var(--wp--preset--font-family--body)' }, + elements: { + heading: { + typography: { + fontFamily: 'var(--wp--preset--font-family--heading)', + }, + }, + }, + }, +}; + +const CARDS = { + color: { + variation: colorVariation, + element: ( + + { () => } + + ), + }, + typography: { + variation: typographyVariation, + element: ( + + { () => ( + + ) } + + ), + }, +}; + +type CardName = keyof typeof CARDS; + +async function renderInTheme( + color: ThemeColor, + children: ReactNode, + { + user = EMPTY_CONFIG, + hostStyle = { color: CONTENT }, + }: { user?: object; hostStyle?: CSSProperties } = {} +) { + const view = await render( + +
+
+
+ {}, + } } + > + { children } + +
+ + ); + const probe = view.getByTestId( 'probe' ).element() as HTMLElement; + + // Start each test with the pointer away from the cards. + await userEvent.hover( view.getByTestId( 'elsewhere' ) ); + + return { + view, + host: view.getByTestId( 'host' ).element() as HTMLElement, + getCard: ( name: string ) => + view.getByRole( 'button', { name } ).element() as HTMLElement, + // The color a token resolves to within this theme. + tokenColor( token: string ) { + probe.style.color = token; + return getComputedStyle( probe ).color; + }, + // The outline width a token resolves to within this theme. + tokenOutlineWidth( token: string ) { + probe.style.outline = `${ token } solid`; + return getComputedStyle( probe ).outlineWidth; + }, + }; +} + +function getPreview( card: HTMLElement ) { + return card.querySelector< HTMLElement >( + '.global-styles-ui-variations_item-preview' + )!; +} + +// The preview outline transitions between states. Wait for any transition to +// end so the computed style is the final one. +async function settle( element: HTMLElement ) { + await Promise.all( + element.getAnimations().map( ( animation ) => animation.finished ) + ); +} + +// The element that paints the preview background, once the preview has measured its width. +async function getFrame( card: HTMLElement ) { + const selector = '.global-styles-ui-preview__wrapper > div'; + await expect.poll( () => card.querySelector( selector ) ).not.toBeNull(); + return card.querySelector< HTMLElement >( selector )!; +} + +function getBoxShadowColor( element: HTMLElement ) { + return getComputedStyle( element ).boxShadow.match( + /^rgba?\([^)]*\)/ + )?.[ 0 ]; +} + +function getLuminance( color: string ) { + const [ r, g, b ] = color + .match( /[\d.]+/g )! + .slice( 0, 3 ) + .map( ( channel ) => { + const value = Number( channel ) / 255; + return value <= 0.04045 + ? value / 12.92 + : ( ( value + 0.055 ) / 1.055 ) ** 2.4; + } ); + return 0.2126 * r + 0.7152 * g + 0.0722 * b; +} + +function getContrast( first: string, second: string ) { + const [ lighter, darker ] = [ + getLuminance( first ), + getLuminance( second ), + ].sort( ( a, b ) => b - a ); + return ( lighter + 0.05 ) / ( darker + 0.05 ); +} + +function toRgb( hex: string ) { + const [ r, g, b ] = [ 1, 3, 5 ].map( ( index ) => + parseInt( hex.slice( index, index + 2 ), 16 ) + ); + return `rgb(${ r }, ${ g }, ${ b })`; +} + +describe( 'Variation cards in a dark theme', () => { + it.each( Object.keys( CARDS ) as CardName[] )( + 'outlines the %s card with the neutral interactive stroke', + async ( name ) => { + const { getCard, tokenColor } = await renderInTheme( + DARK_THEME, + CARDS[ name ].element + ); + + const preview = getPreview( + getCard( CARDS[ name ].variation.title ) + ); + await settle( preview ); + + expect( getComputedStyle( preview ).outlineColor ).toBe( + tokenColor( STROKE ) + ); + } + ); + + it.each( Object.keys( CARDS ) as CardName[] )( + 'switches the %s card outline to the active stroke on hover', + async ( name ) => { + const { getCard, tokenColor } = await renderInTheme( + DARK_THEME, + CARDS[ name ].element + ); + const card = getCard( CARDS[ name ].variation.title ); + + const preview = getPreview( card ); + + await userEvent.hover( card ); + await settle( preview ); + + expect( getComputedStyle( preview ).outlineColor ).toBe( + tokenColor( STROKE_ACTIVE ) + ); + } + ); + + it.each( Object.keys( CARDS ) as CardName[] )( + 'rings the selected %s card with the neutral interactive foreground, visible against the surface', + async ( name ) => { + const { host, getCard, tokenColor, tokenOutlineWidth } = + await renderInTheme( DARK_THEME, CARDS[ name ].element, { + user: CARDS[ name ].variation, + } ); + const card = getCard( CARDS[ name ].variation.title ); + const preview = getPreview( card ); + await settle( preview ); + const ring = getComputedStyle( preview ).outlineColor; + + expect( card ).toHaveAttribute( 'aria-current', 'true' ); + expect( ring ).toBe( tokenColor( SELECTED ) ); + expect( getComputedStyle( preview ).outlineWidth ).toBe( + tokenOutlineWidth( FOCUS_WIDTH ) + ); + expect( + getContrast( ring, getComputedStyle( host ).backgroundColor ) + ).toBeGreaterThanOrEqual( 3 ); + + // A surface band separates the ring from the preview colors. + expect( getBoxShadowColor( preview ) ).toBe( + tokenColor( SURFACE_STRONG ) + ); + expect( + getContrast( ring, getBoxShadowColor( preview )! ) + ).toBeGreaterThanOrEqual( 3 ); + + // Hovering keeps the selected ring. + await userEvent.hover( card ); + await settle( preview ); + expect( getComputedStyle( preview ).outlineColor ).toBe( ring ); + } + ); + + it.each( [ + [ 'follows the theme', { color: CONTENT } ], + [ 'sets no color', {} ], + ] )( + 'pairs the typography card background and text when the host text %s', + async ( _, hostStyle ) => { + const { view, getCard, tokenColor } = await renderInTheme( + DARK_THEME, + CARDS.typography.element, + { hostStyle } + ); + const frame = await getFrame( + getCard( typographyVariation.title ) + ); + const background = getComputedStyle( frame ).backgroundColor; + + expect( background ).toBe( tokenColor( SURFACE_STRONG ) ); + for ( const letter of [ 'A', 'a' ] ) { + const text = getComputedStyle( + view.getByText( letter, { exact: true } ).element() + ).color; + expect( text ).toBe( tokenColor( CONTENT ) ); + expect( + getContrast( text, background ) + ).toBeGreaterThanOrEqual( 4.5 ); + } + } + ); + + it( 'pairs the Browse styles card background and label when the site defines no colors', async () => { + const variation = { ...EMPTY_CONFIG, title: 'Default' }; + const { view, getCard, tokenColor } = await renderInTheme( + DARK_THEME, + + { ( isFocused ) => ( + + ) } + + ); + const frame = await getFrame( getCard( variation.title ) ); + + expect( getComputedStyle( frame ).backgroundColor ).toBe( + tokenColor( SURFACE_STRONG ) + ); + expect( + getComputedStyle( + view.getByText( variation.title, { exact: true } ).element() + ).color + ).toBe( tokenColor( CONTENT ) ); + } ); +} ); + +describe( 'Variation card focus', () => { + it.each( [ + [ 'a dark theme', DARK_THEME ], + [ 'a custom primary color', CUSTOM_PRIMARY_THEME ], + ] )( + 'shows a single focus ring with the focus stroke in %s', + async ( _, color ) => { + const { getCard, tokenColor, tokenOutlineWidth } = + await renderInTheme( color, CARDS.typography.element ); + const card = getCard( typographyVariation.title ); + const preview = getPreview( card ); + + await userEvent.tab(); + await settle( preview ); + + expect( card ).toHaveFocus(); + expect( getComputedStyle( preview ).outlineColor ).toBe( + tokenColor( FOCUS ) + ); + expect( getComputedStyle( preview ).outlineWidth ).toBe( + tokenOutlineWidth( FOCUS_WIDTH ) + ); + expect( getComputedStyle( card ).outlineStyle ).toBe( 'none' ); + + // Hovering keeps the focus ring. + await userEvent.hover( card ); + await settle( preview ); + expect( getComputedStyle( preview ).outlineColor ).toBe( + tokenColor( FOCUS ) + ); + } + ); +} ); + +describe( 'Variation card content in a dark theme', () => { + it( 'keeps the variation palette in the color stripes', async () => { + const { getCard } = await renderInTheme( + DARK_THEME, + CARDS.color.element + ); + const frame = await getFrame( getCard( colorVariation.title ) ); + + const stripes = Array.from( + frame.querySelectorAll< HTMLElement >( 'div' ) + ).filter( ( element ) => element.style.flexGrow === '1' ); + expect( + stripes.map( + ( stripe ) => getComputedStyle( stripe ).backgroundColor + ) + ).toEqual( expect.arrayContaining( PALETTE.map( toRgb ) ) ); + expect( stripes ).toHaveLength( PALETTE.length ); + } ); + + it( 'keeps the variation fonts in the typography sample', async () => { + const { view } = await renderInTheme( + DARK_THEME, + CARDS.typography.element + ); + + await expect + .element( view.getByText( 'A', { exact: true } ) ) + .toHaveStyle( { fontFamily: 'Georgia, serif' } ); + await expect + .element( view.getByText( 'a', { exact: true } ) ) + .toHaveStyle( { fontFamily: '"Courier New", monospace' } ); + } ); + + it( 'keeps the colors a variation defines on the typography card', async () => { + const variation = { + ...typographyVariation, + title: 'Gold', + styles: { + ...typographyVariation.styles, + color: { background: '#ffd700', text: '#123456' }, + }, + }; + const { view, getCard } = await renderInTheme( + DARK_THEME, + + { () => } + + ); + const frame = await getFrame( getCard( variation.title ) ); + + expect( getComputedStyle( frame ).backgroundColor ).toBe( + toRgb( '#ffd700' ) + ); + expect( + getComputedStyle( view.getByText( 'A', { exact: true } ).element() ) + .color + ).toBe( toRgb( '#123456' ) ); + } ); +} ); diff --git a/packages/global-styles-ui/tsconfig.json b/packages/global-styles-ui/tsconfig.json index 2f7fc1105b4b90..c3576c6ae41dec 100644 --- a/packages/global-styles-ui/tsconfig.json +++ b/packages/global-styles-ui/tsconfig.json @@ -23,6 +23,7 @@ { "path": "../kebab-case/tsconfig.build.json" }, { "path": "../keycodes/tsconfig.build.json" }, { "path": "../private-apis/tsconfig.build.json" }, + { "path": "../theme/tsconfig.build.json" }, { "path": "../ui/tsconfig.build.json" } ] } From e857ed5acc25385cf7a31c9867a8245cc56d232e Mon Sep 17 00:00:00 2001 From: Jerry Jones Date: Thu, 3 Dec 2026 09:41:20 -0600 Subject: [PATCH 2/2] Docs: Link the global-styles-ui changelog entry to #87913 Co-Authored-By: Claude Opus 5.5 --- packages/global-styles-ui/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/global-styles-ui/CHANGELOG.md b/packages/global-styles-ui/CHANGELOG.md index fb7c4d6df0f1c6..542b676767fa1f 100644 --- a/packages/global-styles-ui/CHANGELOG.md +++ b/packages/global-styles-ui/CHANGELOG.md @@ -4,7 +4,7 @@ ### Enhancements -- Use theme colors for the outlines of the color, typography, and style variation cards, and for their preview background and text when the site defines no colors ([#TBD](https://github.com/WordPress/gutenberg/pull/TBD)). +- Use theme colors for the outlines of the color, typography, and style variation cards, and for their preview background and text when the site defines no colors ([#87913](https://github.com/WordPress/gutenberg/pull/87913)). ## 2.3.0 (2026-12-02)