-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements prepareStyles as composition of functions in muiTheme
Signed-off-by: Neil Gabbadon <neil.gabbadon@emikra.com>
- Loading branch information
Showing
7 changed files
with
124 additions
and
4 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
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
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
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,7 @@ | ||
export default function(muiTheme) { | ||
if (muiTheme.userAgent === false) return; | ||
|
||
return (style) => { | ||
return muiTheme.prefix(style); | ||
}; | ||
} |
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,15 @@ | ||
import warning from 'warning'; | ||
|
||
const CALLED_ONCE = 'muiPrepared'; | ||
|
||
export default function callOnce() { | ||
if (process.env.NODE_ENV === 'production') return; | ||
|
||
return (style) => { | ||
if (style[CALLED_ONCE]) { | ||
warning(false, 'You cannot call prepareStyles() on the same style object more than once.'); | ||
} | ||
style[CALLED_ONCE] = true; | ||
return style; | ||
}; | ||
} |
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,9 @@ | ||
import autoprefixer from './autoprefixer'; | ||
import callOnce from './callOnce'; | ||
import rtl from './rtl'; | ||
|
||
export { | ||
autoprefixer, | ||
callOnce, | ||
rtl, | ||
}; |
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,81 @@ | ||
const reTranslate = /((^|\s)translate(3d|X)?\()(\-?[\d]+)/; | ||
const reSkew = /((^|\s)skew(x|y)?\()\s*(\-?[\d]+)(deg|rad|grad)(,\s*(\-?[\d]+)(deg|rad|grad))?/; | ||
|
||
/** | ||
* This function ensures that `style` supports both ltr and rtl directions by | ||
* checking `styleConstants` in `muiTheme` and replacing attribute keys if | ||
* necessary. | ||
*/ | ||
export default function rtl(muiTheme) { | ||
if (!muiTheme.isRtl) return; | ||
|
||
return (style) => { | ||
const flippedAttributes = { | ||
// Keys and their replacements. | ||
right: 'left', | ||
left: 'right', | ||
marginRight: 'marginLeft', | ||
marginLeft: 'marginRight', | ||
paddingRight: 'paddingLeft', | ||
paddingLeft: 'paddingRight', | ||
borderRight: 'borderLeft', | ||
borderLeft: 'borderRight', | ||
}; | ||
|
||
let newStyle = {}; | ||
|
||
Object.keys(style).forEach(function(attribute) { | ||
let value = style[attribute]; | ||
let key = attribute; | ||
|
||
if (flippedAttributes.hasOwnProperty(attribute)) { | ||
key = flippedAttributes[attribute]; | ||
} | ||
|
||
switch (attribute) { | ||
case 'float': | ||
case 'textAlign': | ||
if (value === 'right') { | ||
value = 'left'; | ||
} else if (value === 'left') { | ||
value = 'right'; | ||
} | ||
break; | ||
|
||
case 'direction': | ||
if (value === 'ltr') { | ||
value = 'rtl'; | ||
} else if (value === 'rtl') { | ||
value = 'ltr'; | ||
} | ||
break; | ||
|
||
case 'transform': | ||
let matches; | ||
if ((matches = value.match(reTranslate))) { | ||
value = value.replace(matches[0], matches[1] + (-parseFloat(matches[4])) ); | ||
} | ||
if ((matches = value.match(reSkew))) { | ||
value = value.replace(matches[0], matches[1] + (-parseFloat(matches[4])) + matches[5] + | ||
matches[6] ? ',' + (-parseFloat(matches[7])) + matches[8] : '' | ||
); | ||
} | ||
break; | ||
|
||
case 'transformOrigin': | ||
if (value.indexOf('right') > -1) { | ||
value = value.replace('right', 'left'); | ||
} else if (value.indexOf('left') > -1) { | ||
value = value.replace('left', 'right'); | ||
} | ||
break; | ||
} | ||
|
||
newStyle[key] = value; | ||
}); | ||
|
||
return newStyle; | ||
}; | ||
} | ||
|
||
|