diff --git a/ui/component/comment/view.jsx b/ui/component/comment/view.jsx index 0946436cd..879384bd3 100644 --- a/ui/component/comment/view.jsx +++ b/ui/component/comment/view.jsx @@ -26,7 +26,7 @@ import React, { useEffect, useState } from 'react'; import { parseURI } from 'util/lbryURI'; import DateTime from 'component/dateTime'; import Button from 'component/button'; -import Expandable from 'component/expandable'; +import Expandable from 'component/common/expandable'; import MarkdownPreview from 'component/common/markdown-preview'; import CommentBadge from 'component/common/comment-badge'; import ChannelThumbnail from 'component/channelThumbnail'; @@ -88,8 +88,6 @@ type Props = { threadDepthLevel?: number, }; -const LENGTH_TO_COLLAPSE = 300; - function CommentView(props: Props) { const { comment, @@ -176,7 +174,6 @@ function CommentView(props: Props) { const contentChannelClaim = getChannelFromClaim(claim); const commentByOwnerOfContent = contentChannelClaim && contentChannelClaim.permanent_url === authorUri; const stickerFromMessage = parseSticker(message); - const isExpandable = editedMessage.length >= LENGTH_TO_COLLAPSE; let channelOwnerOfContent; try { @@ -388,8 +385,8 @@ function CommentView(props: Props) {
- ) : isExpandable ? ( - + ) : ( + - ) : ( - )} diff --git a/ui/component/common/expandable.jsx b/ui/component/common/expandable.jsx new file mode 100644 index 000000000..5baf84f0c --- /dev/null +++ b/ui/component/common/expandable.jsx @@ -0,0 +1,70 @@ +// @flow +import React from 'react'; +import classnames from 'classnames'; +import Button from 'component/button'; +import { useOnResize } from 'effects/use-on-resize'; + +const COLLAPSED_HEIGHT = 120; + +type Props = { + children: React$Node | Array, +}; + +export default function Expandable(props: Props) { + const { children } = props; + + const ref = React.useRef(); + + const [expanded, setExpanded] = React.useState(false); + const [childRect, setRect] = React.useState(); + + const childOverflows = childRect && childRect.height > COLLAPSED_HEIGHT; + + function handleClick() { + setExpanded(!expanded); + } + + // Update the rect when children changes, + // this is needed when there is an image or claim embed + // to be loaded ! :D + React.useLayoutEffect(() => { + if (ref.current) { + const childElem = ref.current.children[0]; + setRect(childElem.getBoundingClientRect()); + } + }, [children]); + + const expandableRef = React.useCallback((node) => { + if (node) { + const childElem = node.children[0]; + setRect(childElem.getBoundingClientRect()); + ref.current = node; + } + }, []); + + // Update the childRect initially & when the window size changes. + useOnResize(() => expandableRef(ref.current)); + + return ( + <> +
+ {children} +
+ + {childOverflows && ( +