Home Reference Source

src/mixins/position/index.js

  1. /* leny/koutla-swiss
  2. *
  3. * ~/mixins/position/index.js - Position mixins
  4. *
  5. * coded by leny@flatLand!
  6. * started at 13/01/2018
  7. */
  8.  
  9. export const POSITION_STATIC = "static";
  10. export const POSITION_RELATIVE = "relative";
  11. export const POSITION_ABSOLUTE = "absolute";
  12. export const POSITION_STICKY = "sticky";
  13. export const POSITION_FIXED = "fixed";
  14.  
  15. /**
  16. * Mixin to generate a positionned box
  17. * @abstract
  18. * @param {string} type position name
  19. * @param {...string} args values for top, right, bottom, left - treated like for margin or padding properties
  20. * @return {object} `{position: "...", top: "...", right: "...", bottom: "...", left: "..."}`
  21. */
  22. export const position = (type, ...args) => {
  23. if (
  24. ![
  25. POSITION_STATIC,
  26. POSITION_RELATIVE,
  27. POSITION_ABSOLUTE,
  28. POSITION_FIXED,
  29. POSITION_STICKY,
  30. ].includes(type)
  31. ) {
  32. throw new TypeError("Invalid position type");
  33. }
  34.  
  35. let oPosition = {
  36. position: type,
  37. },
  38. aPositionArgs;
  39.  
  40. if (!args.length) {
  41. return oPosition;
  42. }
  43.  
  44. // check object type, cf. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Description
  45. if (args[0] === Object(args[0])) {
  46. return {
  47. ...oPosition,
  48. ...args[0],
  49. };
  50. }
  51.  
  52. aPositionArgs = args.map(mValue => `${mValue}`);
  53.  
  54. switch (aPositionArgs.length) {
  55. case 0:
  56. return oPosition;
  57. case 1:
  58. return {
  59. ...oPosition,
  60. top: aPositionArgs[0],
  61. right: aPositionArgs[0],
  62. bottom: aPositionArgs[0],
  63. left: aPositionArgs[0],
  64. };
  65. case 2:
  66. return {
  67. ...oPosition,
  68. top: aPositionArgs[0],
  69. right: aPositionArgs[1],
  70. bottom: aPositionArgs[0],
  71. left: aPositionArgs[1],
  72. };
  73. case 3:
  74. return {
  75. ...oPosition,
  76. top: aPositionArgs[0],
  77. right: aPositionArgs[1],
  78. bottom: aPositionArgs[2],
  79. left: aPositionArgs[1],
  80. };
  81. case 4:
  82. return {
  83. ...oPosition,
  84. top: aPositionArgs[0],
  85. right: aPositionArgs[1],
  86. bottom: aPositionArgs[2],
  87. left: aPositionArgs[3],
  88. };
  89. default:
  90. throw new TypeError("Invalid argument amount");
  91. }
  92. };
  93.  
  94. /**
  95. * Mixin to generate a relative positionned box
  96. * @abstract
  97. * @param {...string} args values for top, right, bottom, left - treated like for margin or padding properties
  98. * @return {object} `{position: "relative", top: "...", right: "...", bottom: "...", left: "..."}`
  99. */
  100. export const relative = (...args) => position(POSITION_RELATIVE, ...args);
  101.  
  102. /**
  103. * Mixin to generate a absolute positionned box
  104. * @abstract
  105. * @param {...string} args values for top, right, bottom, left - treated like for margin or padding properties
  106. * @return {object} `{position: "absolute", top: "...", right: "...", bottom: "...", left: "..."}`
  107. */
  108. export const absolute = (...args) => position(POSITION_ABSOLUTE, ...args);
  109.  
  110. /**
  111. * Mixin to generate a fixed positionned box
  112. * @abstract
  113. * @param {...string} args values for top, right, bottom, left - treated like for margin or padding properties
  114. * @return {object} `{position: "fixed", top: "...", right: "...", bottom: "...", left: "..."}`
  115. */
  116. export const fixed = (...args) => position(POSITION_FIXED, ...args);
  117.  
  118. /**
  119. * Mixin to generate a sticky positionned box
  120. * @abstract
  121. * @param {...string} args values for top, right, bottom, left - treated like for margin or padding properties
  122. * @return {object} `{position: "sticky", top: "...", right: "...", bottom: "...", left: "..."}`
  123. */
  124. export const sticky = (...args) => position(POSITION_STICKY, ...args);