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 1 commit
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
3 changes: 2 additions & 1 deletion packages/css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"access": "public"
},
"dependencies": {
"csstype": "^2.5.7"
"csstype": "^2.5.7",
"is-object": "^1.0.1"
}
}
15 changes: 12 additions & 3 deletions packages/css/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import isObject from 'is-object'
import { CSSObject, SystemStyleObject, UseThemeFunction, Theme } from './types'

export * from './types'

const { hasOwnProperty } = {}

export function get(
obj: object,
key: string | number,
Expand All @@ -13,10 +16,16 @@ 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

if (isObject(obj) && hasOwnProperty.call(obj, 'default')) {
return (obj as any).default
}

return obj
}

export const defaultBreakpoints = [40, 52, 64].map(n => n + 'em')
export const defaultBreakpoints = [40, 52, 64].map((n) => n + 'em')

const defaultTheme = {
space: [0, 4, 8, 16, 32, 64, 128, 256, 512],
Expand Down Expand Up @@ -223,7 +232,7 @@ const responsive = (styles: Exclude<SystemStyleObject, UseThemeFunction>) => (
(theme && (theme.breakpoints as string[])) || defaultBreakpoints
const mediaQueries = [
null,
...breakpoints.map(n => `@media screen and (min-width: ${n})`),
...breakpoints.map((n) => `@media screen and (min-width: ${n})`),
]

for (const key in styles) {
Expand Down
50 changes: 46 additions & 4 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',
colorScale: {
default: 'color500',
100: 'color100',
jxnblk marked this conversation as resolved.
Show resolved Hide resolved
500: 'color500',
900: 'color900',
},
colorScaleNoDefault: {
100: 'color100',
500: 'color500',
900: 'color900',
},
},
fontSizes: [12, 14, 16, 24, 36],
fonts: {
Expand Down Expand Up @@ -196,7 +207,7 @@ test('works with the css prop', () => {
})

test('works with functional arguments', () => {
const result = css(t => ({
const result = css((t) => ({
color: t.colors.primary,
}))(theme)
expect(result).toEqual({
Expand All @@ -206,13 +217,44 @@ test('works with functional arguments', () => {

test('supports functional values', () => {
const result = css({
color: t => t.colors.primary,
color: (t) => t.colors.primary,
})(theme)
expect(result).toEqual({
color: 'tomato',
})
})

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

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

jxnblk marked this conversation as resolved.
Show resolved Hide resolved
test('returns object when accessing object value with no default key', () => {
const result = css({
color: 'colorScaleNoDefault',
})(theme)
expect(result).toEqual({
color: {
jxnblk marked this conversation as resolved.
Show resolved Hide resolved
'100': 'color100',
'500': 'color500',
'900': 'color900',
},
})
})

test('returns variants from theme', () => {
const result = css({
variant: 'buttons.primary',
Expand Down Expand Up @@ -285,7 +327,7 @@ test('handles negative margins from scale that is an object', () => {
const result = css({
mt: '-s',
mx: '-m',
})({...theme, space: { s: '16px', m: '32px' }})
})({ ...theme, space: { s: '16px', m: '32px' } })
expect(result).toEqual({
marginTop: '-16px',
marginLeft: '-32px',
Expand Down Expand Up @@ -344,7 +386,7 @@ test('ignores array values longer than breakpoints', () => {

test('functional values can return responsive arrays', () => {
const result = css({
color: t => [t.colors.primary, t.colors.secondary],
color: (t) => [t.colors.primary, t.colors.secondary],
})(theme)
expect(result).toEqual({
'@media screen and (min-width: 40em)': {
Expand Down