| 1 |
import { useState, useEffect } from '@wordpress/element' |
| 2 |
import { Transition } from '@headlessui/react' |
| 3 |
import classNames from 'classnames' |
| 4 |
import Color from 'color' |
| 5 |
import Skeleton, { SkeletonTheme } from 'react-loading-skeleton' |
| 6 |
import 'react-loading-skeleton/dist/skeleton.css' |
| 7 |
|
| 8 |
export const SkeletonLoader = ({ theme, context }) => { |
| 9 |
const [highlightColor, setHighlightColor] = useState() |
| 10 |
const color = theme?.color ?? '#000000' |
| 11 |
const bgColor = theme?.bgColor ?? '#cccccc' |
| 12 |
|
| 13 |
useEffect(() => { |
| 14 |
const id = requestAnimationFrame(() => { |
| 15 |
if (!theme?.color) return |
| 16 |
const hl = Color(color).isLight() |
| 17 |
? darkenBy(Color(color), 0.15).hexa() |
| 18 |
: lightenBy(Color(color), 0.15).hexa() |
| 19 |
setHighlightColor(hl) |
| 20 |
}) |
| 21 |
return () => cancelAnimationFrame(id) |
| 22 |
}, [color, bgColor, theme?.color]) |
| 23 |
|
| 24 |
return ( |
| 25 |
<div |
| 26 |
className={classNames({ |
| 27 |
'group w-full overflow-hidden relative min-h-full button-focus button-card': |
| 28 |
context === 'style', |
| 29 |
})}> |
| 30 |
<Transition |
| 31 |
appear={true} |
| 32 |
show={!highlightColor} |
| 33 |
leave="transition-opacity duration-1000" |
| 34 |
leaveFrom="opacity-100" |
| 35 |
leaveTo="opacity-0" |
| 36 |
className="absolute inset-0 z-10 bg-white"> |
| 37 |
<div |
| 38 |
className={classNames({ |
| 39 |
'm-2 p-2 pt-1': context === 'style', |
| 40 |
'p-2': context !== 'style', |
| 41 |
})}> |
| 42 |
<SkeletonParts |
| 43 |
highlightColor="hsl(0deg 0% 75%)" |
| 44 |
color="hsl(0deg 0% 80%)" |
| 45 |
/> |
| 46 |
</div> |
| 47 |
</Transition> |
| 48 |
{/* If a theme is passed in, render a second skeleton under it so we can fade into it */} |
| 49 |
{Boolean(highlightColor) && ( |
| 50 |
<div |
| 51 |
className="overflow-hidden absolute inset-0 opacity-30" |
| 52 |
style={{ |
| 53 |
zIndex: -1, |
| 54 |
}}> |
| 55 |
<div |
| 56 |
className={classNames({ |
| 57 |
'm-2 p-2 pt-1': context === 'style', |
| 58 |
'p-2': context !== 'style', |
| 59 |
})} |
| 60 |
style={{ |
| 61 |
backgroundColor: bgColor, |
| 62 |
textAlign: 'initial', |
| 63 |
}}> |
| 64 |
<SkeletonParts |
| 65 |
highlightColor={highlightColor} |
| 66 |
color={color} |
| 67 |
/> |
| 68 |
</div> |
| 69 |
</div> |
| 70 |
)} |
| 71 |
</div> |
| 72 |
) |
| 73 |
} |
| 74 |
|
| 75 |
const SkeletonParts = ({ color, highlightColor }) => { |
| 76 |
return ( |
| 77 |
<SkeletonTheme |
| 78 |
duration={2.3} |
| 79 |
baseColor={color} |
| 80 |
highlightColor={highlightColor}> |
| 81 |
<Skeleton className="h-36 mb-5 rounded-none" /> |
| 82 |
<div className="flex flex-col items-center"> |
| 83 |
<div> |
| 84 |
<Skeleton className="w-28 h-4 mb-1 rounded-none" /> |
| 85 |
</div> |
| 86 |
<div> |
| 87 |
<Skeleton className="w-44 h-4 mb-1 rounded-none" /> |
| 88 |
</div> |
| 89 |
<div> |
| 90 |
<Skeleton className="w-12 h-6 mb-1 rounded-none" /> |
| 91 |
</div> |
| 92 |
</div> |
| 93 |
<div className="px-4"> |
| 94 |
<Skeleton className="h-24 my-5 rounded-none" /> |
| 95 |
<div className="flex justify-between gap-4"> |
| 96 |
<div> |
| 97 |
<div> |
| 98 |
<Skeleton className="w-40 h-4 mb-1 rounded-none" /> |
| 99 |
</div> |
| 100 |
<div> |
| 101 |
<Skeleton className="w-40 h-4 mb-1 rounded-none" /> |
| 102 |
</div> |
| 103 |
<div> |
| 104 |
<Skeleton className="w-40 h-4 mb-1 rounded-none" /> |
| 105 |
</div> |
| 106 |
</div> |
| 107 |
<div> |
| 108 |
<div> |
| 109 |
<Skeleton className="w-24 h-4 mb-1 rounded-none" /> |
| 110 |
</div> |
| 111 |
<div> |
| 112 |
<Skeleton className="w-24 h-4 mb-1 rounded-none" /> |
| 113 |
</div> |
| 114 |
</div> |
| 115 |
</div> |
| 116 |
</div> |
| 117 |
</SkeletonTheme> |
| 118 |
) |
| 119 |
} |
| 120 |
|
| 121 |
const lightenBy = (color, ratio) => { |
| 122 |
const lightness = color.lightness() |
| 123 |
return color.lightness(lightness + (100 - lightness) * ratio) |
| 124 |
} |
| 125 |
|
| 126 |
const darkenBy = (color, ratio) => { |
| 127 |
const lightness = color.lightness() |
| 128 |
return color.lightness(lightness - lightness * ratio) |
| 129 |
} |
| 130 |
|