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

Support a "default" key for object in scales #951

Merged
merged 20 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
395cedd
[RFC] "default" key for object in scales
folz May 22, 2020
6711427
Changes per PR review comments
folz May 26, 2020
16debaf
Remove is-object dependency
hasparus Dec 2, 2020
3e60ce3
Loosen the check, default can be a responsive tuple or an object
hasparus Dec 2, 2020
4a72cea
chore: rename master to develop and previous to stable
hasparus Dec 9, 2020
da05d78
chore(deps-dev): bump lint-staged from 10.5.1 to 10.5.3
dependabot-preview[bot] Dec 9, 2020
2b74593
chore(deps-dev): bump babel-preset-gatsby from 0.6.0 to 0.8.0
dependabot-preview[bot] Dec 9, 2020
b4a66c3
chore(deps-dev): bump execa from 4.1.0 to 5.0.0
dependabot-preview[bot] Dec 9, 2020
746b278
docs: link to develop and stable branches in readme
hasparus Dec 9, 2020
c35d0e5
chore(deps): bump @emotion/react from 11.1.1 to 11.1.2
dependabot-preview[bot] Dec 9, 2020
ca2e3de
Merge pull request #1341 from system-ui/dependabot/npm_and_yarn/emoti…
hasparus Dec 9, 2020
1048979
chore(deps): [security] bump ini from 1.3.5 to 1.3.7
dependabot-preview[bot] Dec 10, 2020
112490f
Merge pull request #1343 from system-ui/dependabot/npm_and_yarn/lint-…
hasparus Dec 12, 2020
23a27be
Merge pull request #1356 from system-ui/dependabot/npm_and_yarn/ini-1…
hasparus Dec 12, 2020
38e4830
Merge pull request #1344 from system-ui/dependabot/npm_and_yarn/babel…
hasparus Dec 12, 2020
b23b659
chore(deps-dev): bump babel-jest from 26.1.0 to 26.6.3
dependabot-preview[bot] Dec 12, 2020
18c4b70
Merge pull request #1342 from system-ui/dependabot/npm_and_yarn/babel…
hasparus Dec 12, 2020
a7650c7
Merge pull request #1340 from system-ui/dependabot/npm_and_yarn/execa…
hasparus Dec 12, 2020
3c3f329
Merge branch 'master' into develop
hasparus Dec 12, 2020
ec81d22
Merge branch 'develop' into get-default
hasparus Dec 14, 2020
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
2 changes: 1 addition & 1 deletion deprecated/chrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ See [Blocks UI](https://github.com/blocks/blocks) for a theme editor and JSX pag

## Installation

1. [Download extension](https://github.com/system-ui/theme-ui/tree/master/packages/chrome/public)
1. [Download extension](https://github.com/system-ui/theme-ui/tree/stable/packages/chrome/public)
1. Navigate to chrome://extensions/
1. Enable "Developer mode"
1. Click "LOAD UNPACKED"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@types/jest": "^26.0.10",
"@types/react-test-renderer": "^16.9.2",
"babel-jest": "^26.0.1",
"babel-preset-gatsby": "^0.6.0",
"babel-preset-gatsby": "^0.8.0",
"husky": ">=4.0.7",
"jest": "^26.0.1",
"jest-canvas-mock": "^2.2.0",
Expand Down
24 changes: 15 additions & 9 deletions packages/css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {

export * from './types'

const hasDefault = (x: unknown): x is { default: string | number } => {
return typeof x === 'object' && x !== null && 'default' in x
}

export function get(
obj: object,
key: string | number | undefined,
Expand All @@ -19,7 +23,9 @@ export function get(
for (p = 0; p < path.length; p++) {
obj = obj ? (obj as any)[path[p]!] : undef
}
return obj === undef ? def : obj
if (obj === undef) return def

return hasDefault(obj) ? obj.default : obj
}

export const defaultBreakpoints = [40, 52, 64].map((n) => n + 'em')
Expand Down Expand Up @@ -262,8 +268,8 @@ const responsive = (
continue
}
next[media] = next[media] || {}
if (value[i] == null) continue;
(next[media] as Record<string, any>)[key] = value[i]
if (value[i] == null) continue
;(next[media] as Record<string, any>)[key] = value[i]
}
}
return next
Expand All @@ -288,15 +294,15 @@ export const css = (args: ThemeUIStyleObject = {}) => (
for (const key in obj) {
const x = obj[key as keyof typeof styles]
if (key === 'variant') {
const val = typeof x === 'function' ? x(theme) : x;
const variant = get(theme, val as string);
result = { ...result, ...variant };
const val = typeof x === 'function' ? x(theme) : x
const variant = get(theme, val as string)
result = { ...result, ...variant }
} else {
result[key] = x as CSSObject;
}
result[key] = x as CSSObject
}
}
} else {
result = obj as CSSObject ;
result = obj as CSSObject
}
const styles = responsive(result as ThemeUICSSObject)(theme)
result = {}
Expand Down
70 changes: 70 additions & 0 deletions packages/css/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ const theme: Theme = {
secondary: 'cyan',
background: 'white',
text: 'black',
purple: {
default: 'darkviolet',
100: 'rebeccapurple',
500: 'darkviolet',
900: 'violet',
},
pink: {
100: 'mediumvioletred',
500: 'hotpink',
900: 'pink',
},
},
fontSizes: [12, 14, 16, 24, 36],
fonts: {
Expand All @@ -24,6 +35,13 @@ const theme: Theme = {
sidebar: 320,
},
buttons: {
default: {
px: 4,
py: 2,
fontWeight: 'bold',
color: 'secondary',
bg: 'background',
},
primary: {
p: 3,
fontWeight: 'bold',
Expand Down Expand Up @@ -229,6 +247,58 @@ test('supports functional values', () => {
})
})

test('returns `default` key when accessing object value with default', () => {
const result = css({
color: 'purple',
})(theme)
expect(result).toEqual({
color: 'darkviolet',
})
})

test('returns nested key when accessing key from object value with default', () => {
const result = css({
color: 'purple.100',
})(theme)
expect(result).toEqual({
color: 'rebeccapurple',
})
})

test('variant prop returns `default` key when accessing variant object with default', () => {
const result = css({
variant: 'buttons',
})(theme)

expect(result).toEqual({
paddingLeft: 32,
paddingRight: 32,
paddingTop: 8,
paddingBottom: 8,
fontWeight: 600,
color: 'cyan',
backgroundColor: 'white',
})
})

test('returns object when accessing object value with no default key', () => {
const result = css({
color: 'pink',
})(theme)
// Note: Returning this object is the expected behavior; however, an object
// value like this isn't able to become valid CSS. Ensure the theme path
// points to a primitive value (such as 'pink.100') when intending to make
// CSS out of these values.
// Ref: https://github.com/system-ui/theme-ui/pull/951#discussion_r430697168
expect(result).toEqual({
color: {
100: 'mediumvioletred',
500: 'hotpink',
900: 'pink',
},
})
})

test('returns variants from theme', () => {
const result = css({
variant: 'buttons.primary',
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/components/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const images = {
flatiron:
'https://images.unsplash.com/photo-1520222984843-df35ebc0f24d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9',
logo:
'https://raw.githubusercontent.com/system-ui/theme-ui/master/packages/docs/static/logo.png',
'https://raw.githubusercontent.com/system-ui/theme-ui/stable/packages/docs/static/logo.png',
}

const scope = {
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/components/edit-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const EditLink = ({ base, children, ...props }) => (
)

EditLink.defaultProps = {
base: 'https://github.com/system-ui/theme-ui/edit/master/packages/docs/src',
base: 'https://github.com/system-ui/theme-ui/edit/develop/packages/docs/src',
children: 'Edit the page on GitHub',
}

Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/pages/migrating.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ title: Migrating
- The `ThemeProvider` now adds global typographic styles to the `<body>` element based on `theme.styles.root`. To disable this behavior set the `useBodyStyles: false` flag in your theme.
- Theme context is now stateless. If you've made use of `context.setTheme`, this no longer works. An alternative approach is available with the `@theme-ui/editor` package.
- The `ThemeStateProvider` component is no longer avialable, see `@theme-ui/editor` as an alternative.
- The `@theme-ui/editor` package has a completely new API. Please refer to the package's [README.md](https://github.com/system-ui/theme-ui/blob/master/packages/editor/README.md) for more information.
- The `@theme-ui/editor` package has a completely new API. Please refer to the package's [README.md](https://github.com/system-ui/theme-ui/blob/stable/packages/editor/README.md) for more information.

## v0.2

Expand Down
4 changes: 2 additions & 2 deletions packages/docs/src/pages/recipes/demo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsx jsx */
import { jsx, Themed } from 'theme-ui'

const XRay = props => (
const XRay = (props) => (
<div
{...props}
sx={{
Expand All @@ -12,7 +12,7 @@ const XRay = props => (
/>
)

export default props => (
export default (props) => (
<XRay>
<pre>Demo/debugging page</pre>
<div
Expand Down
21 changes: 11 additions & 10 deletions packages/docs/src/pages/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ These values will be used to generate mobile-first (i.e. `min-width`) media quer

The theme object can also include configuration options for Theme UI. The following keys can be used to enable and disable certain features.

Flag | Default | Description
---|---|---
`useCustomProperties` | `true` | Enables CSS custom properties to help mitigate a flash of unstyled content during rehydration
`useBodyStyles` | `true` | Adds styles defined in `theme.styles.root` to the `<body>` element along with `color` and `background-color`
`initialColorModeName` | `'default'` | The key used for the top-level color palette in `theme.colors`
`useColorSchemeMediaQuery` | `false` | Initializes the color mode based on the `prefers-color-scheme` media query
`useBorderBox` | `true` | Adds a global `box-sizing: border-box` style
`useLocalStorage` | `true` | Persists the color mode in `localStorage`
| Flag | Default | Description |
| -------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------ |
| `useCustomProperties` | `true` | Enables CSS custom properties to help mitigate a flash of unstyled content during rehydration |
| `useBodyStyles` | `true` | Adds styles defined in `theme.styles.root` to the `<body>` element along with `color` and `background-color` |
| `initialColorModeName` | `'default'` | The key used for the top-level color palette in `theme.colors` |
| `useColorSchemeMediaQuery` | `false` | Initializes the color mode based on the `prefers-color-scheme` media query |
| `useBorderBox` | `true` | Adds a global `box-sizing: border-box` style |
| `useLocalStorage` | `true` | Persists the color mode in `localStorage` |

## Example Theme

Expand All @@ -187,7 +187,8 @@ export const theme = {
breakpoints: ['40em', '52em', '64em'],
space: [0, 4, 8, 16, 32, 64, 128, 256, 512],
fonts: {
body: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif',
body:
'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif',
heading: 'inherit',
monospace: 'Menlo, monospace',
},
Expand Down Expand Up @@ -275,5 +276,5 @@ export const theme = {

For more information on the Theme UI `theme` object, see the [Theme Specification docs](/theme-spec).

[example]: https://github.com/system-ui/theme-ui/tree/master/packages/preset-base/src/index.ts
[example]: https://github.com/system-ui/theme-ui/tree/stable/packages/preset-base/src/index.ts
[emotion]: https://emotion.sh
2 changes: 1 addition & 1 deletion packages/prism/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default {
}
```

The following themes are available and can be found in the [`presets/`](https://github.com/system-ui/theme-ui/tree/master/packages/prism/presets) directory.
The following themes are available and can be found in the [`presets/`](https://github.com/system-ui/theme-ui/tree/stable/packages/prism/presets) directory.

- `dracula.json`
- `duotone-dark.json`
Expand Down
2 changes: 1 addition & 1 deletion packages/tachyons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@theme-ui/css": "0.6.0-alpha.1",
"babel-polyfill": "^6.26.0",
"execa": "^4.0.0"
"execa": "^5.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@theme-ui/css": "0.6.0-alpha.1",
"babel-polyfill": "^6.26.0",
"execa": "^4.0.0",
"execa": "^5.0.0",
"tailwindcss": "^1.0.4"
},
"publishConfig": {
Expand Down
9 changes: 6 additions & 3 deletions packages/theme-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
alt="Tree Shaking"
/>
</a>
<a href="https://github.com/system-ui/theme-ui/blob/master/LICENSE.md">
<a href="https://github.com/system-ui/theme-ui/blob/stable/LICENSE.md">
<img
src="https://badgen.net/badge/license/MIT/blue"
alt="MIT license"
Expand All @@ -64,8 +64,11 @@
\
Theme UI is a library for creating themeable user interfaces based on constraint-based design principles. Build custom component libraries, design systems, web applications, Gatsby themes, and more with a flexible API for best-in-class developer ergonomics.

**stable docs**: https://theme-ui.com \
**next (v0.6.0-alpha) docs**: [https://development--dev-theme-ui.netlify.app/](https://development--dev-theme-ui.netlify.app/)
**[stable] docs**: https://theme-ui.com \
**[develop] (v0.6.0-alpha) docs**: [https://development--dev-theme-ui.netlify.app/](https://development--dev-theme-ui.netlify.app/)

[stable]: https://github.com/system-ui/theme-ui/tree/stable
[develop]: https://github.com/system-ui/theme-ui/tree/develop

---

Expand Down
Loading