This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): New getLength CSS utility
getLength is a new utility function that converts numbers to the rem unit, but passes through all other values
- Loading branch information
1 parent
5211b18
commit efcb177
Showing
4 changed files
with
50 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |