-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sidebar-text-highlight): Improve component to highlight text and …
…use in navigation (#27894)
- Loading branch information
1 parent
dbda5b2
commit d5e5350
Showing
3 changed files
with
33 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
29 changes: 29 additions & 0 deletions
29
frontend/src/layout/navigation-3000/components/SearchHighlight.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interface Props { | ||
string: string | ||
substring: string | ||
className?: string | ||
} | ||
|
||
/** | ||
* Highlight a substring within a string (case-insensitive) | ||
* @param string - the aggregate string to search within (e.g. "Hello, world!") | ||
* @param substring - the substring to search for (e.g. "world") | ||
* @param className - additional classes to apply to the component | ||
*/ | ||
export default function SearchHighlight({ string, substring, className }: Props): JSX.Element { | ||
const parts = string.split(new RegExp(`(${substring})`, 'gi')) | ||
return ( | ||
<div className={`truncate ${className}`}> | ||
{parts.map((part, index) => ( | ||
<span | ||
key={index} | ||
className={`text-xs ${ | ||
part.toLowerCase() === substring.toLowerCase() ? 'bg-primary-highlight bg-opacity-60' : '' | ||
}`} | ||
> | ||
{part} | ||
</span> | ||
))} | ||
</div> | ||
) | ||
} |
This file contains 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