add Picture component for displaying images in articles with h/v/s layout support

This commit is contained in:
tmshv committed 2021-09-21 16:07:30 +03:00
1 parent 1bd80aa0b2
commit a55d474336
7 files changed
+75 -75

No files matched your search

+48
View File
@@ -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>
)
})
+27
View File
@@ -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;
}