1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import createStaticPrefixer from 'inline-style-prefixer/static/createPrefixer';
|
9 | import createDynamicPrefixer
|
10 | from 'inline-style-prefixer/dynamic/createPrefixer';
|
11 | import ExecutionEnvironment from 'exenv';
|
12 |
|
13 | import staticData from './prefix-data/static';
|
14 | import dynamicData from './prefix-data/dynamic';
|
15 |
|
16 | import {camelCaseToDashCase} from './camel-case-props-to-dash-case';
|
17 |
|
18 | const prefixAll: (style: Object) => Object = createStaticPrefixer(staticData);
|
19 | const InlineStylePrefixer = createDynamicPrefixer(dynamicData, prefixAll);
|
20 |
|
21 | function transformValues(style) {
|
22 | return Object.keys(style).reduce(
|
23 | (newStyle, key) => {
|
24 | let value = style[key];
|
25 | if (Array.isArray(value)) {
|
26 | value = value.join(';' + key + ':');
|
27 | } else if (
|
28 | value &&
|
29 | typeof value === 'object' &&
|
30 | typeof value.toString === 'function'
|
31 | ) {
|
32 | value = value.toString();
|
33 | }
|
34 |
|
35 | newStyle[key] = value;
|
36 | return newStyle;
|
37 | },
|
38 | {}
|
39 | );
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function flattenStyleValues(style) {
|
55 | return Object.keys(style).reduce(
|
56 | (newStyle, key) => {
|
57 | let val = style[key];
|
58 | if (Array.isArray(val)) {
|
59 | if (ExecutionEnvironment.canUseDOM) {
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | val = val[val.length - 1].toString();
|
66 | } else {
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | val = val.join(`;${camelCaseToDashCase(key)}:`);
|
77 | }
|
78 | }
|
79 |
|
80 | newStyle[key] = val;
|
81 | return newStyle;
|
82 | },
|
83 | {}
|
84 | );
|
85 | }
|
86 |
|
87 | let _hasWarnedAboutUserAgent = false;
|
88 | let _lastUserAgent;
|
89 | let _cachedPrefixer;
|
90 |
|
91 | function getPrefixer(
|
92 | userAgent: ?string
|
93 | ): {
|
94 | +prefix: (style: Object) => Object,
|
95 | prefixedKeyframes: string
|
96 | } {
|
97 | const actualUserAgent = userAgent ||
|
98 | (global && global.navigator && global.navigator.userAgent);
|
99 |
|
100 | if (process.env.NODE_ENV !== 'production') {
|
101 | if (!actualUserAgent && !_hasWarnedAboutUserAgent) {
|
102 |
|
103 | console.warn(
|
104 | 'Radium: userAgent should be supplied for server-side rendering. See ' +
|
105 | 'https://github.com/FormidableLabs/radium/tree/master/docs/api#radium ' +
|
106 | 'for more information.'
|
107 | );
|
108 |
|
109 | _hasWarnedAboutUserAgent = true;
|
110 | }
|
111 | }
|
112 |
|
113 | if (
|
114 | process.env.NODE_ENV === 'test' ||
|
115 | (!_cachedPrefixer || actualUserAgent !== _lastUserAgent)
|
116 | ) {
|
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 |
|
131 | export function getPrefixedKeyframes(userAgent?: ?string): string {
|
132 | return getPrefixer(userAgent).prefixedKeyframes || 'keyframes';
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 | export function getPrefixedStyle(style: Object, userAgent?: ?string): Object {
|
138 | const styleWithFallbacks = transformValues(style);
|
139 | const prefixer = getPrefixer(userAgent);
|
140 | const prefixedStyle = prefixer.prefix(styleWithFallbacks);
|
141 | const flattenedStyle = flattenStyleValues(prefixedStyle);
|
142 | return flattenedStyle;
|
143 | }
|