-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.tsx
55 lines (44 loc) · 1.2 KB
/
Image.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import style from "./Image.module.css"
import { ImageBadge, ImageOverlay } from "./addons"
import { type JSX, type Component, splitProps, mergeProps } from "solid-js"
import { createStore } from "solid-js/store"
interface Image extends JSX.HTMLAttributes<HTMLImageElement> {
src?: string
}
type Store = {
isHidden: boolean
}
type ComponentImage = Component<Image> & {
Badge: typeof ImageBadge
Overlay: typeof ImageOverlay
}
const Image: ComponentImage = (props) => {
const merged = mergeProps({}, props)
const [local, others] = splitProps(merged, [
"class",
"classList",
"children",
"onError",
])
const [store, setStore] = createStore<Store>({ isHidden: false })
const onError: JSX.EventHandlerUnion<HTMLImageElement, Event> = (event) => {
setStore("isHidden", true)
local.onError && (local.onError as any)(event)
}
return (
<div
class={style.Image}
classList={{
[style[`Image--hidden`]]: store.isHidden,
[`${local.class}`]: !!local.class,
...local.classList,
}}
>
<img onError={onError} {...others} />
{local.children}
</div>
)
}
Image.Badge = ImageBadge
Image.Overlay = ImageOverlay
export default Image