mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-25 11:29:53 +01:00
feat(ui): add BlurHashCanvas placeholder component
This commit is contained in:
parent
9f2b78ab2f
commit
6ae855256c
4 changed files with 92 additions and 0 deletions
46
ui/src/common/BlurHashCanvas.jsx
Normal file
46
ui/src/common/BlurHashCanvas.jsx
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { useEffect, useRef } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { decode } from 'blurhash'
|
||||
|
||||
// A blurhash carries no detail beyond a few dozen pixels; CSS upscales the canvas.
|
||||
const DECODE_SIZE = 32
|
||||
|
||||
export const BlurHashCanvas = ({ hash, className }) => {
|
||||
const canvasRef = useRef(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!hash || !canvasRef.current) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
const pixels = decode(hash, DECODE_SIZE, DECODE_SIZE)
|
||||
const ctx = canvasRef.current.getContext('2d')
|
||||
if (!ctx) {
|
||||
return
|
||||
}
|
||||
const imageData = ctx.createImageData(DECODE_SIZE, DECODE_SIZE)
|
||||
imageData.data.set(pixels)
|
||||
ctx.putImageData(imageData, 0, 0)
|
||||
} catch {
|
||||
// A malformed hash simply leaves the canvas transparent.
|
||||
}
|
||||
}, [hash])
|
||||
|
||||
if (!hash) {
|
||||
return null
|
||||
}
|
||||
return (
|
||||
<canvas
|
||||
ref={canvasRef}
|
||||
width={DECODE_SIZE}
|
||||
height={DECODE_SIZE}
|
||||
className={className}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
BlurHashCanvas.propTypes = {
|
||||
hash: PropTypes.string,
|
||||
className: PropTypes.string,
|
||||
}
|
||||
38
ui/src/common/BlurHashCanvas.test.jsx
Normal file
38
ui/src/common/BlurHashCanvas.test.jsx
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { render } from '@testing-library/react'
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { BlurHashCanvas } from './BlurHashCanvas'
|
||||
|
||||
describe('BlurHashCanvas', () => {
|
||||
// jsdom has no real 2D context; stub it so getContext doesn't hit its noisy "not implemented" path.
|
||||
let getContextSpy
|
||||
beforeEach(() => {
|
||||
getContextSpy = vi
|
||||
.spyOn(HTMLCanvasElement.prototype, 'getContext')
|
||||
.mockReturnValue({
|
||||
createImageData: (w, h) => ({ data: new Uint8ClampedArray(w * h * 4) }),
|
||||
putImageData: () => {},
|
||||
})
|
||||
})
|
||||
afterEach(() => {
|
||||
getContextSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('renders nothing without a hash', () => {
|
||||
const { container } = render(<BlurHashCanvas hash="" />)
|
||||
expect(container.querySelector('canvas')).toBeNull()
|
||||
})
|
||||
|
||||
it('renders a canvas for a valid hash', () => {
|
||||
const { container } = render(
|
||||
<BlurHashCanvas hash="LEHV6nWB2yk8pyo0adR*.7kCMdnj" />,
|
||||
)
|
||||
expect(container.querySelector('canvas')).not.toBeNull()
|
||||
})
|
||||
|
||||
it('renders a canvas without throwing on a malformed hash', () => {
|
||||
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const { container } = render(<BlurHashCanvas hash="!!!not-a-blurhash!!!" />)
|
||||
expect(container.querySelector('canvas')).not.toBeNull()
|
||||
spy.mockRestore()
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue