UNPKG

6.54 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /**
8 * Based on https://github.com/jsstyles/css-vendor, but without having to
9 * convert between different cases all the time.
10 *
11 *
12 */
13
14exports.getPrefixedKeyframes = getPrefixedKeyframes;
15exports.getPrefixedStyle = getPrefixedStyle;
16
17var _createPrefixer = require('inline-style-prefixer/static/createPrefixer');
18
19var _createPrefixer2 = _interopRequireDefault(_createPrefixer);
20
21var _createPrefixer3 = require('inline-style-prefixer/dynamic/createPrefixer');
22
23var _createPrefixer4 = _interopRequireDefault(_createPrefixer3);
24
25var _exenv = require('exenv');
26
27var _exenv2 = _interopRequireDefault(_exenv);
28
29var _static = require('./prefix-data/static');
30
31var _static2 = _interopRequireDefault(_static);
32
33var _dynamic = require('./prefix-data/dynamic');
34
35var _dynamic2 = _interopRequireDefault(_dynamic);
36
37var _camelCasePropsToDashCase = require('./camel-case-props-to-dash-case');
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
41var prefixAll = (0, _createPrefixer2.default)(_static2.default);
42var InlineStylePrefixer = (0, _createPrefixer4.default)(_dynamic2.default, prefixAll);
43
44function transformValues(style) {
45 return Object.keys(style).reduce(function (newStyle, key) {
46 var value = style[key];
47 if (Array.isArray(value)) {
48 value = value.join(';' + key + ':');
49 } else if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && typeof value.toString === 'function') {
50 value = value.toString();
51 }
52
53 newStyle[key] = value;
54 return newStyle;
55 }, {});
56}
57
58// Flatten prefixed values that are arrays to strings.
59//
60// We get prefixed styles back in the form of:
61// - `display: "flex"` OR
62// - `display: "-webkit-flex"` OR
63// - `display: [/* ... */, "-webkit-flex", "flex"]
64//
65// The last form is problematic for eventual use in the browser and server
66// render. More confusingly, we have to do **different** things on the
67// browser and server (noted inline below).
68//
69// https://github.com/FormidableLabs/radium/issues/958
70function flattenStyleValues(style) {
71 return Object.keys(style).reduce(function (newStyle, key) {
72 var val = style[key];
73 if (Array.isArray(val)) {
74 if (_exenv2.default.canUseDOM) {
75 // For the **browser**, when faced with multiple values, we just take
76 // the **last** one, which is the original passed in value before
77 // prefixing. This _should_ work, because `inline-style-prefixer`
78 // we're just passing through what would happen without ISP.
79
80 val = val[val.length - 1].toString();
81 } else {
82 // For the **server**, we just concatenate things together and convert
83 // the style object values into a hacked-up string of like `display:
84 // "-webkit-flex;display:flex"` that will SSR render correctly to like
85 // `"display:-webkit-flex;display:flex"` but would otherwise be
86 // totally invalid values.
87
88 // We convert keys to dash-case only for the serialize values and
89 // leave the real key camel-cased so it's as expected to React and
90 // other parts of the processing chain.
91 val = val.join(';' + (0, _camelCasePropsToDashCase.camelCaseToDashCase)(key) + ':');
92 }
93 }
94
95 newStyle[key] = val;
96 return newStyle;
97 }, {});
98}
99
100var _hasWarnedAboutUserAgent = false;
101var _lastUserAgent = void 0;
102var _cachedPrefixer = void 0;
103
104function getPrefixer(userAgent) {
105 var actualUserAgent = userAgent || global && global.navigator && global.navigator.userAgent;
106
107 if (process.env.NODE_ENV !== 'production') {
108 if (!actualUserAgent && !_hasWarnedAboutUserAgent) {
109 /* eslint-disable no-console */
110 console.warn('Radium: userAgent should be supplied for server-side rendering. See ' + 'https://github.com/FormidableLabs/radium/tree/master/docs/api#radium ' + 'for more information.');
111 /* eslint-enable no-console */
112 _hasWarnedAboutUserAgent = true;
113 }
114 }
115
116 if (process.env.NODE_ENV === 'test' || !_cachedPrefixer || actualUserAgent !== _lastUserAgent) {
117 if (actualUserAgent === 'all') {
118 _cachedPrefixer = {
119 prefix: prefixAll,
120 prefixedKeyframes: 'keyframes'
121 };
122 } else {
123 _cachedPrefixer = new InlineStylePrefixer({ userAgent: actualUserAgent });
124 }
125 _lastUserAgent = actualUserAgent;
126 }
127
128 return _cachedPrefixer;
129}
130
131function getPrefixedKeyframes(userAgent) {
132 return getPrefixer(userAgent).prefixedKeyframes || 'keyframes';
133}
134
135// Returns a new style object with vendor prefixes added to property names and
136// values.
137function getPrefixedStyle(style, userAgent) {
138 var styleWithFallbacks = transformValues(style);
139 var prefixer = getPrefixer(userAgent);
140 var prefixedStyle = prefixer.prefix(styleWithFallbacks);
141 var flattenedStyle = flattenStyleValues(prefixedStyle);
142 return flattenedStyle;
143}
\No newline at end of file