curls
Version:
💪 Responsive, expressive UI primitives for React written with Style Hooks and Emotion
95 lines (79 loc) • 2.3 kB
JavaScript
exports.__esModule = true
exports.default = exports.isDirectional = exports.directionalRe = void 0
var _core = require('@emotion/core')
var _unit = _interopRequireDefault(require('./unit'))
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj}
}
const ws = /\s+/
const directionalRe = /(?=[\d]+|Auto)/
exports.directionalRe = directionalRe
const defaultDirections = {
_: ['top', 'right', 'bottom', 'left'],
t: ['top'],
r: ['right'],
b: ['bottom'],
l: ['left'],
y: ['top', 'bottom'],
x: ['right', 'left'],
}
const isDirectionalRe = /[a-z]/i
const isDirectional = value =>
typeof value === 'string' &&
value.trim() !== 'auto' &&
isDirectionalRe.test(value)
exports.isDirectional = isDirectional
var _default = (
prefix,
modScale,
modValue,
unit = 'px',
directions = defaultDirections
) => {
let CSS = [],
i = 0,
j,
modVals = String(modValue).split(ws)
for (; i < modVals.length; i++) {
const val = modVals[i]
if (!val) continue
let [abbr, ...value] = val.split(directionalRe)
value = value.join('')
const isAbbrValue = isNaN(parseInt(abbr)) === false || abbr === 'auto'
if (abbr.length === 0 || isAbbrValue === true) {
value = isAbbrValue ? abbr : val
abbr = '_'
}
const direction = directions[abbr]
if (process.env.NODE_ENV !== 'production')
if (direction === void 0)
throw new Error(
`Unrecognized direction '${abbr}' in ${prefix}: ${abbr}\n\n` +
`Allowed values include: ${Object.keys(directions).join(', ')}`
)
let size = modScale[value]
if (size === void 0) {
if (value === 'Auto') size = 'auto'
else {
if (process.env.NODE_ENV !== 'production')
throw new Error(
`Unrecognized scale value in ${prefix}: ${value}\n\n` +
`Allowed values include: ${Object.keys(modScale).join(', ')}`
)
}
}
for (j = 0; j < direction.length; j++)
CSS.push(
/*#__PURE__*/
(0, _core.css)(
prefix.replace('{XYZ}', direction[j]),
':',
(0, _unit.default)(size, unit),
';'
)
)
}
return CSS.length === 1 ? CSS[0] : CSS
}
exports.default = _default