Skip to content

Commit

Permalink
Refactor collapse. Show 3 lines. Use on charity.
Browse files Browse the repository at this point in the history
  • Loading branch information
sipec committed Jan 19, 2023
1 parent 2ce43f6 commit cbd7b2f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 64 deletions.
7 changes: 3 additions & 4 deletions web/components/contract/contract-description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function ContractDescription(props: {
) : (
<CollapsibleContent
content={contract.description}
contractId={contract.id}
stateKey={`isCollapsed-contract-${contract.id}`}
/>
)}
</div>
Expand Down Expand Up @@ -104,10 +104,9 @@ function ContractActions(props: {
<>
<CollapsibleContent
content={contract.description}
contractId={contract.id}
stateKey={`isCollapsed-contract-${contract.id}`}
/>
<Spacer h={4} />
<Row className="my-4 items-center gap-2 text-xs">
<Row className="mb-4 items-center gap-2 text-xs">
{isAdmin && 'Admin '}
<Button color={'gray'} size={'2xs'} onClick={toggleResolver}>
Resolve
Expand Down
44 changes: 22 additions & 22 deletions web/components/widgets/collapsible-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { safeLocalStorage } from 'web/lib/util/local'
import { Row } from '../layout/row'
import { Content } from './editor'

export const COLLAPSIBLE_HEIGHT = 45
export const COLLAPSIBLE_HEIGHT = 26 * 3 // line height is 26px
export const SHOW_COLLAPSE_TRESHOLD = 180

export function ShowMoreLessButton(props: {
Expand Down Expand Up @@ -51,9 +51,9 @@ export function ShowMoreLessButton(props: {

export function CollapsibleContent(props: {
content: JSONContent | string
contractId: string
stateKey: string
}) {
const { content, contractId } = props
const { content, stateKey } = props
const [shouldAllowCollapseOfContent, setShouldAllowCollapseOfContent] =
useState(false)
const contentRef = useRef<HTMLDivElement>(null)
Expand All @@ -66,9 +66,7 @@ export function CollapsibleContent(props: {
}
}, [contentRef.current?.offsetHeight])
if (shouldAllowCollapseOfContent) {
return (
<ActuallyCollapsibleContent content={content} contractId={contractId} />
)
return <ActuallyCollapsibleContent content={content} stateKey={stateKey} />
}
return (
<div ref={contentRef}>
Expand All @@ -80,15 +78,15 @@ export function CollapsibleContent(props: {
// Moved to its own component to reduce unnecessary isCollapsed states in local storage
function ActuallyCollapsibleContent(props: {
content: JSONContent | string
contractId: string
stateKey: string
}) {
const { content, contractId } = props
const { content, stateKey } = props
const [isCollapsed, setIsCollapsed] = usePersistentState<boolean>(false, {
store: storageStore(safeLocalStorage()),
key: `isCollapsed-contract-${contractId}`,
key: stateKey,
})
return (
<div className="relative">
<div>
<div
style={{ height: isCollapsed ? COLLAPSIBLE_HEIGHT : 'auto' }}
className={clsx(
Expand All @@ -106,18 +104,20 @@ function ActuallyCollapsibleContent(props: {
</>
)}
</div>
<ShowMoreLessButton
className="absolute right-0 -bottom-8 bg-transparent"
onClick={() => {
if (!isCollapsed)
window.scrollTo({
top: 0,
behavior: 'smooth',
})
setIsCollapsed(!isCollapsed)
}}
isCollapsed={isCollapsed}
/>
<div className="text-right">
<ShowMoreLessButton
className="bg-transparent"
onClick={() => {
if (!isCollapsed)
window.scrollTo({
top: 0,
behavior: 'smooth',
})
setIsCollapsed(!isCollapsed)
}}
isCollapsed={isCollapsed}
/>
</div>
</div>
)
}
2 changes: 1 addition & 1 deletion web/pages/[username]/[contractSlug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export function ContractPageContent(
/>

<ContractDescription
className="mt-6 mb-2 px-2"
className="mt-6 px-2"
contract={contract}
toggleResolver={() => setShowResolver(!showResolver)}
/>
Expand Down
43 changes: 6 additions & 37 deletions web/pages/charity/[charitySlug].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { sortBy, sumBy, uniqBy } from 'lodash'
import clsx from 'clsx'
import React, { useEffect, useRef, useState } from 'react'
import React, { useState } from 'react'
import Image from 'next/legacy/image'

import { Col } from 'web/components/layout/col'
Expand All @@ -23,6 +22,7 @@ import { track } from 'web/lib/service/analytics'
import { SEO } from 'web/components/SEO'
import { Button } from 'web/components/buttons/button'
import { FullscreenConfetti } from 'web/components/widgets/fullscreen-confetti'
import { CollapsibleContent } from 'web/components/widgets/collapsible-content'

export default function CharityPageWrapper() {
const router = useRouter()
Expand Down Expand Up @@ -87,7 +87,10 @@ function CharityPage(props: { charity: Charity }) {
/>
</Row>
<h2 className="mt-7 mb-2 text-xl text-indigo-700">About</h2>
<Blurb text={description} />
<CollapsibleContent
content={description}
stateKey={`isCollapsed-charity-${charity.id}`}
/>
{newToOld.map((txn) => (
<Donation key={txn.id} txn={txn} />
))}
Expand All @@ -97,40 +100,6 @@ function CharityPage(props: { charity: Charity }) {
)
}

function Blurb({ text }: { text: string }) {
const [open, setOpen] = useState(false)

// Calculate whether the full blurb is already shown
const ref = useRef<HTMLDivElement>(null)
const [hideExpander, setHideExpander] = useState(false)
useEffect(() => {
if (ref.current) {
setHideExpander(ref.current.scrollHeight <= ref.current.clientHeight)
}
}, [])

return (
<>
<div
className={clsx(
'whitespace-pre-line text-gray-500',
!open && 'line-clamp-5'
)}
ref={ref}
>
{text}
</div>
<Button
color="indigo"
onClick={() => setOpen(!open)}
className={clsx('my-3', hideExpander && 'invisible')}
>
{open ? 'Hide' : 'Read more'}
</Button>
</>
)
}

function Details(props: {
charity: Charity
totalRaised: number
Expand Down

3 comments on commit cbd7b2f

@vercel
Copy link

@vercel vercel bot commented on cbd7b2f Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-pi-teal.vercel.app
docs-git-main-mantic.vercel.app
docs.manifold.markets
docs-mantic.vercel.app

@vercel
Copy link

@vercel vercel bot commented on cbd7b2f Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on cbd7b2f Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dev – ./web

dev-manifold.vercel.app
dev.manifold.markets
dev-git-main-mantic.vercel.app
dev-mantic.vercel.app

Please sign in to comment.