koel/resources/assets/js/directives/hideBrokenIcon.ts
2024-10-14 00:37:01 +07:00

12 lines
465 B
TypeScript

import type { Directive } from 'vue'
export const hideBrokenIcon: Directive = {
mounted: async (el: HTMLImageElement) => {
el.addEventListener('error', () => (el.style.visibility = 'hidden'))
// For v-bind, an empty source e.g. :src="emptySrc" will NOT be rendered
// and the error event will not be triggered.
// We'll work around by explicitly setting the src to an empty string, which will trigger the error.
el.src = el.src || ''
},
}