@instructure/ui-themeable
Version:
A UI component library made by Instructure Inc.
83 lines (74 loc) • 3.54 kB
JavaScript
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import PropTypes from 'prop-types';
import { ThemeablePropValues } from "./ThemeablePropValues.js";
var SHADOW_TYPES = ThemeablePropValues.SHADOW_TYPES,
STACKING_TYPES = ThemeablePropValues.STACKING_TYPES,
BORDER_WIDTHS = ThemeablePropValues.BORDER_WIDTHS,
BORDER_RADII = ThemeablePropValues.BORDER_RADII,
BACKGROUNDS = ThemeablePropValues.BACKGROUNDS,
SIZES = ThemeablePropValues.SIZES,
SPACING = ThemeablePropValues.SPACING;
/**
* ---
* category: utilities/themes
* ---
* Custom prop types for themeable React components.
* @module ThemeablePropTypes
*/
var ThemeablePropTypes = {
shadow: PropTypes.oneOf(Object.values(SHADOW_TYPES)),
stacking: PropTypes.oneOf(Object.values(STACKING_TYPES)),
borderWidth: shorthandPropType(Object.values(BORDER_WIDTHS)),
borderRadius: shorthandPropType(Object.values(BORDER_RADII)),
background: PropTypes.oneOf(Object.values(BACKGROUNDS)),
size: PropTypes.oneOf(Object.values(SIZES)),
spacing: shorthandPropType(Object.values(SPACING))
};
function shorthandPropType(validValues) {
return function (props, propName, componentName, location) {
var propValue = props[propName];
if (typeof propValue === 'undefined') {
return;
}
var propValueType = typeof propValue;
if (propValueType !== 'string') {
return new Error("Invalid ".concat(location, " `").concat(propName, "` of type `").concat(propValueType, "` supplied to `").concat(componentName, "`, expected ") + "a string.");
}
var propValues = propValue.split(' ');
var valuesLength = propValues.length;
if (valuesLength > 0 && valuesLength < 5) {
for (var i = 0; i < valuesLength; i++) {
var valueIndex = validValues.indexOf(propValues[i]);
if (valueIndex === -1) {
return new Error("Invalid ".concat(location, " `").concat(propName, "` `").concat(propValues[i], "` supplied to `").concat(componentName, "`, expected ") + "a one of `".concat(validValues.join(', '), "`."));
}
}
} else {
return new Error("Invalid ".concat(location, " `").concat(propName, "` `").concat(propValue, "` supplied to `").concat(componentName, "`, expected ") + "between one and four of the following valid values: `".concat(validValues.join(', '), "`."));
}
};
}
export default ThemeablePropTypes;
export { ThemeablePropTypes, shorthandPropType };