-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Migrate ExpandableInfo to Chakra
#8224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
23fe136
f5b4062
7cb5c44
1420c34
356d3d7
ed72587
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,216 +1,155 @@ | ||||||||||
| import React, { ReactNode, useState } from "react" | ||||||||||
| import styled from "@emotion/styled" | ||||||||||
| import { motion } from "framer-motion" | ||||||||||
| import React, { ReactNode } from "react" | ||||||||||
| import { GatsbyImage, IGatsbyImageData } from "gatsby-plugin-image" | ||||||||||
| import { | ||||||||||
| BackgroundProps, | ||||||||||
| Box, | ||||||||||
| Center, | ||||||||||
| ChakraProps, | ||||||||||
| Collapse, | ||||||||||
| forwardRef, | ||||||||||
| Heading, | ||||||||||
| HStack, | ||||||||||
| Icon, | ||||||||||
| Stack, | ||||||||||
| Text, | ||||||||||
| useDisclosure, | ||||||||||
| VStack, | ||||||||||
| } from "@chakra-ui/react" | ||||||||||
| import { motion } from "framer-motion" | ||||||||||
| import { MdExpandMore } from "react-icons/md" | ||||||||||
|
|
||||||||||
| import Icon from "./Icon" | ||||||||||
|
|
||||||||||
| const Card = styled.div<{ | ||||||||||
| background: string | ||||||||||
| }>` | ||||||||||
| border: 1px solid ${({ theme }) => theme.colors.border}; | ||||||||||
| border-radius: 2px; | ||||||||||
| padding: 1.5rem; | ||||||||||
| display: flex; | ||||||||||
| flex-direction: column; | ||||||||||
| margin-bottom: 1rem; | ||||||||||
| background: ${({ background, theme }) => | ||||||||||
| background ? theme.colors[background] : theme.colors.background}; | ||||||||||
| &:hover { | ||||||||||
| img { | ||||||||||
| transform: scale(1.08); | ||||||||||
| transition: transform 0.1s; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const Content = styled.div` | ||||||||||
| display: flex; | ||||||||||
| align-items: center; | ||||||||||
| justify-content: space-between; | ||||||||||
| gap: 3rem; | ||||||||||
| @media (max-width: ${({ theme }) => theme.breakpoints.m}) { | ||||||||||
| gap: 2rem; | ||||||||||
| flex-direction: column; | ||||||||||
| } | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const TitleContent = styled.div` | ||||||||||
| display: flex; | ||||||||||
| align-items: center; | ||||||||||
| gap: 3rem; | ||||||||||
| width: 100%; | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const Title = styled.h2` | ||||||||||
| margin-top: 0rem; | ||||||||||
| margin-bottom: 0.5rem; | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const TextPreview = styled.p` | ||||||||||
| font-weight: 400; | ||||||||||
| color: ${(props) => props.theme.colors.text200}; | ||||||||||
| margin-bottom: 0rem; | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const Text = styled(motion.div)` | ||||||||||
| font-size: 16px; | ||||||||||
| font-weight: 400; | ||||||||||
| color: ${(props) => props.theme.colors.text}; | ||||||||||
| margin-top: 2rem; | ||||||||||
| border-top: 1px solid ${(props) => props.theme.colors.border}; | ||||||||||
| padding-top: 1.5rem; | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const Question = styled.div` | ||||||||||
| margin-right: 1rem; | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const Header = styled.div` | ||||||||||
| display: flex; | ||||||||||
| width: 100%; | ||||||||||
| margin: 1rem 0; | ||||||||||
| align-items: center; | ||||||||||
| img { | ||||||||||
| margin-right: 1.5rem; | ||||||||||
| } | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| const ButtonContainer = styled(motion.div)` | ||||||||||
| display: flex; | ||||||||||
| width: 5rem; | ||||||||||
| justify-content: center; | ||||||||||
| align-items: center; | ||||||||||
| min-height: 10rem; | ||||||||||
| cursor: pointer; | ||||||||||
| @media (max-width: ${({ theme }) => theme.breakpoints.m}) { | ||||||||||
| min-height: 100%; | ||||||||||
| height: 100%; | ||||||||||
| width: 100%; | ||||||||||
| margin: 0; | ||||||||||
| } | ||||||||||
| &:hover { | ||||||||||
| svg { | ||||||||||
| transform: scale(1.25); | ||||||||||
| transition: transform 0.1s; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| ` | ||||||||||
|
|
||||||||||
| export interface IProps { | ||||||||||
| export interface IProps extends ChakraProps { | ||||||||||
| children?: React.ReactNode | ||||||||||
| image?: IGatsbyImageData | ||||||||||
| title: ReactNode | ||||||||||
| contentPreview: ReactNode | ||||||||||
| background: string | ||||||||||
| forceOpen: boolean | ||||||||||
| background: BackgroundProps["background"] | ||||||||||
| forceOpen?: boolean | ||||||||||
| className?: string | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const ExpandableInfo: React.FC<IProps> = ({ | ||||||||||
| image, | ||||||||||
| title, | ||||||||||
| contentPreview, | ||||||||||
| children, | ||||||||||
| background, | ||||||||||
| forceOpen, | ||||||||||
| className, | ||||||||||
| }) => { | ||||||||||
| const [isVisible, setIsVisible] = useState(forceOpen) | ||||||||||
| const expandCollapse = { | ||||||||||
| collapsed: { | ||||||||||
| height: 0, | ||||||||||
| transition: { | ||||||||||
| when: "afterChildren", | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| expanded: { | ||||||||||
| height: "100%", | ||||||||||
| transition: { | ||||||||||
| when: "beforeChildren", | ||||||||||
| }, | ||||||||||
| const ExpandableInfo = forwardRef<IProps, "div">( | ||||||||||
|
||||||||||
| ( | ||||||||||
| { | ||||||||||
| image, | ||||||||||
| title, | ||||||||||
| contentPreview, | ||||||||||
| children, | ||||||||||
| background, | ||||||||||
| forceOpen, | ||||||||||
| className, | ||||||||||
| ...rest | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| const chevronFlip = { | ||||||||||
| collapsed: { | ||||||||||
| rotate: 0, | ||||||||||
| transition: { | ||||||||||
| duration: 0.1, | ||||||||||
| ref | ||||||||||
| ) => { | ||||||||||
| const { isOpen, getButtonProps, getDisclosureProps } = useDisclosure({ | ||||||||||
|
||||||||||
| defaultIsOpen: forceOpen, | ||||||||||
| }) | ||||||||||
|
|
||||||||||
| const chevronFlip = { | ||||||||||
| collapsed: { | ||||||||||
| rotate: 0, | ||||||||||
| transition: { | ||||||||||
| duration: 0.1, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| expanded: { | ||||||||||
| rotate: 180, | ||||||||||
| transition: { | ||||||||||
| duration: 0.4, | ||||||||||
| expanded: { | ||||||||||
| rotate: 180, | ||||||||||
| transition: { | ||||||||||
| duration: 0.4, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| const showHide = { | ||||||||||
| collapsed: { | ||||||||||
| display: "none", | ||||||||||
| }, | ||||||||||
| expanded: { | ||||||||||
| display: "inline-block", | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| const fadeInOut = { | ||||||||||
| collapsed: { | ||||||||||
| opacity: 0, | ||||||||||
| transition: { | ||||||||||
| duration: 0.1, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| expanded: { | ||||||||||
| opacity: 1, | ||||||||||
| transition: { | ||||||||||
| duration: 0.4, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| return ( | ||||||||||
| <Card background={background} className={className}> | ||||||||||
| <Content> | ||||||||||
| {image && <GatsbyImage image={image} alt="" />} | ||||||||||
| <TitleContent> | ||||||||||
| <Question> | ||||||||||
| <Header> | ||||||||||
| <Title>{title}</Title> | ||||||||||
| </Header> | ||||||||||
| <TextPreview>{contentPreview}</TextPreview> | ||||||||||
| </Question> | ||||||||||
| </TitleContent> | ||||||||||
| {!forceOpen && ( | ||||||||||
| <ButtonContainer | ||||||||||
| variants={chevronFlip} | ||||||||||
| animate={isVisible ? "expanded" : "collapsed"} | ||||||||||
| initial={false} | ||||||||||
| onClick={() => setIsVisible(!isVisible)} | ||||||||||
| > | ||||||||||
| <Icon name="chevronDown" size="36" /> | ||||||||||
| </ButtonContainer> | ||||||||||
| )} | ||||||||||
| </Content> | ||||||||||
| <motion.div | ||||||||||
| variants={expandCollapse} | ||||||||||
| animate={isVisible ? "expanded" : "collapsed"} | ||||||||||
| initial={false} | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const animateToggle = isOpen ? "expanded" : "collapsed" | ||||||||||
|
|
||||||||||
| return ( | ||||||||||
| <VStack | ||||||||||
| border="1px solid" | ||||||||||
| borderColor="border" | ||||||||||
| borderRadius="2px" | ||||||||||
| p="1.5rem" | ||||||||||
| mb="1rem" | ||||||||||
|
||||||||||
| p="1.5rem" | |
| mb="1rem" | |
| p={6} | |
| mb={4} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bad habit of mine. I'll be sure to keep this in mind for future changes and any other existing PRs! 😄
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Transition effect simplified to the single Collapse component, as all the fade-in/out effects are apart of the collapse transition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼