VRow.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Styles
  2. import "./VGrid.css";
  3. // Composables
  4. import { makeComponentProps } from "../../composables/component.mjs";
  5. import { breakpoints } from "../../composables/display.mjs";
  6. import { makeTagProps } from "../../composables/tag.mjs"; // Utilities
  7. import { capitalize, computed, h } from 'vue';
  8. import { genericComponent, propsFactory } from "../../util/index.mjs"; // Types
  9. const ALIGNMENT = ['start', 'end', 'center'];
  10. const SPACE = ['space-between', 'space-around', 'space-evenly'];
  11. function makeRowProps(prefix, def) {
  12. return breakpoints.reduce((props, val) => {
  13. const prefixKey = prefix + capitalize(val);
  14. props[prefixKey] = def();
  15. return props;
  16. }, {});
  17. }
  18. const ALIGN_VALUES = [...ALIGNMENT, 'baseline', 'stretch'];
  19. const alignValidator = str => ALIGN_VALUES.includes(str);
  20. const alignProps = makeRowProps('align', () => ({
  21. type: String,
  22. default: null,
  23. validator: alignValidator
  24. }));
  25. const JUSTIFY_VALUES = [...ALIGNMENT, ...SPACE];
  26. const justifyValidator = str => JUSTIFY_VALUES.includes(str);
  27. const justifyProps = makeRowProps('justify', () => ({
  28. type: String,
  29. default: null,
  30. validator: justifyValidator
  31. }));
  32. const ALIGN_CONTENT_VALUES = [...ALIGNMENT, ...SPACE, 'stretch'];
  33. const alignContentValidator = str => ALIGN_CONTENT_VALUES.includes(str);
  34. const alignContentProps = makeRowProps('alignContent', () => ({
  35. type: String,
  36. default: null,
  37. validator: alignContentValidator
  38. }));
  39. const propMap = {
  40. align: Object.keys(alignProps),
  41. justify: Object.keys(justifyProps),
  42. alignContent: Object.keys(alignContentProps)
  43. };
  44. const classMap = {
  45. align: 'align',
  46. justify: 'justify',
  47. alignContent: 'align-content'
  48. };
  49. function breakpointClass(type, prop, val) {
  50. let className = classMap[type];
  51. if (val == null) {
  52. return undefined;
  53. }
  54. if (prop) {
  55. // alignSm -> Sm
  56. const breakpoint = prop.replace(type, '');
  57. className += `-${breakpoint}`;
  58. }
  59. // .align-items-sm-center
  60. className += `-${val}`;
  61. return className.toLowerCase();
  62. }
  63. export const makeVRowProps = propsFactory({
  64. dense: Boolean,
  65. noGutters: Boolean,
  66. align: {
  67. type: String,
  68. default: null,
  69. validator: alignValidator
  70. },
  71. ...alignProps,
  72. justify: {
  73. type: String,
  74. default: null,
  75. validator: justifyValidator
  76. },
  77. ...justifyProps,
  78. alignContent: {
  79. type: String,
  80. default: null,
  81. validator: alignContentValidator
  82. },
  83. ...alignContentProps,
  84. ...makeComponentProps(),
  85. ...makeTagProps()
  86. }, 'VRow');
  87. export const VRow = genericComponent()({
  88. name: 'VRow',
  89. props: makeVRowProps(),
  90. setup(props, _ref) {
  91. let {
  92. slots
  93. } = _ref;
  94. const classes = computed(() => {
  95. const classList = [];
  96. // Loop through `align`, `justify`, `alignContent` breakpoint props
  97. let type;
  98. for (type in propMap) {
  99. propMap[type].forEach(prop => {
  100. const value = props[prop];
  101. const className = breakpointClass(type, prop, value);
  102. if (className) classList.push(className);
  103. });
  104. }
  105. classList.push({
  106. 'v-row--no-gutters': props.noGutters,
  107. 'v-row--dense': props.dense,
  108. [`align-${props.align}`]: props.align,
  109. [`justify-${props.justify}`]: props.justify,
  110. [`align-content-${props.alignContent}`]: props.alignContent
  111. });
  112. return classList;
  113. });
  114. return () => h(props.tag, {
  115. class: ['v-row', classes.value, props.class],
  116. style: props.style
  117. }, slots.default?.());
  118. }
  119. });
  120. //# sourceMappingURL=VRow.mjs.map