Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
feat(utils): New getLength CSS utility
Browse files Browse the repository at this point in the history
getLength is a new utility function that converts
numbers to the rem unit, but passes through all other values
  • Loading branch information
diondiondion committed Feb 27, 2020
1 parent 5211b18 commit efcb177
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
11 changes: 11 additions & 0 deletions src/utils/getLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {pxToRem} from './units';

function getLength(length) {
if (typeof shortCode === 'number') {
return pxToRem(length);
}

return length;
}

export default getLength;
32 changes: 32 additions & 0 deletions src/utils/getSpacing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {pxToRem} from './units';

const passThroughValues = ['auto'];

function getSpacing(shortCode, theme) {
if (shortCode === undefined || shortCode === null || shortCode === false) {
return shortCode;
}

if (
(typeof shortCode !== 'number' && typeof shortCode !== 'string') ||
shortCode === '0'
) {
return '0';
}

if (typeof shortCode === 'number') {
return pxToRem(shortCode);
}

if (passThroughValues.indexOf(shortCode) > -1) {
return shortCode;
}

if (shortCode.charAt(0) === '-') {
return '-' + theme.globals.spacing[shortCode.substring(1)];
} else {
return theme.globals.spacing[shortCode];
}
}

export default getSpacing;
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export {default as colors} from './colors';
export {default as mergeRefs} from './mergeRefs';
export {default as getLength} from './getLength';
export {default as getSpacing} from './getSpacing';
export {default as spacing} from './spacing';
export {default as truncateText} from './truncateText';
export {default as units} from './units';
35 changes: 5 additions & 30 deletions src/utils/spacing.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
import {pxToRem} from './units';

const passThroughValues = ['auto'];

function getSpacing(shortCode, theme) {
if (shortCode === undefined || shortCode === null || shortCode === false) {
return shortCode;
}

if (
(typeof shortCode !== 'number' && typeof shortCode !== 'string') ||
shortCode === '0'
) {
return '0';
}

if (typeof shortCode === 'number') {
return pxToRem(shortCode);
}

if (passThroughValues.indexOf(shortCode) > -1) {
return shortCode;
}

if (shortCode.charAt(0) === '-') {
return '-' + theme.globals.spacing[shortCode.substring(1)];
} else {
return theme.globals.spacing[shortCode];
}
}
import getSpacing from './getSpacing';

/**
* Legacy export for backwards compatibility.
* Prefer `import {getSpacing} from 'utils';`
*/
export {getSpacing};

0 comments on commit efcb177

Please sign in to comment.