position.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = position;
  4. var _directionalProperty = _interopRequireDefault(require("../helpers/directionalProperty"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  6. function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  7. var positionMap = ['absolute', 'fixed', 'relative', 'static', 'sticky'];
  8. /**
  9. * Shorthand accepts up to five values, including null to skip a value, and maps them to their respective directions. The first value can optionally be a position keyword.
  10. * @example
  11. * // Styles as object usage
  12. * const styles = {
  13. * ...position('12px', '24px', '36px', '48px')
  14. * }
  15. *
  16. * // styled-components usage
  17. * const div = styled.div`
  18. * ${position('12px', '24px', '36px', '48px')}
  19. * `
  20. *
  21. * // CSS as JS Output
  22. *
  23. * div {
  24. * 'top': '12px',
  25. * 'right': '24px',
  26. * 'bottom': '36px',
  27. * 'left': '48px'
  28. * }
  29. *
  30. * // Styles as object usage
  31. * const styles = {
  32. * ...position('absolute', '12px', '24px', '36px', '48px')
  33. * }
  34. *
  35. * // styled-components usage
  36. * const div = styled.div`
  37. * ${position('absolute', '12px', '24px', '36px', '48px')}
  38. * `
  39. *
  40. * // CSS as JS Output
  41. *
  42. * div {
  43. * 'position': 'absolute',
  44. * 'top': '12px',
  45. * 'right': '24px',
  46. * 'bottom': '36px',
  47. * 'left': '48px'
  48. * }
  49. */
  50. function position(firstValue) {
  51. for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  52. values[_key - 1] = arguments[_key];
  53. }
  54. if (positionMap.indexOf(firstValue) >= 0 && firstValue) {
  55. return _extends({}, _directionalProperty["default"].apply(void 0, [''].concat(values)), {
  56. position: firstValue
  57. });
  58. } else {
  59. return _directionalProperty["default"].apply(void 0, ['', firstValue].concat(values));
  60. }
  61. }
  62. module.exports = exports.default;