Skip to content

Commit

Permalink
Modify ReadMoreAccordion to be more mobile friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
lizageorge committed Jun 19, 2024
1 parent 7c690e6 commit cface7f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/components/accordion/ReadMoreAccordion.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import clsx from 'clsx'
import type { FC } from 'react'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import React from 'react'

import { ReadMoreButton } from '../index'
Expand All @@ -19,21 +19,43 @@ const ReadMoreAccordion: FC<ReadMoreAccordionProps> = ({
centerReadMoreButton = false,
}) => {
const [folded, setFolded] = useState(true)
const MAX_WORD_COUNT = 75
const MOBILE_MAX_WORD_COUNT = 15
const DESKTOP_MAX_WORD_COUNT = 75
// maxWordCount dynamically changes based on screen size
const [maxWordCount, setMaxWordCount] = useState(MOBILE_MAX_WORD_COUNT)

useEffect(() => {
const updateWordCount = () => {
if (window.innerWidth <= 768) {
setMaxWordCount(MOBILE_MAX_WORD_COUNT)
} else {
setMaxWordCount(DESKTOP_MAX_WORD_COUNT)
}
}

// Initial check
updateWordCount()

// Add resize event listener
window.addEventListener('resize', updateWordCount)
return () => {
window.removeEventListener('resize', updateWordCount)
}
}, [])

let contentTruncated = content
let childrenTruncated = children

const shouldTruncate =
(content && content.split(' ').length > MAX_WORD_COUNT) ||
(content && content.split(' ').length > maxWordCount) ||
(children && React.Children.toArray(children).length > 1)

if (children && shouldTruncate) {
childrenTruncated = <div>{React.Children.toArray(children)[0]}</div>
}

if (content && shouldTruncate) {
contentTruncated = content.split(' ').slice(0, MAX_WORD_COUNT).join(' ')
contentTruncated = content.split(' ').slice(0, maxWordCount).join(' ')
contentTruncated += '...'
}

Expand Down
1 change: 1 addition & 0 deletions src/pages/company/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ const Company: FC<CompanyDetailsProps> = ({
renewableEnergyDetails.description,
mainParagraphStyle,
)}
{/* <ReadMoreAccordion content={renewableEnergyDetails.description} /> */}
</ChartDetailsCard>
}
chartOnLeft={chartDirections['energy']}
Expand Down

0 comments on commit cface7f

Please sign in to comment.