You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					33 lines
				
				817 B
			
		
		
			
		
	
	
					33 lines
				
				817 B
			| 
											3 years ago
										 | // @flow | ||
|  | const cssRegex = /^([+-]?(?:\d+|\d*\.\d+))([a-z]*|%)$/ | ||
|  | 
 | ||
|  | /** | ||
|  |  * Returns a given CSS value and its unit as elements of an array. | ||
|  |  * | ||
|  |  * @example | ||
|  |  * // Styles as object usage | ||
|  |  * const styles = { | ||
|  |  *   '--dimension': getValueAndUnit('100px')[0], | ||
|  |  *   '--unit': getValueAndUnit('100px')[1], | ||
|  |  * } | ||
|  |  * | ||
|  |  * // styled-components usage | ||
|  |  * const div = styled.div` | ||
|  |  *   --dimension: ${getValueAndUnit('100px')[0]}; | ||
|  |  *   --unit: ${getValueAndUnit('100px')[1]}; | ||
|  |  * ` | ||
|  |  * | ||
|  |  * // CSS in JS Output | ||
|  |  * | ||
|  |  * element { | ||
|  |  *   '--dimension': 100, | ||
|  |  *   '--unit': 'px', | ||
|  |  * } | ||
|  |  */ | ||
|  | export default function getValueAndUnit(value: string | number): any { | ||
|  |   if (typeof value !== 'string') return [value, ''] | ||
|  |   const matchedValue = value.match(cssRegex) | ||
|  |   if (matchedValue) return [parseFloat(value), matchedValue[2]] | ||
|  |   return [value, undefined] | ||
|  | } |