import React, {forwardRef, ImgHTMLAttributes, useState, AriaAttributes, DOMAttributes} from 'react'
import {styled} from '@karma.run/react'
import {themeMiddleware, Theme, useThemeStyle, cssRuleWithTheme} from '../style/themeContext'
import {
TransitionDuration,
MarginProps,
WidthProps,
HeightProps,
extractStyleProps,
FlexChildProps
} from '../style/helpers'
interface ImageWrapperProps extends WidthProps, HeightProps, MarginProps, FlexChildProps {
isLoaded?: boolean
contain?: boolean
theme: Theme
}
const ImageWrapper = styled(
'img',
({theme, isLoaded, contain, ...props}: ImageWrapperProps) => ({
_className: process.env.NODE_ENV !== 'production' ? 'Image' : undefined,
display: 'block',
objectFit: contain ? 'contain' : 'cover',
opacity: isLoaded ? 1 : 0,
backgroundColor: theme.colors.light,
transitionProperty: 'opacity',
transitionDuration: TransitionDuration.ExtraSlow,
...props
}),
themeMiddleware
)
export interface ImageProps
extends WidthProps,
HeightProps,
MarginProps,
FlexChildProps,
Omit, 'width' | 'height'> {
contain?: boolean
imageWidth?: number
imageHeight?: number
}
export const Image = forwardRef(function Image(
{contain, onLoad, imageWidth, imageHeight, ...props},
ref
) {
const [isLoaded, setLoaded] = useState(false)
const [styleProps, elementProps] = extractStyleProps(props)
return (
{
setLoaded(true)
onLoad?.(e)
}}
/>
)
})
interface PlaceholderImageStyleProps extends WidthProps, HeightProps, MarginProps, FlexChildProps {
theme: Theme
}
const PlaceholderImageStyle = cssRuleWithTheme(({theme, ...props}) => ({
_className: process.env.NODE_ENV !== 'production' ? 'PlaceholderImage' : undefined,
overflow: 'hidden',
fill: 'inherit',
stroke: theme.colors.grayLight,
...props
}))
export interface PlaceholderImageProps
extends WidthProps,
HeightProps,
MarginProps,
FlexChildProps,
AriaAttributes,
DOMAttributes {}
export const PlaceholderImage = forwardRef(
function PlaceholderImage(props, ref) {
const [layoutProps, elementProps] = extractStyleProps(props)
const css = useThemeStyle(layoutProps)
return (
)
}
)