1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import capitalizeString from '../internalHelpers/_capitalizeString'
- import type { Styles } from '../types/style'
- const positionMap = ['Top', 'Right', 'Bottom', 'Left']
- function generateProperty(property: string, position: string) {
- if (!property) return position.toLowerCase()
- const splitProperty = property.split('-')
- if (splitProperty.length > 1) {
- splitProperty.splice(1, 0, position)
- return splitProperty.reduce((acc, val) => `${acc}${capitalizeString(val)}`)
- }
- const joinedProperty = property.replace(/([a-z])([A-Z])/g, `$1${position}$2`)
- return property === joinedProperty ? `${property}${position}` : joinedProperty
- }
- function generateStyles(property: string, valuesWithDefaults: Array<?string | ?number>) {
- const styles = {}
- for (let i = 0; i < valuesWithDefaults.length; i += 1) {
- if (valuesWithDefaults[i] || valuesWithDefaults[i] === 0) {
- styles[generateProperty(property, positionMap[i])] = valuesWithDefaults[i]
- }
- }
- return styles
- }
- export default function directionalProperty(
- property: string,
- ...values: Array<?string | ?number>
- ): Styles {
-
- const [firstValue, secondValue = firstValue, thirdValue = firstValue, fourthValue = secondValue] = values
- const valuesWithDefaults = [firstValue, secondValue, thirdValue, fourthValue]
- return generateStyles(property, valuesWithDefaults)
- }
|