Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.
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
11 changes: 8 additions & 3 deletions packages/system/src/style.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { minBreakpoint, minWidth, DEFAULT_BREAKPOINTS } from './media'
import { is, num, string, obj, identity, merge, get, func } from './util'
import { is, num, string, obj, merge, get, func } from './util'

function callOrReturn(fn, arg) {
if (!func(fn)) return fn
Expand Down Expand Up @@ -28,7 +28,12 @@ function styleFromValue(cssProperties, value, theme, themeKey, transform) {
if (string(computedValue) || num(computedValue)) {
const style = {}
for (let i = 0; i < cssProperties.length; i++) {
style[cssProperties[i]] = transform(computedValue, variants)
style[cssProperties[i]] = transform
? transform(computedValue, {
rawValue: value,
variants,
})
: computedValue
}
return style
}
Expand Down Expand Up @@ -112,7 +117,7 @@ export function style({
prop,
cssProperties,
themeKey = null,
transform = identity,
transform = null,
}) {
function getStyle(attrs, theme) {
const value = attrs[prop]
Expand Down
10 changes: 5 additions & 5 deletions packages/system/src/styles/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { num, negative } from '../util'

const DEFAULT_SPACING = [0, 8, 16, 24, 32, 40, 48, 56, 64, 72]

function transform(n, variants = DEFAULT_SPACING) {
if (!num(n)) {
return variants[n] || n
function transform(transformedValue, { rawValue, variants = DEFAULT_SPACING }) {
if (!num(rawValue)) {
return variants[rawValue] || rawValue
}
const abs = Math.abs(n)
const neg = negative(n)
const abs = Math.abs(rawValue)
const neg = negative(rawValue)
const value = variants[abs] || abs
if (!num(value)) {
return neg ? `-${value}` : value
Expand Down
1 change: 1 addition & 0 deletions packages/system/src/styles/space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { space } from './space'

describe('space', () => {
it('should support m', () => {
expect(space.props({ m: 1 })).toEqual({ margin: 8 })
expect(space.props({ m: 2 })).toEqual({ margin: 16 })
expect(space.props({ m: -2 })).toEqual({ margin: -16 })
expect(space.props({ m: 10 })).toEqual({ margin: 10 })
Expand Down
1 change: 0 additions & 1 deletion packages/system/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const num = n => typeof n === 'number' && !Number.isNaN(n)
export const string = n => typeof n === 'string' && n !== ''
export const obj = n => typeof n === 'object' && n !== null
export const func = n => typeof n === 'function'
export const identity = n => n
export const negative = n => n < 0

export const get = (obj, path) =>
Expand Down