VSkeletonLoader.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { createVNode as _createVNode } from "vue";
  2. // Styles
  3. import "./VSkeletonLoader.css";
  4. // Composables
  5. import { useBackgroundColor } from "../../composables/color.mjs";
  6. import { makeDimensionProps, useDimension } from "../../composables/dimensions.mjs";
  7. import { makeElevationProps, useElevation } from "../../composables/elevation.mjs";
  8. import { useLocale } from "../../composables/locale.mjs";
  9. import { makeThemeProps, provideTheme } from "../../composables/theme.mjs"; // Utilities
  10. import { computed, toRef } from 'vue';
  11. import { genericComponent, propsFactory, useRender, wrapInArray } from "../../util/index.mjs"; // Types
  12. export const rootTypes = {
  13. actions: 'button@2',
  14. article: 'heading, paragraph',
  15. avatar: 'avatar',
  16. button: 'button',
  17. card: 'image, heading',
  18. 'card-avatar': 'image, list-item-avatar',
  19. chip: 'chip',
  20. 'date-picker': 'list-item, heading, divider, date-picker-options, date-picker-days, actions',
  21. 'date-picker-options': 'text, avatar@2',
  22. 'date-picker-days': 'avatar@28',
  23. divider: 'divider',
  24. heading: 'heading',
  25. image: 'image',
  26. 'list-item': 'text',
  27. 'list-item-avatar': 'avatar, text',
  28. 'list-item-two-line': 'sentences',
  29. 'list-item-avatar-two-line': 'avatar, sentences',
  30. 'list-item-three-line': 'paragraph',
  31. 'list-item-avatar-three-line': 'avatar, paragraph',
  32. paragraph: 'text@3',
  33. sentences: 'text@2',
  34. subtitle: 'text',
  35. table: 'table-heading, table-thead, table-tbody, table-tfoot',
  36. 'table-heading': 'chip, text',
  37. 'table-thead': 'heading@6',
  38. 'table-tbody': 'table-row-divider@6',
  39. 'table-row-divider': 'table-row, divider',
  40. 'table-row': 'text@6',
  41. 'table-tfoot': 'text@2, avatar@2',
  42. text: 'text'
  43. };
  44. function genBone(type) {
  45. let children = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  46. return _createVNode("div", {
  47. "class": ['v-skeleton-loader__bone', `v-skeleton-loader__${type}`]
  48. }, [children]);
  49. }
  50. function genBones(bone) {
  51. // e.g. 'text@3'
  52. const [type, length] = bone.split('@');
  53. // Generate a length array based upon
  54. // value after @ in the bone string
  55. return Array.from({
  56. length
  57. }).map(() => genStructure(type));
  58. }
  59. function genStructure(type) {
  60. let children = [];
  61. if (!type) return children;
  62. // TODO: figure out a better way to type this
  63. const bone = rootTypes[type];
  64. // End of recursion, do nothing
  65. /* eslint-disable-next-line no-empty, brace-style */
  66. if (type === bone) {}
  67. // Array of values - e.g. 'heading, paragraph, text@2'
  68. else if (type.includes(',')) return mapBones(type);
  69. // Array of values - e.g. 'paragraph@4'
  70. else if (type.includes('@')) return genBones(type);
  71. // Array of values - e.g. 'card@2'
  72. else if (bone.includes(',')) children = mapBones(bone);
  73. // Array of values - e.g. 'list-item@2'
  74. else if (bone.includes('@')) children = genBones(bone);
  75. // Single value - e.g. 'card-heading'
  76. else if (bone) children.push(genStructure(bone));
  77. return [genBone(type, children)];
  78. }
  79. function mapBones(bones) {
  80. // Remove spaces and return array of structures
  81. return bones.replace(/\s/g, '').split(',').map(genStructure);
  82. }
  83. export const makeVSkeletonLoaderProps = propsFactory({
  84. boilerplate: Boolean,
  85. color: String,
  86. loading: Boolean,
  87. loadingText: {
  88. type: String,
  89. default: '$vuetify.loading'
  90. },
  91. type: {
  92. type: [String, Array],
  93. default: 'image'
  94. },
  95. ...makeDimensionProps(),
  96. ...makeElevationProps(),
  97. ...makeThemeProps()
  98. }, 'VSkeletonLoader');
  99. export const VSkeletonLoader = genericComponent()({
  100. name: 'VSkeletonLoader',
  101. props: makeVSkeletonLoaderProps(),
  102. setup(props, _ref) {
  103. let {
  104. slots
  105. } = _ref;
  106. const {
  107. backgroundColorClasses,
  108. backgroundColorStyles
  109. } = useBackgroundColor(toRef(props, 'color'));
  110. const {
  111. dimensionStyles
  112. } = useDimension(props);
  113. const {
  114. elevationClasses
  115. } = useElevation(props);
  116. const {
  117. themeClasses
  118. } = provideTheme(props);
  119. const {
  120. t
  121. } = useLocale();
  122. const items = computed(() => genStructure(wrapInArray(props.type).join(',')));
  123. useRender(() => {
  124. const isLoading = !slots.default || props.loading;
  125. return _createVNode("div", {
  126. "class": ['v-skeleton-loader', {
  127. 'v-skeleton-loader--boilerplate': props.boilerplate
  128. }, themeClasses.value, backgroundColorClasses.value, elevationClasses.value],
  129. "style": [backgroundColorStyles.value, isLoading ? dimensionStyles.value : {}],
  130. "aria-busy": !props.boilerplate ? isLoading : undefined,
  131. "aria-live": !props.boilerplate ? 'polite' : undefined,
  132. "aria-label": !props.boilerplate ? t(props.loadingText) : undefined,
  133. "role": !props.boilerplate ? 'alert' : undefined
  134. }, [isLoading ? items.value : slots.default?.()]);
  135. });
  136. return {};
  137. }
  138. });
  139. //# sourceMappingURL=VSkeletonLoader.mjs.map