Repository navigation
fix(bulma-ui): make Theme colorMode switch the scheme under the prefixed builds #894
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01efe6e
fix(bulma-ui): make Theme colorMode switch the scheme under the prefi…
allxsmith 75d504a
fix(bulma-ui): warn when a class prefix leaves Theme colorMode no att…
allxsmith 0e084e0
Merge remote-tracking branch 'origin/main' into fix/theme-colormode-p…
allxsmith ece8511
fix(bulma-ui): name the prefixed theme attribute wherever colorMode i…
allxsmith 81b02df
fix(bulma-ui): name the prefixed theme attribute in the skill lists a…
allxsmith 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // `colorMode` flips Bulma's scheme by writing a theme attribute on <html>. | ||
| // Bulma names that attribute after the class prefix a sheet was built with | ||
| // (`data-<prefix>theme`), so a prefixed build never reads `data-theme`. An | ||
| // attribute reaching the DOM proves nothing about which one the sheet reads, | ||
| // so these compile every published stylesheet that carries a dark scheme and | ||
| // resolve the scheme variable on the root, the way DateInput.styles.test.tsx | ||
| // checks the calendar against its real SCSS. | ||
| // | ||
| // jsdom applies an `@media` block only when its list names `screen`, so the | ||
| // visitor's OS preference is simulated by renaming the sheet's own | ||
| // `prefers-color-scheme` block for that scheme, which keeps the block where | ||
| // Bulma put it in the cascade. | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import * as sass from 'sass'; | ||
| import { render } from '@testing-library/react'; | ||
| import { ConfigProvider } from '../Config'; | ||
| import { Theme } from '../Theme'; | ||
|
|
||
| const PKG = path.resolve(__dirname, '../../..'); | ||
| const SCSS = path.join(PKG, 'src', 'scss'); | ||
|
|
||
| const compile = (file: string) => | ||
| sass.compile(path.join(SCSS, file), { | ||
| loadPaths: [path.resolve(PKG, '../node_modules')], | ||
| quietDeps: true, | ||
| logger: sass.Logger.silent, | ||
| }).css; | ||
|
|
||
| /** | ||
| * Every published stylesheet built with Bulma's full theme set, read off | ||
| * `exports` the way Loader.styles.test.tsx does, with the class prefix it | ||
| * sets. The light-only flavor and `extras.css` carry no dark scheme for | ||
| * `colorMode` to reach. | ||
| */ | ||
| function themedStylesheets(): Array<{ file: string; prefix: string }> { | ||
| const manifest = JSON.parse( | ||
| fs.readFileSync(path.join(PKG, 'package.json'), 'utf8') | ||
| ) as { exports: Record<string, { default?: string }> }; | ||
| const files = new Set<string>(); | ||
| for (const entry of Object.values(manifest.exports)) { | ||
| const target = entry?.default; | ||
| if (target?.endsWith('.css')) { | ||
| files.add(target.replace(/^\.\/dist\//, '').replace(/\.css$/, '.scss')); | ||
| } | ||
| } | ||
| return [...files] | ||
| .sort() | ||
| .map(file => ({ | ||
| file, | ||
| source: fs.readFileSync(path.join(SCSS, file), 'utf8'), | ||
| })) | ||
| .filter(({ source }) => /bulma\/sass(?:\/themes)?['"]/.test(source)) | ||
| .map(({ file, source }) => ({ | ||
| file, | ||
| prefix: /\$class-prefix:\s*["']([^"']*)["']/.exec(source)?.[1] ?? '', | ||
| })); | ||
| } | ||
|
|
||
| const SHEETS = themedStylesheets(); | ||
|
|
||
| it('checks a prefixed and an unprefixed stylesheet', () => { | ||
| // Floor against the manifest read or the filter going quietly empty. | ||
| expect(SHEETS.map(sheet => sheet.prefix)).toEqual( | ||
| expect.arrayContaining(['', 'bestax-']) | ||
| ); | ||
| }); | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| type Os = 'light' | 'dark'; | ||
|
|
||
| /** The sheet as a visitor whose OS prefers `os` sees it. */ | ||
| const forOs = (css: string, os: Os) => { | ||
| const block = `@media (prefers-color-scheme: ${os})`; | ||
| expect(css).toContain(block); | ||
| return css.split(block).join('@media screen'); | ||
| }; | ||
|
|
||
| /** What the root resolves `--bulma-scheme-main-l` to. */ | ||
| const schemeMainL = () => | ||
| getComputedStyle(document.documentElement) | ||
| .getPropertyValue('--bulma-scheme-main-l') | ||
| .trim(); | ||
|
|
||
| describe.each(SHEETS)('$file', ({ file, prefix }) => { | ||
| let css: string; | ||
| let light: string; | ||
| let dark: string; | ||
| let sheet: HTMLStyleElement | undefined; | ||
|
|
||
| const loadAs = (os: Os) => { | ||
| sheet?.remove(); | ||
| sheet = document.createElement('style'); | ||
| sheet.textContent = forOs(css, os); | ||
| document.head.appendChild(sheet); | ||
| }; | ||
|
|
||
| beforeAll(() => { | ||
| css = compile(file); | ||
| // The scheme each of Bulma's own theme blocks sets, read off the sheet, | ||
| // so the expectations follow Bulma rather than restating its values. | ||
| const value = (scheme: Os) => { | ||
| const rule = new RegExp( | ||
| `\\[data-${prefix}theme=${scheme}\\][^{]*\\{[^}]*--bulma-scheme-main-l:\\s*([^;]+);` | ||
| ).exec(css); | ||
| if (!rule) throw new Error(`${file} has no ${scheme} theme block`); | ||
| return rule[1].trim(); | ||
| }; | ||
| light = value('light'); | ||
| dark = value('dark'); | ||
| expect(light).not.toBe(dark); | ||
| }, 60000); | ||
|
|
||
| afterEach(() => { | ||
| sheet?.remove(); | ||
| sheet = undefined; | ||
| for (const name of Array.from(document.documentElement.attributes)) { | ||
| if (name.name.endsWith('theme')) { | ||
| document.documentElement.removeAttribute(name.name); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| const renderTheme = ( | ||
| colorMode: 'light' | 'dark' | 'system', | ||
| isRoot: boolean | ||
| ) => | ||
| render( | ||
| <ConfigProvider classPrefix={prefix}> | ||
| <Theme colorMode={colorMode} isRoot={isRoot}> | ||
| <span>Content</span> | ||
| </Theme> | ||
| </ConfigProvider> | ||
| ); | ||
|
|
||
| it('follows the OS when nothing sets the scheme', () => { | ||
| loadAs('light'); | ||
| expect(schemeMainL()).toBe(light); | ||
| loadAs('dark'); | ||
| expect(schemeMainL()).toBe(dark); | ||
| }); | ||
|
|
||
| describe.each([ | ||
| ['a scoped Theme', false], | ||
| ['a root Theme', true], | ||
| ])('%s', (_label, isRoot) => { | ||
| it('turns the page dark with colorMode="dark" on a light OS', () => { | ||
| loadAs('light'); | ||
| renderTheme('dark', isRoot); | ||
| expect(schemeMainL()).toBe(dark); | ||
| }); | ||
|
|
||
| it('keeps the page light with colorMode="light" on a dark OS', () => { | ||
| loadAs('dark'); | ||
| renderTheme('light', isRoot); | ||
| expect(schemeMainL()).toBe(light); | ||
| }); | ||
|
|
||
| it('hands the scheme back to the OS with colorMode="system"', () => { | ||
| loadAs('dark'); | ||
| const { rerender } = renderTheme('light', isRoot); | ||
| expect(schemeMainL()).toBe(light); | ||
| rerender( | ||
| <ConfigProvider classPrefix={prefix}> | ||
| <Theme colorMode="system" isRoot={isRoot}> | ||
| <span>Content</span> | ||
| </Theme> | ||
| </ConfigProvider> | ||
| ); | ||
| expect(schemeMainL()).toBe(dark); | ||
| }); | ||
|
|
||
| it('hands the scheme back when it unmounts', () => { | ||
| loadAs('light'); | ||
| const { unmount } = renderTheme('dark', isRoot); | ||
| expect(schemeMainL()).toBe(dark); | ||
| unmount(); | ||
| expect(schemeMainL()).toBe(light); | ||
| }); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.