Skip to content

Commit

Permalink
Do not define a default color-scheme on Provider if one is defined at…
Browse files Browse the repository at this point in the history
… the page level (adobe#6897)
  • Loading branch information
devongovett committed Aug 16, 2024
1 parent c8d4985 commit ac6116d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/@react-spectrum/s2/src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import type {ColorScheme, Router} from '@react-types/provider';
import {colorScheme, UnsafeStyles} from './style-utils' with {type: 'macro'};
import {createContext, JSX, ReactNode, useContext} from 'react';
import {generateDefaultColorSchemeStyles} from './page.macro' with {type: 'macro'};
import {I18nProvider, RouterProvider, useLocale} from 'react-aria-components';
import {mergeStyles} from '../style/runtime';
import {style} from '../style/spectrum-theme' with {type: 'macro'};
Expand Down Expand Up @@ -52,7 +53,7 @@ export const ColorSchemeContext = createContext<ColorScheme | 'light dark' | nul
export function Provider(props: ProviderProps) {
let result = <ProviderInner {...props} />;
let parentColorScheme = useContext(ColorSchemeContext);
let colorScheme = props.colorScheme || parentColorScheme || 'light dark';
let colorScheme = props.colorScheme || parentColorScheme;
if (colorScheme !== parentColorScheme) {
result = <ColorSchemeContext.Provider value={colorScheme}>{result}</ColorSchemeContext.Provider>;
}
Expand All @@ -68,6 +69,8 @@ export function Provider(props: ProviderProps) {
return result;
}

generateDefaultColorSchemeStyles();

let providerStyles = style({
...colorScheme(),
'--s2-container-bg': {
Expand All @@ -80,7 +83,14 @@ let providerStyles = style({
}
}
},
backgroundColor: '--s2-container-bg'
backgroundColor: {
// Don't set a background unless one is requested.
background: {
base: '--s2-container-bg',
'layer-1': '--s2-container-bg',
'layer-2': '--s2-container-bg'
}
}
});

function ProviderInner(props: ProviderProps) {
Expand Down
24 changes: 24 additions & 0 deletions packages/@react-spectrum/s2/src/page.macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@ export function generatePageStyles(this: MacroContext | void) {
});
}
}

// This generates a low specificity rule to define default values for
// --lightningcss-light and --lightningcss-dark. This is used when rendering
// a <Provider> without setting a colorScheme prop, and when page.css is not present.
// It is equivalent to setting `color-scheme: light dark`, but without overriding
// the browser default for content outside the provider.
export function generateDefaultColorSchemeStyles(this: MacroContext | void) {
if (this && typeof this.addAsset === 'function') {
this.addAsset({
type: 'css',
content: `@layer _.a {
:where(html) {
--lightningcss-light: initial;
--lightningcss-dark: ;
@media (prefers-color-scheme: dark) {
--lightningcss-light: ;
--lightningcss-dark: initial;
}
}
}`
});
}
}
2 changes: 2 additions & 0 deletions packages/@react-spectrum/s2/src/style-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export const fieldInput = () => ({

export const colorScheme = () => ({
colorScheme: {
// Default to page color scheme if none is defined.
default: '[var(--lightningcss-light, light) var(--lightningcss-dark, dark)]',
colorScheme: {
'light dark': 'light dark',
light: 'light',
Expand Down

0 comments on commit ac6116d

Please sign in to comment.