Raw
1 import React from "react"
2 import clsx from "clsx"
3 import { Badge } from "@/components/Badge"
4 import { BorderedIcon } from "@/components/BorderedIcon"
5 import { Link } from "@/components/Link"
6 import { ArrowUpRightOnBox, TriangleRightMini } from "@medusajs/icons"
7 import { CardProps } from "../../.."
8 import { useIsExternalLink } from "../../../.."
9
10 export const CardDefaultLayout = ({
11 icon,
12 image,
13 title,
14 text,
15 href,
16 className,
17 contentClassName,
18 iconClassName,
19 children,
20 badge,
21 rightIcon: RightIconComponent,
22 highlightText = [],
23 onClick,
24 }: CardProps) => {
25 const isExternal = useIsExternalLink({ href })
26
27 const getHighlightedText = (textToHighlight: string) => {
28 if (!highlightText.length) {
29 return textToHighlight
30 }
31
32 const parts = textToHighlight.split(
33 new RegExp(`(${highlightText.join("|")})`, "gi")
34 )
35 return parts.map((part, index) => {
36 const isHighlighted = highlightText.some((highlight) => {
37 return part.toLowerCase() === highlight.toLowerCase()
38 })
39 return isHighlighted ? (
40 <span
41 key={index}
42 className="bg-medusa-tag-blue-bg px-px rounded-s-docs_xxs"
43 data-testid="highlight-text"
44 >
45 {part}
46 </span>
47 ) : (
48 part
49 )
50 })
51 }
52
53 return (
54 <div
55 className={clsx(
56 "bg-medusa-bg-component w-full rounded-docs_DEFAULT",
57 "shadow-elevation-card-rest dark:shadow-elevation-card-rest-dark",
58 "py-docs_0.5 px-docs_0.75 relative",
59 "flex justify-start items-center gap-docs_0.75 transition-shadow",
60 href &&
61 "hover:shadow-elevation-card-hover dark:hover:shadow-elevation-card-hover-dark",
62 className
63 )}
64 >
65 {icon && (
66 <BorderedIcon
67 wrapperClassName={clsx(
68 "p-[4.5px] bg-medusa-bg-component-hover",
69 iconClassName
70 )}
71 IconComponent={icon}
72 />
73 )}
74 {image && (
75 <BorderedIcon
76 wrapperClassName={clsx("bg-medusa-bg-base", iconClassName)}
77 icon={image}
78 />
79 )}
80 <div
81 className={clsx("flex flex-col flex-1 overflow-auto", contentClassName)}
82 >
83 {title && (
84 <div
85 className="text-small-plus text-medusa-fg-base truncate"
86 data-testid="title"
87 >
88 {getHighlightedText(title)}
89 </div>
90 )}
91 {text && (
92 <span
93 className="text-small-plus text-medusa-fg-subtle"
94 data-testid="text"
95 >
96 {getHighlightedText(text)}
97 </span>
98 )}
99 {children}
100 </div>
101 {badge && <Badge {...badge} />}
102 <span className="text-medusa-fg-subtle">
103 {RightIconComponent && <RightIconComponent />}
104 {!RightIconComponent && isExternal && (
105 <ArrowUpRightOnBox data-testid="external-icon" />
106 )}
107 {!RightIconComponent && !isExternal && (
108 <TriangleRightMini data-testid="internal-icon" />
109 )}
110 </span>
111
112 {href && (
113 <Link
114 href={href}
115 className="absolute left-0 top-0 h-full w-full rounded"
116 prefetch={false}
117 rel={isExternal ? "noopener noreferrer" : undefined}
118 target={isExternal ? "_blank" : undefined}
119 aria-label={title}
120 onClick={onClick}
121 />
122 )}
123 </div>
124 )
125 }