@elastic/eui
Version:
Elastic UI Component Library
157 lines (146 loc) • 6.38 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { keysOf } from '../../components/common';
import LOGICALS from './logicals.json';
/**
* EUI utilizes logical CSS properties to enable directional writing-modes.
* To encourage use of logical properties, we provide a few helper utilities to
* convert certain directional properties to logical properties.
* https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties
*/
export var logicalSide = {
left: 'inline-start',
right: 'inline-end',
top: 'block-start',
bottom: 'block-end',
horizontal: 'inline',
vertical: 'block'
};
export var LOGICAL_SIDES = keysOf(logicalSide);
export var logicals = LOGICALS;
var _shorthands = LOGICALS._shorthands,
_logicals = _objectWithoutProperties(LOGICALS, ["_shorthands"]);
export var LOGICAL_PROPERTIES = keysOf(_logicals);
/**
*
* @param property A string that is a valid CSS logical property
* @param value String to output as the property value
* @returns `string` Returns the logical CSS property version for the given `property: value` pair
*/
export var logicalCSS = function logicalCSS(property, value) {
return "".concat(logicals[property], ": ").concat(value, ";");
};
/**
* Some logical properties are not yet fully supported by all browsers.
* For those cases, we should use the old property as a fallback for
* browsers missing support, while allowing supporting browsers to use
* the logical properties.
*
* Examples:
* https://caniuse.com/?search=overflow-block
* https://caniuse.com/mdn-css_properties_float_flow_relative_values
*/
export var logicalCSSWithFallback = function logicalCSSWithFallback(property, value) {
return "\n ".concat(property, ": ").concat(value, ";\n ").concat(logicalCSS(property, value), "\n");
};
/**
* Casing utils for swapping between camel case (style objs) and kebab case (CSS)
*/
var camelCase = function camelCase(kebabCasedString) {
return kebabCasedString.replace(/-\w/g, function (str) {
return str.charAt(1).toUpperCase();
});
};
var kebabCase = function kebabCase(camelCasedString) {
return camelCasedString.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
};
/**
*
* @param property A string that is a valid CSS logical property
* @param value String to output as the property value
* @returns `object` Returns the logical CSS property version for the given `property: value` pair
*/
export var logicalStyle = function logicalStyle(property, value) {
return _defineProperty({}, camelCase(logicals[property]), value);
};
/**
* Given a style object with any amount of unknown CSS properties,
* find ones that can be converted to logical properties and convert them
*
* @param styleObject - A React object of camelCased styles
* @returns `object`
*/
export var logicalStyles = function logicalStyles(styleObject) {
var logicalStyleObject = {};
Object.entries(styleObject).forEach(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 2),
key = _ref3[0],
value = _ref3[1];
var cssProperty = kebabCase(key);
if (logicals.hasOwnProperty(cssProperty)) {
var logicalKey = camelCase(logicals[cssProperty]);
logicalStyleObject[logicalKey] = value;
} else {
logicalStyleObject[key] = value;
}
});
return logicalStyleObject;
};
/**
*
* @param width A string value for the LTR width
* @param height A string value for the LTR height
* @returns `string` Returns the logical CSS properties for height and width
*/
export var logicalSizeCSS = function logicalSizeCSS(width) {
var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : width;
return "\n ".concat(logicals.width, ": ").concat(width, ";\n ").concat(logicals.height, ": ").concat(height, ";\n ");
};
/**
*
* @param width A string value for the LTR width
* @param height A string value for the LTR height
* @returns `object` Returns the logical CSS properties for height and width
*/
export var logicalSizeStyle = function logicalSizeStyle(width, height) {
return _objectSpread(_objectSpread({}, logicalStyle('width', width)), logicalStyle('height', height));
};
// Text alignment is separate because its the value that changes not the property
export var logicalText = {
'text-align': {
left: 'start',
center: 'center',
right: 'end'
}
};
export var LOGICAL_TEXT_ALIGNMENT = keysOf(logicalText['text-align']);
/**
*
* @param property A string that is a valid CSS logical property
* @param value String to output as the property value
* @returns `string` Returns the logical CSS property version for the given `property: value` pair
*/
export var logicalTextAlignCSS = function logicalTextAlignCSS(value) {
return "text-align: ".concat(logicalText['text-align'][value], ";");
};
/**
*
* @param property A string that is a valid CSS logical property
* @param value String to output as the property value
* @returns `object` Returns the logical CSS property version for the given `property: value` pair
*/
export var logicalTextAlignStyle = function logicalTextAlignStyle(value) {
return {
textAlign: logicalText['text-align'][value]
};
};