Skip to content

Commit 1f8058d

Browse files
authored
Merge pull request #14017 from kushagrasarathe/migrateExpandableCard
feat: migrate ExpandableCard component to shadcn
2 parents 5b37201 + c0f29ed commit 1f8058d

File tree

2 files changed

+45
-101
lines changed

2 files changed

+45
-101
lines changed

src/components/ExpandableCard.tsx

Lines changed: 38 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import { type ReactNode, useState } from "react"
1+
import React, { type ReactNode, useState } from "react"
22
import { useTranslation } from "next-i18next"
3-
import {
4-
Accordion,
5-
AccordionButton,
6-
AccordionItem,
7-
AccordionPanel,
8-
Box,
9-
Heading,
10-
type Icon as ChakraIcon,
11-
} from "@chakra-ui/react"
123

13-
import Text from "@/components/OldText"
4+
import { Flex, HStack, VStack } from "@/components/ui/flex"
145

156
import { trackCustomEvent } from "@/lib/utils/matomo"
167

8+
import {
9+
Accordion,
10+
AccordionContent,
11+
AccordionItem,
12+
AccordionTrigger,
13+
} from "../../tailwind/ui/accordion"
14+
1715
export type ExpandableCardProps = {
1816
children?: ReactNode
1917
contentPreview?: ReactNode
2018
title: ReactNode
21-
svg?: typeof ChakraIcon
19+
svg?: React.ElementType
2220
eventAction?: string
2321
eventCategory?: string
2422
eventName?: string
@@ -54,96 +52,38 @@ const ExpandableCard = ({
5452
}
5553

5654
return (
57-
<Accordion
58-
border="1px"
59-
borderColor="border"
60-
borderRadius="sm"
61-
display="flex"
62-
flexDirection="column"
63-
marginBottom="4"
64-
cursor="pointer"
65-
_hover={{
66-
backgroundColor: "ednBackground",
67-
}}
68-
index={isVisible ? [0] : []}
69-
>
70-
<AccordionItem borderTopWidth="0" flex="1">
71-
<Heading as="h3" m={0}>
72-
<AccordionButton
73-
width="100%"
74-
px="6"
75-
py="6"
76-
flex="1"
55+
<>
56+
<Accordion type="single" collapsible className="mb-4">
57+
<AccordionItem
58+
value="item-1"
59+
className="rounded-sm border hover:bg-background-highlight"
60+
>
61+
<AccordionTrigger
62+
hideIcon
7763
onClick={onClick}
78-
_hover={{
79-
backgroundColor: "ednBackground",
80-
}}
81-
_expanded={{
82-
backgroundColor: "transparent",
83-
}}
64+
className="w-full p-6 transition-colors hover:bg-background-highlight hover:text-black md:p-6 dark:hover:text-white [&[data-state=open]]:bg-transparent [&[data-state=open]]:text-black dark:[&[data-state=open]]:text-white"
8465
>
85-
<Box
86-
display="flex"
87-
width="100%"
88-
alignItems="center"
89-
flexDir={{ base: "column", sm: "row" }}
90-
textAlign="start"
91-
>
92-
<Box marginBottom={{ base: 2, sm: 0 }} me={{ base: 0, sm: 4 }}>
93-
<Box
94-
display="flex"
95-
alignItems="center"
96-
sx={{
97-
svg: { me: "1.5rem" },
98-
}}
99-
my="4"
100-
>
101-
{!!Svg && <Svg />}
102-
<Text fontSize="xl" fontWeight="semibold" flex="1" m="0">
103-
{title}
104-
</Text>
105-
</Box>
106-
<Text
107-
fontSize="sm"
108-
color="text200"
109-
marginBottom="0"
110-
width="fit-content"
111-
>
66+
<Flex className="w-full flex-col items-center text-left sm:flex-row">
67+
<VStack className="items-center md:items-start">
68+
<HStack className="mb-2 mt-4">
69+
{Svg && <Svg className="mr-6" />}
70+
<h3 className="text-xl font-semibold">{title}</h3>
71+
</HStack>
72+
<p className="w-fit text-sm text-body-medium">
11273
{contentPreview}
113-
</Text>
114-
</Box>
115-
<Text
116-
fontSize="md"
117-
color="primary.base"
118-
ms={{ base: undefined, sm: "auto" }}
119-
mt="auto"
120-
mb="auto"
121-
>
74+
</p>
75+
</VStack>
76+
<span className="my-auto text-md text-primary sm:ml-auto">
12277
{t(isVisible ? "less" : "more")}
123-
</Text>
124-
</Box>
125-
</AccordionButton>
126-
</Heading>
127-
<AccordionPanel
128-
p="0"
129-
mt="0"
130-
paddingX="6"
131-
paddingBottom="6"
132-
paddingTop="0"
133-
onClick={onClick}
134-
>
135-
<Box
136-
fontSize="md"
137-
color="text"
138-
paddingTop="6"
139-
borderTop="1px solid"
140-
borderColor="border"
141-
>
142-
{children}
143-
</Box>
144-
</AccordionPanel>
145-
</AccordionItem>
146-
</Accordion>
78+
</span>
79+
</Flex>
80+
</AccordionTrigger>
81+
<AccordionContent className="p-6 pt-0 md:p-6 md:pt-0">
82+
<div className="border-t pt-6 text-md text-body">{children}</div>
83+
</AccordionContent>
84+
</AccordionItem>
85+
</Accordion>
86+
</>
14787
)
14888
}
14989

tailwind/ui/accordion.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ AccordionItem.displayName = "AccordionItem"
1616

1717
const AccordionTrigger = React.forwardRef<
1818
React.ElementRef<typeof AccordionPrimitive.Trigger>,
19-
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Trigger>
20-
>(({ className, children, ...props }, ref) => (
19+
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Trigger> & {
20+
hideIcon?: boolean
21+
}
22+
>(({ className, children, hideIcon = false, ...props }, ref) => (
2123
<AccordionPrimitive.Header className="flex">
2224
<AccordionPrimitive.Trigger
2325
ref={ref}
@@ -28,7 +30,9 @@ const AccordionTrigger = React.forwardRef<
2830
{...props}
2931
>
3032
{children}
31-
<MdChevronRight className="size-[1em] shrink-0 text-2xl transition-transform duration-200" />
33+
{!hideIcon && (
34+
<MdChevronRight className="size-[1em] shrink-0 text-2xl transition-transform duration-200" />
35+
)}
3236
</AccordionPrimitive.Trigger>
3337
</AccordionPrimitive.Header>
3438
))

0 commit comments

Comments
 (0)