|
| 1 | +import { ref, watch } from '@vue/composition-api'; |
| 2 | + |
| 3 | +import useGridLayout from '../cards/useGridLayout'; |
| 4 | +import { getBreakpointConfig } from '../cards/utils'; |
| 5 | + |
| 6 | +const DEFAULT_SKELETON = { |
| 7 | + count: undefined, // default determined by the grid layout and the current breakpoint |
| 8 | + height: '200px', |
| 9 | + orientation: 'horizontal', |
| 10 | + thumbnailDisplay: 'none', |
| 11 | + thumbnailAlign: 'left', |
| 12 | +}; |
| 13 | + |
| 14 | +export default function useGridLoading(skeletonsConfig, layout, layoutOverride) { |
| 15 | + const { currentBreakpointConfig, windowBreakpoint } = useGridLayout(layout, layoutOverride); |
| 16 | + |
| 17 | + const skeletonCount = ref(DEFAULT_SKELETON.count); |
| 18 | + const skeletonHeight = ref(DEFAULT_SKELETON.height); |
| 19 | + const skeletonOrientation = ref(DEFAULT_SKELETON.orientation); |
| 20 | + const skeletonThumbnailDisplay = ref(DEFAULT_SKELETON.thumbnailDisplay); |
| 21 | + const skeletonThumbnailAlign = ref(DEFAULT_SKELETON.thumbnailAlign); |
| 22 | + |
| 23 | + // Updates the loading skeleton configuration |
| 24 | + //for the current breakpoint |
| 25 | + watch( |
| 26 | + [windowBreakpoint, skeletonsConfig, currentBreakpointConfig], |
| 27 | + ([newBreakpoint]) => { |
| 28 | + skeletonCount.value = currentBreakpointConfig.value.cardsPerRow; |
| 29 | + |
| 30 | + const breakpointSkeletonConfig = getBreakpointConfig(skeletonsConfig.value, newBreakpoint); |
| 31 | + if (breakpointSkeletonConfig) { |
| 32 | + if (breakpointSkeletonConfig.count) { |
| 33 | + skeletonCount.value = breakpointSkeletonConfig.count; |
| 34 | + } |
| 35 | + if (breakpointSkeletonConfig.height) { |
| 36 | + skeletonHeight.value = breakpointSkeletonConfig.height; |
| 37 | + } |
| 38 | + if (breakpointSkeletonConfig.orientation) { |
| 39 | + skeletonOrientation.value = breakpointSkeletonConfig.orientation; |
| 40 | + } |
| 41 | + if (breakpointSkeletonConfig.thumbnailDisplay) { |
| 42 | + skeletonThumbnailDisplay.value = breakpointSkeletonConfig.thumbnailDisplay; |
| 43 | + } |
| 44 | + if (breakpointSkeletonConfig.thumbnailAlign) { |
| 45 | + skeletonThumbnailAlign.value = breakpointSkeletonConfig.thumbnailAlign; |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + { immediate: true, deep: true } |
| 50 | + ); |
| 51 | + |
| 52 | + return { |
| 53 | + skeletonCount, |
| 54 | + skeletonHeight, |
| 55 | + skeletonOrientation, |
| 56 | + skeletonThumbnailDisplay, |
| 57 | + skeletonThumbnailAlign, |
| 58 | + }; |
| 59 | +} |
0 commit comments