Skip to content

Commit 3d7bd1a

Browse files
committed
Add highlight to search
1 parent 553d754 commit 3d7bd1a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

apps/landing/src/components/SearchModal/SearchContent.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Box, Center, css, Flex, Text, VStack } from '@devup-ui/react'
33
import Link from 'next/link'
44
import { useSearchParams } from 'next/navigation'
5-
import { Fragment, useEffect, useState } from 'react'
5+
import { Fragment, useEffect, useMemo, useState } from 'react'
66

77
import { URL_PREFIX } from '../../constants'
88

@@ -38,6 +38,7 @@ export function SearchContent() {
3838
)
3939
}
4040
}, [query])
41+
const reg = useMemo(() => new RegExp(`(${query})`, 'gi'), [query])
4142
if (!query) return
4243
const inner = data ? (
4344
<>
@@ -58,8 +59,22 @@ export function SearchContent() {
5859
p="10px"
5960
>
6061
<Text typography="textSbold">{item.title}</Text>
61-
<Text color="$search" typography="caption">
62-
{item.text}
62+
<Text color="$caption" typography="caption">
63+
{item.text
64+
.substring(0, 100)
65+
.split(reg)
66+
.map((part, idx) =>
67+
part.toLowerCase() === query.toLowerCase() ? (
68+
<Text key={idx} color="$search" fontWeight="bold">
69+
{part}
70+
</Text>
71+
) : (
72+
<Text key={idx} as="span">
73+
{part}
74+
</Text>
75+
),
76+
)}
77+
...
6378
</Text>
6479
</VStack>
6580
</Link>

0 commit comments

Comments
 (0)