Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add native RtL css support #193

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/__tests__/boxModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ it('transforms margin, padding with 1 value', () => {
})
})

// RTL
it('transforms margin-inline-start with marginStart', () => {
expect(transformCss([['margin-inline-start', '1px']])).toEqual({
marginStart: 1,
})
})

it('transforms start with start', () => {
expect(transformCss([['start', '1px']])).toEqual({
start: 1,
})
})

it('transforms margin, padding with 2 values', () => {
expect(transformCss([['margin', '1px 2px']])).toEqual({
marginTop: 1,
Expand Down Expand Up @@ -117,6 +130,8 @@ it('transforms margin shorthand with auto', () => {
})
})



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run prettier on this file?

it('transforms border width', () => {
expect(transformCss([['border-width', '1px 2px 3px 4px']])).toEqual({
borderTopWidth: 1,
Expand Down
21 changes: 15 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ const transformShorthandValue =
process.env.NODE_ENV === 'production'
? baseTransformShorthandValue
: (propName, value) => {
try {
return baseTransformShorthandValue(propName, value)
} catch (e) {
throw new Error(`Failed to parse declaration "${propName}: ${value}"`)
}
try {
return baseTransformShorthandValue(propName, value)
} catch (e) {
throw new Error(`Failed to parse declaration "${propName}: ${value}"`)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier again


export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
const isRawValue = allowShorthand === false || !(propName in transforms)
Expand All @@ -70,12 +70,21 @@ export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
return propValues
}

const cssRtLToNativeCamelized = {
"margin-inline-start": "marginStart",
"margin-inline-end": "marginEnd",
"padding-inline-start": "paddingStart",
"padding-inline-end": "paddingEnd",
"inset-inline-start": "start",
"inset-inline-end": "end"
}

export const getPropertyName = propName => {
const isCustomProp = /^--\w+/.test(propName)
if (isCustomProp) {
return propName
}
return camelizeStyleName(propName)
return cssRtLToNativeCamelized[propName] || camelizeStyleName(propName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return cssRtLToNativeCamelized[propName] || camelizeStyleName(propName)
return cssRtLToNativeCamelized[propName] ?? camelizeStyleName(propName)

}

export default (rules, shorthandBlacklist = []) =>
Expand Down