Dasticly changed Image component

It is now WAY lighter and LESS buggy

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-05-13 18:15:34 +02:00
parent 48d60f60e0
commit 0e85ce9722
Signed by: Florian Bouillon
GPG Key ID: 50BD648F12C86AB6
2 changed files with 108 additions and 196 deletions

View File

@ -1,43 +1,34 @@
.parent .parent
position relative transition-property padding, width, height, background, top, left
display inline-flex transition-duration .3s
transition-timing-function ease-in-out
cursor pointer
.image // Animation part 1
transition .3s // this is set to move the image from a normal position to a fixed one
object-fit contain // + one the image itself there is style with position
z-index 2 .fs1
position fixed
&.ph1 z-index 1000
position fixed padding 0
transition 0s > div
width 100%
&.ph2
width 100% !important
position fixed
max-width 100% !important
max-height 100% !important
box-sizing border-box
padding 5%
top 0 !important
left 0 !important
height 100% height 100%
background #000000A0
// Animation part 2
// this animation move the card from its original pos to a fullscreen one
.fs2
padding 8px
width 100% !important
height 100vh !important
background rgba(black, 50%)
// padding 0
user-select none
top 0 !important
left 0 !important
> div
width 100%
height 100%
&.after .body
background #00000000
// height 100%
box-sizing border-box
position fixed
// width 100%
z-index 0
padding initial
.hideOverflow
overflow hidden overflow hidden
.none
display none

View File

@ -1,182 +1,103 @@
import React from 'react' import React, { MouseEventHandler } from 'react'
import { buildClassName } from '../Util'
import NextImage from 'next/image'
import NextImage, { ImageProps } from 'next/image'
import css from './Image.module.styl' import css from './Image.module.styl'
import { buildClassName } from '../Util'
export interface ImageProps { interface Props {
src: string imageProps: ImageProps
deleteOnError?: boolean /**
canFullscreen?: boolean * Define if the image can go fullscreen
width: number */
height: number fullscreen?: boolean
alt?: string
// ClassNames
parentClassName?: string
className?: string
// Events
onClick?: () => void
} }
type evType<T = HTMLImageElement> = React.SyntheticEvent<T, Event> interface States {
image?: {
size: [number | string, number | string]
pos: [number, number]
}
transform?: [number, number]
className?: string
}
export default class Image extends React.Component<ImageProps> { export default class Image extends React.Component<Props, States> {
private ref: React.RefObject<HTMLImageElement> = React.createRef() public state: States = {}
private plchldr: React.RefObject<HTMLDivElement> = React.createRef()
private parent: React.RefObject<HTMLDivElement> = React.createRef()
private pic: React.RefObject<HTMLDivElement> = React.createRef()
private cardPos: Array<number> = [] private animationCount = 0
private cardSize: Array<number> = []
private isFullscreen = false public componentDidUpdate() {
if (!this.props.fullscreen) {return}
public async componentDidMount() { if (this.state.image) {
if (this.props.canFullscreen) { document.body.classList.add(css.body)
window.addEventListener('scroll', this.onScroll) } else {
window.addEventListener('resize', this.onResize) document.body.classList.remove(css.body)
this.onScroll()
this.onResize()
} }
} }
public async componentDidUpdate() { public componentWillUnmount() {
this.pic.current?.classList.remove(css.none) if (!this.props.fullscreen) {return}
if (this.props.canFullscreen) { document.body.classList.remove(css.body)
this.onScroll()
this.onResize()
}
if (this.isFullscreen) {
this.onClick()
}
}
public async componentWillUnmount() {
if (this.props.canFullscreen) {
window.removeEventListener('scroll', this.onScroll)
window.removeEventListener('resize', this.onResize)
}
} }
public render() { public render() {
const pic = ( if (!this.props.fullscreen) {
<div ref={this.pic} className={buildClassName(this.props.parentClassName, css.parent)}> return <NextImage
<NextImage {...this.props.imageProps}
className={buildClassName([css.image], [this.props.className])} objectFit="contain"
src={this.props.src} />
onClick={this.props.canFullscreen ? this.onClick : this.props.onClick} }
onError={this.props.deleteOnError && this.onError || undefined} return (
// layout="fill" <>
width={this.props.width} {this.state.image && (
height={this.props.height} <div style={{width: this.state.image.size[0], height: this.state.image.size[1]}}></div>
alt={this.props.alt} )}
/>
</div> <div
) className={buildClassName(css.parent, [css.fs1, this.state.image], [this.state.className, this.state.image])}
if (this.props.canFullscreen) { style={this.state.image ? {
return ( top: this.state.image.pos[1],
<div ref={this.parent}> left: this.state.image.pos[0],
<div ref={this.plchldr} className={css.none}></div> width: this.state.image.size[0],
{pic} height: this.state.image.size[1]
} : undefined}
onClick={this.props.fullscreen ? this.onClick : undefined}
>
<NextImage
priority
quality={100}
{...this.props.imageProps}
objectFit="contain"
/>
</div> </div>
) </>
} )
return pic
} }
private onScroll = async () => { private onClick: MouseEventHandler<HTMLDivElement> = (ev) => {
if (!this.ref.current || this.isFullscreen || !this.props.canFullscreen) { const target = ev.currentTarget
return const isFullscreen = !(this.state.image && this.state.className)
} const currentCount = ++this.animationCount
this.setState(isFullscreen ? {
this.cardPos = [this.ref.current.offsetTop - window.scrollY, this.ref.current.offsetLeft - window.scrollX] image: this.state.image ?? {
this.ref.current.style.top = this.cardPos[0] + 'px' size: [target.offsetWidth, target.offsetHeight],
this.ref.current.style.left = this.cardPos[1] + 'px' pos: [target.offsetLeft - window.scrollX, target.offsetTop - window.scrollY]
} },
className: undefined
private onResize = async () => { } : {
if (!this.ref.current || !this.plchldr.current || !this.props.canFullscreen || this.isFullscreen) { className: undefined
return }, () => {
}
let tmp = [this.ref.current.offsetHeight, this.ref.current.offsetWidth]
if (this.parent.current) {
tmp = [this.parent.current.offsetHeight, this.ref.current.offsetWidth]
}
this.plchldr.current.style.width = `${tmp[1]}px`
this.plchldr.current.style.height = `${tmp[0]}px`
}
private onClick = async () => {
if (!this.ref.current || !this.props.canFullscreen || !this.plchldr.current) {
return
}
if (this.props.onClick) {
this.props.onClick()
}
const i = this.ref.current
const c = this.plchldr.current
const body = document.body
i.style.top = this.cardPos[0] + 'px'
i.style.left = this.cardPos[1] + 'px'
if (this.isFullscreen) {
i.style.width = this.cardSize[1] + 'px'
i.style.height = this.cardSize[0] + 'px'
body.classList.remove(css.hideOverflow)
i.classList.remove(css.ph2)
i.classList.add(css.after)
setTimeout(() => { setTimeout(() => {
if (i.classList.contains(css.ph2) || i.classList.contains(css.ph1) || this.isFullscreen) { if (this.animationCount !== currentCount) {
return return
} }
const w = this.valToPixel(this.props.width) this.setState({
const mh = this.valToPixel(this.props?.height) className: isFullscreen ? css.fs2 : undefined,
const mw = this.valToPixel(this.props?.width) image: isFullscreen ? this.state.image : undefined
c.classList.add(css.none) })
i.style.height = '' }, isFullscreen ? 10 : 310)
i.style.width = w })
i.style.maxHeight = mh
i.style.maxWidth = mw
i.classList.remove(css.after)
}, 350)
this.isFullscreen = false
} else {
i.classList.add(css.ph1)
c.classList.remove(css.none)
i.classList.add(css.ph2)
i.classList.remove(css.ph1)
body.classList.add(css.hideOverflow)
this.isFullscreen = true
}
} }
private valToPixel(value: number|string|undefined): string {
if (typeof value === 'number') {
return `${value}px`
}
if (typeof value === 'undefined') {
return ''
}
return value
}
private onLoad = async (ev: evType) => {
ev.currentTarget.style.height = ''
ev.currentTarget.style.width = ''
}
private onError = async (ev: evType) => {
this.w('Picture not loaded', ev.currentTarget.src)
ev.currentTarget.parentElement?.classList.add(css.none)
}
private w(...messages: any) {
console.warn('[ Picture ]', ...messages)
}
} }