Skip to content

Commit

Permalink
feat(modules): All properties produced by polished are now camel-cased (
Browse files Browse the repository at this point in the history
#210)

* feat(modules): All properties produced by polished are now camel-cased

* docs(modules): Removed incorrect spread usage in several module docs
  • Loading branch information
bhough committed Jul 12, 2017
1 parent 7343037 commit 4780613
Show file tree
Hide file tree
Showing 44 changed files with 1,213 additions and 1,171 deletions.
576 changes: 293 additions & 283 deletions docs/assets/polished.js

Large diffs are not rendered by default.

334 changes: 166 additions & 168 deletions docs/docs/index.html

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions src/helpers/directionalProperty.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// @flow
const positionMap = ['top', 'right', 'bottom', 'left']
import capitalizeString from '../internalHelpers/_capitalizeString'

const positionMap = ['Top', 'Right', 'Bottom', 'Left']

function generateProperty(property: string, position: string) {
if (!property) return position
const splitPropertyName = property.split('-')
splitPropertyName.splice(1, 0, position)
return splitPropertyName.join('-')
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>) {
Expand All @@ -19,7 +25,7 @@ function generateStyles(property: string, valuesWithDefaults: Array<?string>) {
}

/**
* The directional property helper enables shorthand for direction based properties. It accepts a property and up to four values that map to top, right, bottom, and left, respectively. You can optionally pass an empty string to get only the directional values as properties. You can optionally pass a null argument for a directional value to ignore it.
* A helper that enables shorthand for direction based properties. It accepts a property (hyphenated or camelCased) and up to four values that map to top, right, bottom, and left, respectively. You can optionally pass an empty string to get only the directional values as properties. You can also optionally pass a null argument for a directional value to ignore it.
* @example
* // Styles as object usage
* const styles = {
Expand All @@ -34,10 +40,10 @@ function generateStyles(property: string, valuesWithDefaults: Array<?string>) {
* // CSS as JS Output
*
* div {
* 'padding-top': '12px',
* 'padding-right': '24px',
* 'padding-bottom': '36px',
* 'padding-left': '48px'
* 'paddingTop': '12px',
* 'paddingRight': '24px',
* 'paddingBottom': '36px',
* 'paddingLeft': '48px'
* }
*/

Expand Down
6 changes: 3 additions & 3 deletions src/helpers/modularScale.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ type Ratio =
* // Styles as object usage
* const styles = {
* // Increment two steps up the default scale
* 'font-size': modularScale(2)
* 'fontSize': modularScale(2)
* }
*
* // styled-components usage
* const div = styled.div`
* // Increment two steps up the default scale
* font-size: ${modularScale(2)}
* fontSize: ${modularScale(2)}
* `
*
* // CSS in JS Output
*
* element {
* 'font-size': '1.77689em'
* 'fontSize': '1.77689em'
* }
*/
function modularScale(steps: number, base?: number|string = '1em', ratio?: Ratio = 'perfectFourth') {
Expand Down
97 changes: 53 additions & 44 deletions src/helpers/test/__snapshots__/directionalProperty.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
exports[`directionalProperty properly applies a value when passed only one 1`] = `
Object {
"border-bottom": "12px",
"border-left": "12px",
"border-right": "12px",
"border-top": "12px",
"borderBottom": "12px",
"borderLeft": "12px",
"borderRight": "12px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly applies values when passed four 1`] = `
Object {
"border-bottom": "36px",
"border-left": "48px",
"border-right": "24px",
"border-top": "12px",
"borderBottom": "36px",
"borderLeft": "48px",
"borderRight": "24px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly applies values when passed three 1`] = `
Object {
"border-bottom": "36px",
"border-left": "24px",
"border-right": "24px",
"border-top": "12px",
"borderBottom": "36px",
"borderLeft": "24px",
"borderRight": "24px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly applies values when passed two 1`] = `
Object {
"border-bottom": "12px",
"border-left": "24px",
"border-right": "24px",
"border-top": "12px",
"borderBottom": "12px",
"borderLeft": "24px",
"borderRight": "24px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly generates properties when passed a camelCased property 1`] = `
Object {
"borderBottomWidth": "12px",
"borderLeftWidth": "12px",
"borderRightWidth": "12px",
"borderTopWidth": "12px",
}
`;

exports[`directionalProperty properly generates properties when passed a hyphenated property 1`] = `
Object {
"border-bottom-width": "12px",
"border-left-width": "12px",
"border-right-width": "12px",
"border-top-width": "12px",
"borderBottomWidth": "12px",
"borderLeftWidth": "12px",
"borderRightWidth": "12px",
"borderTopWidth": "12px",
}
`;

Expand All @@ -54,69 +63,69 @@ Object {

exports[`directionalProperty properly skips bottom property when last value is null 1`] = `
Object {
"border-left": "24px",
"border-right": "24px",
"border-top": "12px",
"borderLeft": "24px",
"borderRight": "24px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly skips bottom property when third value is null 1`] = `
Object {
"border-left": "48px",
"border-right": "24px",
"border-top": "12px",
"borderLeft": "48px",
"borderRight": "24px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly skips left and right properties when second value is null 1`] = `
Object {
"border-bottom": "12px",
"border-top": "12px",
"borderBottom": "12px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly skips left property when fourth value is null 1`] = `
Object {
"border-bottom": "36px",
"border-right": "24px",
"border-top": "12px",
"borderBottom": "36px",
"borderRight": "24px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly skips right and left properties when second value is null 1`] = `
Object {
"border-bottom": "36px",
"border-top": "12px",
"borderBottom": "36px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly skips right property when second value is null 1`] = `
Object {
"border-bottom": "36px",
"border-left": "48px",
"border-top": "12px",
"borderBottom": "36px",
"borderLeft": "48px",
"borderTop": "12px",
}
`;

exports[`directionalProperty properly skips top and bottom properties when first value is null 1`] = `
Object {
"border-left": "12px",
"border-right": "12px",
"borderLeft": "12px",
"borderRight": "12px",
}
`;

exports[`directionalProperty properly skips top property when first value is null 1`] = `
Object {
"border-bottom": "36px",
"border-left": "24px",
"border-right": "24px",
"borderBottom": "36px",
"borderLeft": "24px",
"borderRight": "24px",
}
`;

exports[`directionalProperty properly skips top property when first value is null 2`] = `
Object {
"border-bottom": "36px",
"border-left": "48px",
"border-right": "24px",
"borderBottom": "36px",
"borderLeft": "48px",
"borderRight": "24px",
}
`;
5 changes: 5 additions & 0 deletions src/helpers/test/directionalProperty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ describe('directionalProperty', () => {
it('properly generates properties when passed a hyphenated property', () => {
expect(directionalProperty('border-width', '12px')).toMatchSnapshot()
})

it('properly generates properties when passed a camelCased property', () => {
expect(directionalProperty('borderWidth', '12px')).toMatchSnapshot()
})

it('properly passes just the position when not given a property', () => {
expect(directionalProperty('', '12px')).toMatchSnapshot()
})
Expand Down
8 changes: 8 additions & 0 deletions src/internalHelpers/_capitalizeString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @flow

// @private
function capitalizeString(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}

export default capitalizeString
11 changes: 11 additions & 0 deletions src/internalHelpers/test/_capitalizeString.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @flow
import capitalizeString from '../_capitalizeString'

describe('capitalizeString', () => {
it('capitalizes a string', () => {
expect(capitalizeString('polished')).toEqual('Polished')
})
it('returns a capitalized string untouched', () => {
expect(capitalizeString('Polished')).toEqual('Polished')
})
})
16 changes: 8 additions & 8 deletions src/mixins/ellipsis.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
*
* div: {
* 'display': 'inline-block',
* 'max-width': '250px',
* 'maxWidth': '250px',
* 'overflow': 'hidden',
* 'text-overflow': 'ellipsis',
* 'white-space': 'nowrap',
* 'word-wrap': 'normal'
* 'textOverflow': 'ellipsis',
* 'whiteSpace': 'nowrap',
* 'wordWrap': 'normal'
* }
*/

function ellipsis(width: string = '100%') {
return {
'display': 'inline-block',
'max-width': width,
'maxWidth': width,
'overflow': 'hidden',
'text-overflow': 'ellipsis',
'white-space': 'nowrap',
'word-wrap': 'normal',
'textOverflow': 'ellipsis',
'whiteSpace': 'nowrap',
'wordWrap': 'normal',
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/mixins/fontFace.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function generateSources(fontFilePath?: string, localFonts?: Array<string>, file
* // CSS as JS Output
*
* '@font-face': {
* 'font-family': 'Sans-Pro',
* 'fontFamily': 'Sans-Pro',
* 'src': 'url("path/to/file.eot"), url("path/to/file.woff2"), url("path/to/file.woff"), url("path/to/file.ttf"), url("path/to/file.svg")',
* }
*/
Expand All @@ -77,13 +77,13 @@ function fontFace({

const fontFaceDeclaration = {
'@font-face': {
'font-family': fontFamily,
'src': generateSources(fontFilePath, localFonts, fileFormats),
'unicode-range': unicodeRange,
'font-stretch': fontStretch,
'font-style': fontStyle,
'font-variant': fontVariant,
'font-weight': fontWeight,
fontFamily,
src: generateSources(fontFilePath, localFonts, fileFormats),
unicodeRange,
fontStretch,
fontStyle,
fontVariant,
fontWeight,
},
}

Expand Down
16 changes: 8 additions & 8 deletions src/mixins/hideText.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
* @example
* // Styles as object usage
* const styles = {
* 'background-image': 'url(logo.png)',
* 'backgroundImage': 'url(logo.png)',
* ...hideText(),
* }
*
* // styled-components usage
* const div = styled.div`
* background-image: url(logo.png);
* backgroundImage: url(logo.png);
* ${hideText()};
* `
*
* // CSS as JS Output
*
* 'div': {
* 'background-image': 'url(logo.png)',
* 'text-indent': '101%',
* 'backgroundImage': 'url(logo.png)',
* 'textIndent': '101%',
* 'overflow': 'hidden',
* 'white-space': 'nowrap',
* 'whiteSpace': 'nowrap',
* }
*/

function hideText() {
return {
'text-indent': '101%',
'overflow': 'hidden',
'white-space': 'nowrap',
textIndent: '101%',
overflow: 'hidden',
whiteSpace: 'nowrap',
}
}

Expand Down

0 comments on commit 4780613

Please sign in to comment.