add Picture component for displaying images in articles with h/v/s layout support
This commit is contained in:
1 parent
1bd80aa0b2
commit
a55d474336
7 files changed
+75
-75
No files matched your search
@@ -1,36 +0,0 @@
|
||||
import s from './styles.module.css'
|
||||
|
||||
import { memo, useRef, useEffect, useCallback } from 'react'
|
||||
|
||||
export type ImageProps = {
|
||||
style?: React.CSSProperties
|
||||
src: string
|
||||
alt?: string
|
||||
srcSet?: string
|
||||
}
|
||||
|
||||
export const Image: React.FC<ImageProps> = memo(props => {
|
||||
const ref = useRef<HTMLImageElement>()
|
||||
const onLoad = useCallback((event) => {
|
||||
event.target.classList.add('loaded')
|
||||
event.target.classList.remove('pending')
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (ref.current) {
|
||||
ref.current.classList.add('pending')
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<img
|
||||
ref={ref}
|
||||
onLoad={onLoad}
|
||||
className={s.image}
|
||||
style={props.style}
|
||||
alt={props.alt}
|
||||
src={props.src}
|
||||
srcSet={props.srcSet}
|
||||
/>
|
||||
)
|
||||
})
|
||||
@@ -1,12 +0,0 @@
|
||||
.image {
|
||||
width: 100%;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.image.pending {
|
||||
/* opacity: 0; */
|
||||
}
|
||||
|
||||
.image.loaded {
|
||||
/* opacity: 1; */
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import s from './picture.module.css'
|
||||
|
||||
import { memo } from 'react'
|
||||
import Image from 'next/image'
|
||||
import { Pic } from '@/types'
|
||||
|
||||
const modifiers = new Map([
|
||||
['square', s.square],
|
||||
['vertical', s.vertical],
|
||||
['horizontal', ''],
|
||||
])
|
||||
|
||||
function size(w: number, h: number, tolerance: number) {
|
||||
const r = w / h
|
||||
if (r < 1 && r > tolerance) {
|
||||
return 'square'
|
||||
}
|
||||
|
||||
if (w < h) {
|
||||
return 'vertical'
|
||||
}
|
||||
|
||||
return 'horizontal'
|
||||
}
|
||||
|
||||
export type PictureProps = Pic & {
|
||||
style?: React.CSSProperties
|
||||
}
|
||||
|
||||
export const Picture: React.FC<PictureProps> = memo(props => {
|
||||
const ss = size(props.width, props.height, 0.95)
|
||||
const layout = modifiers.get(ss)
|
||||
|
||||
return (
|
||||
<figure className={`${s.pic} ${layout}`}>
|
||||
<Image
|
||||
src={props.src}
|
||||
alt={props.alt}
|
||||
width={props.width}
|
||||
height={props.height}
|
||||
layout={'responsive'}
|
||||
/>
|
||||
{!props.caption ? null : (
|
||||
<figcaption>{props.caption} ({props.width}x{props.height})</figcaption>
|
||||
)}
|
||||
</figure>
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
.pic {
|
||||
/* position: relative; */
|
||||
/* width: calc(100% + var(--kazimir-image-increase)); */
|
||||
/* margin-left: calc(-0.5 * var(--kazimir-image-increase)); */
|
||||
margin-bottom: var(--size-xl);
|
||||
}
|
||||
|
||||
.pic.vertical {
|
||||
max-width: 60%;
|
||||
}
|
||||
|
||||
.pic.square {
|
||||
max-width: 80%;
|
||||
}
|
||||
|
||||
.pic img {
|
||||
border-radius: var(--radius);
|
||||
}
|
||||
|
||||
.pic figcaption {
|
||||
color: var(--color-text);
|
||||
font-size: var(--font-size-second);
|
||||
font-style: italic;
|
||||
font-weight: 100;
|
||||
padding-top: var(--size-xs);
|
||||
text-align: center;
|
||||
}
|
||||
Reference in new issue
Block a user