60 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-01-29 17:10:49 -08:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
2015-01-29 17:10:49 -08:00
*
* @providesModule flattenStyle
2015-03-24 19:34:12 -07:00
* @flow
2015-01-29 17:10:49 -08:00
*/
'use strict';
var StyleSheetRegistry = require('StyleSheetRegistry');
2015-03-24 19:34:12 -07:00
var invariant = require('invariant');
2015-01-29 17:10:49 -08:00
2015-03-24 19:34:12 -07:00
type Atom = number | bool | Object | Array<?Atom>
type StyleObj = Atom | Array<?StyleObj>
2015-01-29 17:10:49 -08:00
function getStyle(style) {
if (typeof style === 'number') {
return StyleSheetRegistry.getStyleByID(style);
}
return style;
}
function flattenStyle(style: ?StyleObj): ?Object {
2015-01-29 17:10:49 -08:00
if (!style) {
return undefined;
}
2015-03-24 19:34:12 -07:00
invariant(style !== true, 'style may be false but not true');
2015-01-29 17:10:49 -08:00
if (!Array.isArray(style)) {
return getStyle(style);
}
var result = {};
for (var i = 0; i < style.length; ++i) {
var computedStyle = flattenStyle(style[i]);
if (computedStyle) {
for (var key in computedStyle) {
result[key] = computedStyle[key];
if (__DEV__) {
var value = computedStyle[key];
invariant(
!value || typeof value !== 'object' || !value.getValue,
'You passed an Animated.Value to a normal component. ' +
'You need to wrap that component in an Animated. For example, ' +
'replace <View /> by <Animated.View />.'
);
}
}
2015-01-29 17:10:49 -08:00
}
}
return result;
}
module.exports = flattenStyle;