UNPKG

fela-plugin-theme-value

Version:

Fela plugin to resolve values from a theme

37 lines (30 loc) 1.17 kB
import isPlainObject from 'isobject'; import { arrayReduce } from 'fast-loops'; function getThemeValue(object, key) { var value = arrayReduce(key.split('.'), function (value, index) { if (isPlainObject(value) && value[index]) { return value[index]; } return undefined; }, object); return value || key; } function resolveThemeValues(style) { var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var mapping = arguments.length > 2 ? arguments[2] : undefined; for (var property in style) { var value = style[property]; if (typeof value === 'string' && mapping[property]) { style[property] = getThemeValue(mapping[property](theme), value); } else if (isPlainObject(value)) { style[property] = resolveThemeValues(style[property], theme, mapping); } } return style; } export default function themeValue() { var mapping = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return function themeValuePlugin(style, type, renderer, props) { return resolveThemeValues(style, props === null || props === void 0 ? void 0 : props.theme, mapping); }; }