Repository navigation
Theme: forward ThemeProvider cornerRadius preset to :root for root providers #79153
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
Changes from all commits
338420f
7f0fa3b
89743f0
415e57d
708164e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| // is asserted on the provider's own scoping element (where the property is | ||
| // defined and from which real children would inherit) and on the document root. | ||
|
|
||
| import { readFileSync } from 'fs'; | ||
| import { join } from 'path'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { ThemeProvider } from '../theme-provider'; | ||
|
|
||
|
|
@@ -21,6 +23,7 @@ jest.mock( '../style.module.css', () => ( { | |
| const BRAND_BG = '--wpds-color-background-interactive-brand-strong'; | ||
| const SURFACE_BG = '--wpds-color-background-surface-neutral'; | ||
| const CURSOR_CONTROL = '--wpds-cursor-control'; | ||
| const BORDER_RADIUS_SM = '--wpds-border-radius-sm'; | ||
| const PRIMARY = '#1e90ff'; | ||
| const OTHER_PRIMARY = '#8e44ad'; | ||
| const BACKGROUND = '#f8f8f8'; | ||
|
|
@@ -127,6 +130,72 @@ describe( 'ThemeProvider', () => { | |
|
|
||
| expect( readProp( document.documentElement, BRAND_BG ) ).toBe( '' ); | ||
| } ); | ||
|
|
||
| // Unlike color/cursor (emitted at runtime in the provider's own | ||
| // `<style>`), the `cornerRadius` preset resolves to | ||
| // `--wpds-border-radius-*` values through the prebuilt design-token CSS | ||
| // (the modes authored in `terrazzo.config.ts`). A root provider relies | ||
| // on that stylesheet's `:root:has( [data-wpds-root-provider='true']… )` | ||
| // rule to forward the preset to the document element, so these tests | ||
| // load the prebuilt CSS to exercise it. It is injected only for this | ||
| // block (it also defines base tokens on `:root`) to avoid interfering | ||
| // with the color assertions above. | ||
| describe( 'cornerRadius forwarding', () => { | ||
| let prebuiltStyle: HTMLStyleElement; | ||
|
|
||
| beforeAll( () => { | ||
| prebuiltStyle = document.createElement( 'style' ); | ||
| prebuiltStyle.textContent = readFileSync( | ||
| join( __dirname, '../prebuilt/css/design-tokens.css' ), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tangent: I wonder if we could save ourselves some future headache adopting something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea, I'll add it to #77462 as a separate task
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow-up at #79362 |
||
| 'utf8' | ||
| ); | ||
| document.head.appendChild( prebuiltStyle ); | ||
| } ); | ||
|
|
||
| afterAll( () => { | ||
| prebuiltStyle.remove(); | ||
| } ); | ||
|
|
||
| it( 'forwards the preset to the document root when isRoot is set', () => { | ||
| render( | ||
| <ThemeProvider isRoot cornerRadius="moderate"> | ||
| <div data-testid="child">x</div> | ||
| </ThemeProvider> | ||
| ); | ||
|
|
||
| const provider = getScopingProvider( | ||
| screen.getByTestId( 'child' ) | ||
| ); | ||
| const forwarded = readProp( | ||
| document.documentElement, | ||
| BORDER_RADIUS_SM | ||
| ); | ||
|
|
||
| // `:root` resolves to the same `moderate` value as the provider. | ||
| expect( forwarded ).toBeTruthy(); | ||
| expect( forwarded ).toBe( | ||
| readProp( provider, BORDER_RADIUS_SM ) | ||
| ); | ||
| } ); | ||
|
|
||
| it( 'does not forward the preset to the document root by default', () => { | ||
| render( | ||
| <ThemeProvider cornerRadius="moderate"> | ||
| <div data-testid="child">x</div> | ||
| </ThemeProvider> | ||
| ); | ||
|
|
||
| const provider = getScopingProvider( | ||
| screen.getByTestId( 'child' ) | ||
| ); | ||
|
|
||
| // `:root` keeps the base preset rather than the provider's | ||
| // `moderate` one, since the provider is not a root provider. | ||
| expect( | ||
| readProp( document.documentElement, BORDER_RADIUS_SM ) | ||
| ).not.toBe( readProp( provider, BORDER_RADIUS_SM ) ); | ||
| } ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'nested providers', () => { | ||
|
|
||
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.
Arguably a bug fix, but since it hasn't released yet I suppose it's not a bug quite yet!