-
Notifications
You must be signed in to change notification settings - Fork 8
chore: Adds inheritsMode support for visual contexts #188
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
Open
pan-kot
wants to merge
1
commit into
main
Choose a base branch
from
chore-internal-inherits-mode
base: main
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 all commits
Commits
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
This file contains hidden or 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,78 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { Theme, Context } from '../shared/theme'; | ||
| import { colorMode, createStubPropertiesMap, densityMode } from './common'; | ||
|
|
||
| // Fixtures for visual contexts inheritance via inheritsMode. | ||
| // These contexts inherit tokens from the specified non-default mode ("dark", "compact"). | ||
|
|
||
| export { colorMode, densityMode }; | ||
|
|
||
| export const topNavigationContext: Context = { | ||
| id: 'top-navigation', | ||
| selector: '.top-navigation', | ||
| inheritsMode: 'dark', | ||
| // Own override applied on top of the inherited dark values. | ||
| tokens: { bgColor: 'navy' }, | ||
| }; | ||
|
|
||
| export const compactTableContext: Context = { | ||
| id: 'compact-table', | ||
| selector: '.compact-table', | ||
| inheritsMode: 'compact', | ||
| // No own overrides: pure inheritance of the compact density state. | ||
| tokens: {}, | ||
| }; | ||
|
|
||
| export const theme: Theme = { | ||
| id: 'inheritance', | ||
| selector: 'body', | ||
| tokens: { | ||
| fontFamily: 'Arial', | ||
| textColor: { light: 'black', dark: 'white' }, | ||
| bgColor: { light: 'white', dark: 'black' }, | ||
| linkColor: { light: 'blue', dark: 'cyan' }, | ||
| spaceScaled: { comfortable: '20px', compact: '4px' }, | ||
| }, | ||
| tokenModeMap: { textColor: 'color', bgColor: 'color', linkColor: 'color', spaceScaled: 'density' }, | ||
| contexts: { 'top-navigation': topNavigationContext, 'compact-table': compactTableContext }, | ||
| modes: { color: colorMode, density: densityMode }, | ||
| }; | ||
|
|
||
| export const secondaryTheme: Theme = { | ||
| ...theme, | ||
| id: 'inheritance-secondary', | ||
| selector: '.secondary', | ||
| // The secondary theme tweaks the dark link color. | ||
| tokens: { | ||
| ...theme.tokens, | ||
| linkColor: { light: 'green', dark: 'lime' }, | ||
| }, | ||
| contexts: { 'top-navigation': { ...topNavigationContext } }, | ||
| }; | ||
|
|
||
| export const propertiesMap = createStubPropertiesMap(theme); | ||
|
|
||
| // Visual context that uses legacy defaultMode property. This must not trigger selectors | ||
| // dedup, applied when inheritsMode is used. | ||
| export const legacyContext: Context = { | ||
| id: 'legacy-top-navigation', | ||
| selector: '.legacy-top-navigation', | ||
| defaultMode: 'dark', | ||
| tokens: { | ||
| // Dark values copied into the context (mode-invariant), mirroring how | ||
| // components author `defaultMode` contexts via pickState('dark'). | ||
| textColor: { light: 'white', dark: 'white' }, | ||
| bgColor: { light: 'navy', dark: 'navy' }, | ||
| linkColor: { light: 'cyan', dark: 'cyan' }, | ||
| }, | ||
| }; | ||
|
|
||
| export const legacyTheme: Theme = { | ||
| ...theme, | ||
| id: 'legacy', | ||
| contexts: { | ||
| 'legacy-top-navigation': legacyContext, | ||
| }, | ||
| }; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
81 changes: 81 additions & 0 deletions
81
src/shared/declaration/__tests__/__snapshots__/inheritance.test.ts.snap
This file contains hidden or 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,81 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`visual context inheritance > legacy theme output snapshot (no dedup) 1`] = ` | ||
| "@layer awsui-base-theme { | ||
| body{ | ||
| --fontFamily-css:Arial; | ||
| --textColor-css:black; | ||
| --bgColor-css:white; | ||
| --linkColor-css:blue; | ||
| --spaceScaled-css:20px; | ||
| } | ||
| @media not print {.dark{ | ||
| --textColor-css:white; | ||
| --bgColor-css:black; | ||
| --linkColor-css:cyan; | ||
| }} | ||
| .compact{ | ||
| --spaceScaled-css:4px; | ||
| } | ||
| .legacy-top-navigation{ | ||
| --textColor-css:white; | ||
| --bgColor-css:navy; | ||
| --linkColor-css:cyan; | ||
| } | ||
| }" | ||
| `; | ||
|
|
||
| exports[`visual context inheritance > multi theme output snapshot 1`] = ` | ||
| "@layer awsui-base-theme { | ||
| body{ | ||
| --fontFamily-css:Arial; | ||
| --textColor-css:black; | ||
| --bgColor-css:white; | ||
| --linkColor-css:blue; | ||
| --spaceScaled-css:20px; | ||
| } | ||
| @media not print {.dark,.top-navigation{ | ||
| --textColor-css:white; | ||
| --bgColor-css:black; | ||
| --linkColor-css:cyan; | ||
| }} | ||
| .compact,.compact-table{ | ||
| --spaceScaled-css:4px; | ||
| } | ||
| @media not print {.top-navigation{ | ||
| --bgColor-css:navy; | ||
| }} | ||
| .secondary{ | ||
| --linkColor-css:green; | ||
| } | ||
| @media not print {.dark.secondary,.secondary .top-navigation,.secondary.top-navigation{ | ||
| --linkColor-css:lime; | ||
| }} | ||
| @media not print {.secondary .top-navigation,.secondary.top-navigation{ | ||
| --bgColor-css:navy; | ||
| }} | ||
| }" | ||
| `; | ||
|
|
||
| exports[`visual context inheritance > single theme output snapshot 1`] = ` | ||
| "@layer awsui-base-theme { | ||
| body{ | ||
| --fontFamily-css:Arial; | ||
| --textColor-css:black; | ||
| --bgColor-css:white; | ||
| --linkColor-css:blue; | ||
| --spaceScaled-css:20px; | ||
| } | ||
| @media not print {.dark,.top-navigation{ | ||
| --textColor-css:white; | ||
| --bgColor-css:black; | ||
| --linkColor-css:cyan; | ||
| }} | ||
| .compact,.compact-table{ | ||
| --spaceScaled-css:4px; | ||
| } | ||
| @media not print {.top-navigation{ | ||
| --bgColor-css:navy; | ||
| }} | ||
| }" | ||
| `; |
This file contains hidden or 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,160 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, test, expect } from 'vitest'; | ||
| import * as fixtures from '../../../__fixtures__/inherits-mode'; | ||
| import { createBuildDeclarations } from '../index'; | ||
| import { Theme } from '../../theme'; | ||
|
|
||
| const usedTokens = Object.keys(fixtures.theme.tokens); | ||
|
|
||
| function render(theme: Theme, secondary: Theme[] = []): string { | ||
| return createBuildDeclarations(theme, secondary, fixtures.propertiesMap, (s) => s, usedTokens); | ||
| } | ||
|
|
||
| function renderLegacy(): string { | ||
| return createBuildDeclarations(fixtures.legacyTheme, [], fixtures.propertiesMap, (s) => s, usedTokens); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the body of the rule whose selector is exactly `selector`. The | ||
| * selector must be a full selector (preceded by a rule/block boundary), so that | ||
| * looking up `.top-navigation` does not accidentally match the shared | ||
| * `.dark,.top-navigation` rule. | ||
| */ | ||
| function matchExactRule(css: string, selector: string): null | string { | ||
| const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| const match = css.match(new RegExp(`(?:^|[\\n{}])${escaped}\\{([^}]*)\\}`)); | ||
| return match ? match[1].trim() : null; | ||
| } | ||
|
|
||
| describe('visual context inheritance', () => { | ||
| test('single theme output snapshot', () => { | ||
| expect(render(fixtures.theme, [])).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('multi theme output snapshot', () => { | ||
| expect(render(fixtures.theme, [fixtures.secondaryTheme])).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('legacy theme output snapshot (no dedup)', () => { | ||
| expect(renderLegacy()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('top-navigation context inherits from dark mode via selector aliases', () => { | ||
| const css = render(fixtures.theme); | ||
| expect(css).toMatch(/\.dark,\.top-navigation/); | ||
| }); | ||
|
|
||
| test(`shared dark + top-navigation rule stays within mode's media rule`, () => { | ||
| const css = render(fixtures.theme); | ||
| expect(css).toMatch(/@media not print \{\.dark,\.top-navigation\{/); | ||
| }); | ||
|
|
||
| test('top-navigation override rule only includes unique tokens', () => { | ||
| const css = render(fixtures.theme); | ||
|
|
||
| // bgColor is the only token top-navigation overrides relative to dark. | ||
| const standalone = matchExactRule(css, '.top-navigation'); | ||
| expect(standalone).not.toBeNull(); | ||
| expect(standalone).toBe('--bgColor-css:navy;'); | ||
|
|
||
| // The override rule stays inside the inherited mode's media rule. | ||
| expect(css).toMatch(/@media not print \{\.top-navigation\{/); | ||
|
|
||
| // The standalone rule comes after the shared rule, so it wins the cascade. | ||
| expect(Array.from(css.matchAll(/\.top-navigation/g))).toHaveLength(2); | ||
| expect(css.indexOf('{.dark,.top-navigation')).toBeLessThan(css.indexOf('{.top-navigation')); | ||
| }); | ||
|
|
||
| test('shared compact rule is media-free (compact has no media query)', () => { | ||
| const css = render(fixtures.theme); | ||
| const shared = matchExactRule(css, '.compact,.compact-table'); | ||
| expect(shared).not.toBeNull(); | ||
| expect(shared).toContain('--spaceScaled-css:4px;'); | ||
| // Not wrapped in any media query. | ||
| expect(Array.from(css.matchAll(/\.compact-table/g))).toHaveLength(1); | ||
| expect(css).toMatch(/(^|\n)\.compact,\.compact-table\{/); | ||
| }); | ||
|
|
||
| test('compact override rule is not emitted (it has no unique tokens)', () => { | ||
| const css = render(fixtures.theme); | ||
| expect(css).not.toMatch(/(^|\n|\})\.compact-table\{/); | ||
| }); | ||
|
|
||
| test('non-inherited orthogonal combinations are not created', () => { | ||
| const css = render(fixtures.theme); | ||
| expect(css).not.toContain('.dark,.compact-table'); | ||
| expect(css).not.toContain('.compact,.top-navigation'); | ||
| }); | ||
|
|
||
| test('secondary dark mode rule is shared with the secondary context selector', () => { | ||
| const css = render(fixtures.theme, [fixtures.secondaryTheme]); | ||
| expect(css).toMatch(/\.dark\.secondary[^{]*\.top-navigation/); | ||
| }); | ||
|
|
||
| test('the secondary inheriting context retains the token instead of leaking the primary value', () => { | ||
| const primary: Theme = { | ||
| id: 'primary', | ||
| selector: 'body', | ||
| tokens: { | ||
| paletteLight: 'plight', | ||
| paletteDark: 'pdark', | ||
| btnBg: { light: '{paletteLight}', dark: '{paletteDark}' }, | ||
| }, | ||
| tokenModeMap: { btnBg: 'color' }, | ||
| contexts: { ctx: { id: 'ctx', selector: '.ctx', inheritsMode: 'dark', tokens: {} } }, | ||
| modes: { color: fixtures.colorMode }, | ||
| }; | ||
| const secondary: Theme = { | ||
| id: 'secondary', | ||
| selector: '.secondary', | ||
| tokens: { | ||
| paletteLight: 'plight', | ||
| paletteDark: 'pdark', | ||
| borderColor: { light: '{paletteLight}', dark: '{paletteDark}' }, | ||
| // Mode-invariant reference in the secondary theme: the emitted var string | ||
| // is identical in light and dark, so it would be deduplicated away. | ||
| btnBg: '{borderColor}', | ||
| }, | ||
| tokenModeMap: { borderColor: 'color', btnBg: 'color' }, | ||
| contexts: { ctx: { id: 'ctx', selector: '.ctx', inheritsMode: 'dark', tokens: {} } }, | ||
| modes: { color: fixtures.colorMode }, | ||
| }; | ||
| const usedTokens = ['paletteLight', 'paletteDark', 'borderColor', 'btnBg']; | ||
| const propertiesMap = usedTokens.reduce((acc, t) => ({ ...acc, [t]: `--${t}-css` }), {}); | ||
| const css = createBuildDeclarations(primary, [secondary], propertiesMap, (s) => s, usedTokens); | ||
|
|
||
| // Primary .ctx carries the --btnBg-css with the primary dark value. | ||
| expect(matchExactRule(css, '.dark,.ctx')).toBe('--btnBg-css:var(--paletteDark-css);'); | ||
| // Secondary .ctx re-declares --btnBg-css with the secondary value so it wins over the primary. | ||
| expect(matchExactRule(css, '.secondary .ctx,.ctx.secondary')).toBe('--btnBg-css:var(--borderColor-css);'); | ||
| }); | ||
|
|
||
| test('inheritance is ignored if inherited mode state does not exist or is default', () => { | ||
| const theme: Theme = { | ||
| id: 'theme', | ||
| selector: 'body', | ||
| tokens: { | ||
| btnBg: { light: 'black', dark: 'white' }, | ||
| btnFg: 'yellow', | ||
| }, | ||
| tokenModeMap: { btnBg: 'color', btnFg: 'color' }, | ||
| contexts: { | ||
| u: { id: 'unknown', selector: '.unknown', inheritsMode: '?', tokens: { btnFg: 'green' } }, | ||
| default: { id: 'default', selector: '.default', inheritsMode: 'light', tokens: { btnFg: 'purple' } }, | ||
| }, | ||
| modes: { color: fixtures.colorMode }, | ||
| }; | ||
| const usedTokens = ['btnBg', 'btnFg']; | ||
| const propertiesMap = usedTokens.reduce((acc, t) => ({ ...acc, [t]: `--${t}-css` }), {}); | ||
| const css = createBuildDeclarations(theme, [], propertiesMap, (s) => s, usedTokens); | ||
|
|
||
| expect(matchExactRule(css, 'body')).toBe('--btnBg-css:black;\n\t--btnFg-css:yellow;'); | ||
| expect(matchExactRule(css, '.dark')).toBe('--btnBg-css:white;'); | ||
| expect(matchExactRule(css, '.unknown')).toBe('--btnFg-css:green;'); | ||
| expect(matchExactRule(css, '.default')).toBe('--btnFg-css:purple;'); | ||
| expect(Array.from(css.matchAll(/\.unknown/g))).toHaveLength(1); | ||
| expect(Array.from(css.matchAll(/\.default/g))).toHaveLength(1); | ||
| }); | ||
| }); | ||
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.
If we keep defaultMode as backwards-compatible property, should we add a test that guarantees it's previous output (not producing the same behavior as inheritedMode)?
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.
Yes, this test already exists: https://github.com/cloudscape-design/theming-core/pull/188/changes#diff-cfb5e560a311d0c145221d390a01210d25065f7eb529b4f21d9e3d0c8d3e23acR41