From 1299a0e7669b588f0c2df03af392eb65c5b82b2b Mon Sep 17 00:00:00 2001 From: muerwre Date: Sun, 25 Aug 2019 17:29:26 +0700 Subject: [PATCH] NodeImageBlock resizes to content --- src/components/node/ImageSwitcher/index.tsx | 9 ++- src/components/node/ImageSwitcher/styles.scss | 3 +- src/components/node/NodeImageBlock/index.tsx | 58 ++++++++++++++++--- .../node/NodeImageBlock/styles.scss | 19 ++++++ .../examples/ImageExample/index.tsx | 4 +- 5 files changed, 79 insertions(+), 14 deletions(-) diff --git a/src/components/node/ImageSwitcher/index.tsx b/src/components/node/ImageSwitcher/index.tsx index ac91c6a3..92b20433 100644 --- a/src/components/node/ImageSwitcher/index.tsx +++ b/src/components/node/ImageSwitcher/index.tsx @@ -7,13 +7,18 @@ import classNames = require('classnames'); interface IProps { total: number; current: number; + onChange: (current: number) => void; } -const ImageSwitcher: FC = ({ total, current }) => ( +const ImageSwitcher: FC = ({ total, current, onChange }) => (
{range(0, total).map(item => ( -
+
onChange(item)} + /> ))}
diff --git a/src/components/node/ImageSwitcher/styles.scss b/src/components/node/ImageSwitcher/styles.scss index ba53465b..998fe6b0 100644 --- a/src/components/node/ImageSwitcher/styles.scss +++ b/src/components/node/ImageSwitcher/styles.scss @@ -2,6 +2,7 @@ width: 100%; height: 0; position: relative; + z-index: 2; } .switcher { @@ -22,7 +23,7 @@ display: flex; align-items: center; justify-content: center; - opacity: .5; + opacity: 0.5; transition: opacity 0.25s; &:hover { diff --git a/src/components/node/NodeImageBlock/index.tsx b/src/components/node/NodeImageBlock/index.tsx index 1e675602..fe12fa9b 100644 --- a/src/components/node/NodeImageBlock/index.tsx +++ b/src/components/node/NodeImageBlock/index.tsx @@ -1,10 +1,20 @@ -import React, { FC, useMemo } from 'react'; +import React, { + FC, + useMemo, + useState, + useEffect, + RefObject, + LegacyRef, + useRef, + useCallback, +} from 'react'; import { ImageSwitcher } from '../ImageSwitcher'; import * as styles from './styles.scss'; import { INode } from '~/redux/types'; import classNames from 'classnames'; import { getImageSize } from '~/utils/dom'; import { UPLOAD_TYPES } from '~/redux/uploads/constants'; +import { readFileSync } from 'fs'; interface IProps { is_loading: boolean; @@ -12,26 +22,56 @@ interface IProps { } const NodeImageBlock: FC = ({ node, is_loading }) => { + const [current, setCurrent] = useState(0); + const [height, setHeight] = useState(0); + const refs = useRef([]); + // const [refs, setRefs] = useState>>({}); + const images = useMemo( () => (node && node.files && node.files.filter(({ type }) => type === UPLOAD_TYPES.IMAGE)) || [], [node] ); + const setRef = useCallback( + index => el => { + refs.current[index] = el; + }, + [refs] + ); + + useEffect(() => { + console.log({ refs }); + + if (!refs || !refs.current[current]) return; + + const el = refs.current[current]; + + const element_height = el.getBoundingClientRect && el.getBoundingClientRect().height; + + setHeight(element_height); + }, [refs, current]); + return (
{!is_loading && (
- + -
- {images.map(file => ( - + {images.map((file, index) => ( +
+ > + +
))}
diff --git a/src/components/node/NodeImageBlock/styles.scss b/src/components/node/NodeImageBlock/styles.scss index 5a664443..cc05c9cb 100644 --- a/src/components/node/NodeImageBlock/styles.scss +++ b/src/components/node/NodeImageBlock/styles.scss @@ -5,6 +5,9 @@ display: flex; align-items: center; justify-content: center; + position: relative; + overflow: hidden; + transition: height 0.5s; .image { max-height: 800px; @@ -13,3 +16,19 @@ border-radius: $radius $radius 0 0; } } + +.image_wrap { + width: 100%; + position: absolute; + top: 0; + left: 0; + opacity: 0; + pointer-events: none; + touch-action: none; + z-index: 1; + transition: opacity 0.5s; + + &:global(.is_active) { + opacity: 1; + } +} diff --git a/src/containers/examples/ImageExample/index.tsx b/src/containers/examples/ImageExample/index.tsx index 5a39beed..b511526f 100644 --- a/src/containers/examples/ImageExample/index.tsx +++ b/src/containers/examples/ImageExample/index.tsx @@ -9,13 +9,13 @@ import { NodePanel } from '~/components/node/NodePanel'; import { NodeRelated } from '~/components/node/NodeRelated'; import { Tags } from '~/components/node/Tags'; import { NodeNoComments } from '~/components/node/NodeNoComments'; -import { ImageSwitcher } from "~/components/node/ImageSwitcher"; +import { ImageSwitcher } from '~/components/node/ImageSwitcher'; interface IProps {} const ImageExample: FC = () => ( - +