Created
December 8, 2024 22:09
-
-
Save Ryanjso/596afc549d613f9572e195eca460e350 to your computer and use it in GitHub Desktop.
dropdown example auto height
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useRouter } from 'next/router'; | |
| import React, { useRef, useState } from 'react'; | |
| import BlackChevron from '@/public/images/chevron.svg'; | |
| import WhiteChevron from '@/public/images/white_chevron.svg'; | |
| const DropDown = (props: { | |
| question: string; | |
| answer: string; | |
| isToggled: boolean; | |
| makeActive: () => void; | |
| idx: number; | |
| isHighlighted?: boolean; | |
| arrowColor?: string; | |
| circleColor?: string; | |
| textColor?: string; | |
| backgroundColor?: string; | |
| }) => { | |
| const { backgroundColor, textColor, circleColor, arrowColor } = props; | |
| const ref = useRef<HTMLParagraphElement>(null); | |
| const [wrapperHeight, setWH] = useState(0); | |
| const router = useRouter(); | |
| const showHide = () => { | |
| if (props.isToggled) { | |
| props.makeActive(); | |
| setWH(0); | |
| } else { | |
| if (!ref || !ref.current) return; | |
| const paragraphHeight = ref.current.offsetHeight; | |
| props.makeActive(); | |
| setWH(paragraphHeight); | |
| } | |
| }; | |
| const onAnswerClick = (e: React.MouseEvent<HTMLDivElement>) => { | |
| const target = e.target as HTMLDivElement; | |
| const link = target.closest('a'); | |
| if (!link) return; | |
| e.preventDefault(); | |
| const href = link.href.replace(link.origin, ''); | |
| router.push(href); | |
| }; | |
| return ( | |
| <div | |
| className={`mb-[0.4rem] rounded-xl last:mb-0`} | |
| style={{ backgroundColor: backgroundColor || '#bababa25' }} | |
| > | |
| <div | |
| onClick={() => showHide()} | |
| className={`flex cursor-pointer items-center justify-between p-3 text-base sm:py-7 sm:px-8 sm:text-[28px] `} | |
| style={{ | |
| color: textColor || 'black', | |
| }} | |
| > | |
| {props.question} | |
| <div | |
| className={`relative ml-2 h-6 w-6 shrink-0 rounded-full sm:h-12 sm:w-12 `} | |
| style={{ | |
| backgroundColor: circleColor || '#00D54B', | |
| }} | |
| > | |
| <div | |
| className={` flex h-full w-full select-none items-center justify-center ${ | |
| !props.isToggled && 'rotate-180' | |
| }`} | |
| > | |
| {arrowColor === 'white' ? ( | |
| <WhiteChevron className='mb-1' /> | |
| ) : ( | |
| <BlackChevron className='mb-1' /> | |
| )} | |
| </div> | |
| </div> | |
| </div> | |
| <div | |
| style={{ height: props.isToggled ? wrapperHeight : 0 }} | |
| className={`grid overflow-y-hidden transition-height duration-300`} | |
| > | |
| <div | |
| onClick={onAnswerClick} | |
| ref={ref} | |
| className={`px-3 pb-3 pt-1 font-ppa-b text-xs sm:px-8 sm:pb-7 sm:text-xl ${ | |
| props.isHighlighted ? 'a-link-black ' : 'a-link-green ' | |
| }`} | |
| style={{ | |
| color: textColor || '#A3A3A3', | |
| }} | |
| dangerouslySetInnerHTML={{ __html: props.answer }} | |
| ></div> | |
| </div> | |
| </div> | |
| ); | |
| }; | |
| export { DropDown }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment