Skip to content
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

fix(color-modes): make useRootStyles=false work with useCustomProperties #2519

Merged
merged 15 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

- uses: pnpm/action-setup@v2
with:
version: 8
version: 9.12.1

- uses: actions/setup-node@v3
with:
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:

- uses: pnpm/action-setup@v2
with:
version: 8
version: 9.12.1

- uses: actions/setup-node@v3
with:
Expand All @@ -90,7 +90,7 @@ jobs:
registry-url: 'https://registry.npmjs.org'

- name: Install
run: pnpm install --no-optional --no-frozen-lockfile
run: pnpm install --no-optional

- name: Queue in release turnstile
id: turnstyle
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- uses: pnpm/action-setup@v2
with:
version: 7
version: 9.12.1

- uses: actions/setup-node@v2
with:
Expand Down
4 changes: 2 additions & 2 deletions auto.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const conventionalCommitsOptions: ConventionalCommitsOptions = {
defaultReleaseType: 'none',
}

const allContributorsOptions: IAllContributorsPluginOptions = {
const _allContributorsOptions: IAllContributorsPluginOptions = {
exclude: [
'dependabot',
'dependabot[bot]',
Expand Down Expand Up @@ -61,7 +61,7 @@ export default function config(): AutoRc {
['conventional-commits', conventionalCommitsOptions],
'first-time-contributor',
'released',
['all-contributors', allContributorsOptions],
// ['all-contributors', allContributorsOptions],
['omit-commits', omitCommitsOptions],
// 'magic-zero',
],
Expand Down
2 changes: 1 addition & 1 deletion examples/gatsby-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "gatsby build"
},
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"gatsby": "^5",
"gatsby-plugin-mdx": "^5",
"gatsby-plugin-theme-ui": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion examples/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "gatsby build"
},
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"@mdx-js/react": "^2.3.0",
"@theme-ui/mdx": "workspace:^",
"gatsby": "^5",
Expand Down
2 changes: 1 addition & 1 deletion examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"@mdx-js/loader": "^2.3.0",
"@mdx-js/react": "^2.3.0",
"@next/mdx": "^14.0.4",
Expand Down
45 changes: 23 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,56 +63,57 @@
}
},
"dependencies": {
"@auto-it/all-contributors": "^10.46.0",
"@auto-it/conventional-commits": "^10.46.0",
"@auto-it/core": "^10.46.0",
"@auto-it/first-time-contributor": "^10.46.0",
"@auto-it/magic-zero": "^10.46.0",
"@auto-it/npm": "^10.46.0",
"@auto-it/omit-commits": "^11.0.4",
"@auto-it/released": "^10.46.0",
"@auto-it/all-contributors": "^11.2.1",
"@auto-it/conventional-commits": "^11.2.1",
"@auto-it/core": "^11.2.1",
"@auto-it/first-time-contributor": "^11.2.1",
"@auto-it/magic-zero": "^11.2.1",
"@auto-it/npm": "^11.2.1",
"@auto-it/omit-commits": "^11.2.1",
"@auto-it/released": "^11.2.1",
"@babel/cli": "^7.22.5",
"@babel/helper-string-parser": "^7.22.5",
"@babel/helper-validator-identifier": "^7.22.5",
"@babel/plugin-transform-runtime": "^7.24.3",
"@babel/preset-env": "^7.22.5",
"@babel/preset-react": "^7.22.5",
"@babel/preset-typescript": "^7.22.5",
"@babel/runtime": "^7.22.5",
"@babel/runtime": "^7.25.7",
"@codechecks/build-size-watcher": "^0.1.0",
"@codechecks/client": "0.1.12",
"@emotion/jest": "^11.11.0",
"@jest/types": "^29.5.0",
"@preconstruct/cli": "^2.7.0",
"@emotion/jest": "^11.13.0",
"@jest/types": "^29.6.3",
"@preconstruct/cli": "^2.8.9",
"@testing-library/react": "^14.2.1",
"@types/eslint": "8.56.2",
"@types/jest": "^29.5.2",
"@types/jest": "^29.5.13",
"@types/node": "^20.3.1",
"@types/react-dom": "^18.2.5",
"@types/react-test-renderer": "^18.3.0",
"@types/semver": "^7.5.0",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"auto": "^11.0.4",
"auto": "^11.2.1",
"babel-jest": "^29.7.0",
"babel-preset-gatsby": "^3.10.0",
"babel-preset-gatsby": "^3.13.2",
"cross-env": "^7.0.3",
"egzek": "^1.2.0",
"eslint": "^8.56.0",
"eslint-config-react-app": "^7.0.1",
"jest": "^29.5.0",
"jest-canvas-mock": "^2.5.1",
"jest-environment-jsdom": "^29.5.0",
"jest": "^29.7.0",
"jest-canvas-mock": "^2.5.2",
"jest-environment-jsdom": "^29.7.0",
"jest-mock-console": "^2.0.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.8.8",
"react-test-renderer": "^18.3.1",
"rimraf": "^5.0.1",
"semver": "^7.5.1",
"ts-jest": "^29.1.0",
"semver": "^7.6.3",
"ts-jest": "^29.2.5",
"ts-toolbelt": "^9.6.0",
"typecov": "^0.2.3",
"typescript": "^5"
}
"typescript": "^5.6.3"
},
"packageManager": "[email protected]+sha512.e5a7e52a4183a02d5931057f7a0dbff9d5e9ce3161e33fa68ae392125b79282a8a8a470a51dfc8a0ed86221442eb2fb57019b0990ed24fab519bf0e1bc5ccfc4"
}
4 changes: 2 additions & 2 deletions packages/color-modes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"deepmerge": "^4.2.2"
},
"peerDependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"react": ">=18"
},
"devDependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"@theme-ui/test-utils": "workspace:^",
"@types/react": "^18.2.12",
"react": "^18.1.0"
Expand Down
65 changes: 37 additions & 28 deletions packages/color-modes/src/custom-properties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ColorMode, ColorModesScale, css, Theme } from '@theme-ui/css'
import {
ColorMode,
ColorModesScale,
css,
Theme,
ThemeUICSSObject,
} from '@theme-ui/css'

const toVarName = (key: string) => `--theme-ui-${key.replace('-__default', '')}`
const toVarValue = (key: string) => `var(${toVarName(key)})`
Expand Down Expand Up @@ -75,40 +81,43 @@ export const __createColorStyles = (theme: Theme = {}) => {
} = theme.config || theme || {}
const colors = theme.rawColors || theme.colors

if (!colors || useRootStyles === false) return {}
if (useCustomProperties === false) {
return css({
color: 'text',
bg: 'background',
})(theme)
}

const modes = colors.modes || {}
if (!colors) return {}

const styles = __createColorProperties(colors, modes)
const res: ThemeUICSSObject = {}

if (printColorModeName) {
let printMode = modes[printColorModeName]
if (!printMode && printColorModeName === initialColorModeName)
printMode = colors

if (printMode) {
styles['@media print'] = __objectToVars('colors', printMode)
if (useRootStyles !== false) {
if (useCustomProperties === false) {
res.color = 'text'
res.bg = 'background'
} else {
console.error(
`Theme UI \`printColorModeName\` was not found in colors scale`,
{ colors, printColorModeName }
)
res.color = toVarValue('colors-text')
res.bg = toVarValue('colors-background')
}
}

const colorToVarValue = (color: string) => toVarValue(`colors-${color}`)
if (useCustomProperties !== false) {
const modes = colors.modes || {}
const styles = __createColorProperties(colors, modes)

if (printColorModeName) {
let printMode = modes[printColorModeName]
if (!printMode && printColorModeName === initialColorModeName)
printMode = colors

if (printMode) {
styles['@media print'] = __objectToVars('colors', printMode)
} else {
console.error(
`Theme UI \`printColorModeName\` was not found in colors scale`,
{ colors, printColorModeName }
)
}
}

Object.assign(res, styles)
}

return css({
...styles,
color: colorToVarValue('text'),
bg: colorToVarValue('background'),
})(theme)
return css(res)(theme)
}

/**
Expand Down
12 changes: 5 additions & 7 deletions packages/color-modes/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -903,12 +903,11 @@ test('colorMode accepts function from previous state to new one', () => {
test('warns when localStorage is disabled', () => {
const restoreConsole = mockConsole()

const localStorage = window.localStorage
Object.defineProperty(window, 'localStorage', {
get: jest.fn(() => {
const spy = jest
.spyOn(Storage.prototype, 'getItem')
.mockImplementation(() => {
throw new Error('SecurityError: The operation is insecure.')
}),
})
})

let mode = ''
const Consumer = () => {
Expand All @@ -927,8 +926,6 @@ test('warns when localStorage is disabled', () => {

expect(mode).toBe(undefined)

Object.defineProperty(window, 'localStorage', { value: localStorage })

expect((console.warn as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(`
[
"localStorage is disabled and color mode might not work as expected.",
Expand All @@ -937,6 +934,7 @@ test('warns when localStorage is disabled', () => {
]
`)

spy.mockClear()
restoreConsole()
})

Expand Down
4 changes: 2 additions & 2 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"@types/styled-system": "^5.1.13"
},
"peerDependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"@theme-ui/theme-provider": "workspace:^",
"react": ">=18"
},
"devDependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"@theme-ui/test-utils": "workspace:^",
"@types/react": "^18.2.12",
"react": "^18.1.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/jsx-dev-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"sideEffects": false,
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"react": "^18"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/jsx-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"sideEffects": false,
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"react": "^18"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"deepmerge": "^4.2.2"
},
"peerDependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"react": ">=18"
},
"devDependencies": {
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"@theme-ui/test-utils": "workspace:^",
"@types/react": "^18.2.12",
"react": "^18.1.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"access": "public"
},
"dependencies": {
"csstype": "^3.0.10"
"csstype": "3.0.10"
},
"peerDependencies": {
"@emotion/react": "^11.11.1"
Expand Down
3 changes: 3 additions & 0 deletions packages/css/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,11 @@ test('supports vendor properties', () => {
test('omits empty values', () => {
expect(
css({
// @ts-ignore TS2873
color: false && 'blue',
// @ts-ignore TS2873
backgroundColor: undefined && 'whitesmoke',
// @ts-ignore TS2873
textDecoration: null && 'underline',
border: '1px solid black',
})(theme)
Expand Down
6 changes: 3 additions & 3 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"icon": "npx repng src/components/logo.js -d static -f icon.png -w=32 -h=32 --props='{\"size\":32}'"
},
"dependencies": {
"unist-util-visit-2": "npm:unist-util-visit@^2.0.2",
"unist-util-visit-2": "npm:[email protected].3",
"@babel/helper-string-parser": "^7.22.5",
"@emotion/react": "^11.11.1",
"@emotion/react": "^11.13.3",
"@mdx-js/loader": "^2.3.0",
"@mdx-js/react": "^2.3.0",
"@theme-ui/color": "workspace:^",
Expand Down Expand Up @@ -58,7 +58,7 @@
"react-dom": "^18",
"react-helmet": "^6.1.0",
"react-live": "^2.1.2",
"remark-gfm-1": "npm:remark-gfm@^1.0.0",
"remark-gfm-1": "npm:[email protected]",
"remark-slug": "^6",
"stringify-object": "^3",
"theme-ui": "workspace:^",
Expand Down
4 changes: 3 additions & 1 deletion packages/docs/src/components/sidenav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ export const AccordionButton = (props: {
onClick: EventHandler<MouseEvent<HTMLButtonElement>>
}) => {
const transform = props.open ? 'rotate(-180 8 8)' : 'rotate(0 8 8)'
const disabled = props.pathname ? props.pathname.includes(props.href) : false
const disabled = props.pathname
? props.href.startsWith(props.pathname)
: false

return (
<button
Expand Down
Loading
Loading