Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/perfect-candles-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lg-chat/rich-links': minor
'@lg-chat/message': minor
---

Add the onLinkClick prop to support callbacks whenever a rich link is clicked
3 changes: 3 additions & 0 deletions chat/message/src/Message/Message.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ WithRichLinks.args = {
messageBody: MongoText,
avatar: <Avatar variant="mongo" />,
children: <MessageFeedback />,
onLinkClick: props => {
alert(`Link clicked: ${props.href}`);
},
links: [
{
href: 'https://mongodb.design',
Expand Down
2 changes: 2 additions & 0 deletions chat/message/src/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const Message = forwardRef(
componentOverrides,
links,
linksHeading,
onLinkClick,
markdownProps,
verified,
darkMode: darkModeProp,
Expand Down Expand Up @@ -146,6 +147,7 @@ export const Message = forwardRef(
as={componentOverrides?.MessageLinks ?? MessageLinks}
headingText={linksHeading}
links={links}
onLinkClick={onLinkClick}
/>
) : null}
{children}
Expand Down
5 changes: 5 additions & 0 deletions chat/message/src/Message/Message.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export interface MessageProps
* The heading text to display for the links section.
*/
linksHeading?: string;

/**
* A callback function that is called when any link is clicked.
*/
onLinkClick?: RichLinkProps['onLinkClick'];
}

export interface VerificationInfo {
Expand Down
3 changes: 2 additions & 1 deletion chat/message/src/MessageLinks/MessageLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export function MessageLinks({
darkMode: darkModeProp,
headingText = 'Related Resources',
links,
onLinkClick,
...divProps
}: MessageLinksProps) {
const { theme } = useDarkMode(darkModeProp);
return (
<div className={cx(baseStyles)} {...divProps}>
<hr className={cx(dividingLineStyles[theme])} />
<Subtitle className={cx(linksHeadingStyles)}>{headingText}</Subtitle>
<RichLinksArea links={links} />
<RichLinksArea links={links} onLinkClick={onLinkClick} />
</div>
);
}
Expand Down
5 changes: 5 additions & 0 deletions chat/message/src/MessageLinks/MessageLinks.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export interface MessageLinksProps
*/
headingText?: string;

/**
* A callback function that is called when any link is clicked.
*/
onLinkClick?: RichLinkProps['onLinkClick'];

/**
* An list of link data to render in the links section.
*/
Expand Down
10 changes: 10 additions & 0 deletions chat/rich-links/src/RichLink/RichLink.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,13 @@ ExplicitlyUndefinedVariant.args = {
children: 'Example Link (variant undefined)',
variant: undefined,
};

export const ClickHandler = Template.bind({});
ClickHandler.args = {
href: 'https://example.com',
children: 'Example Link (variant undefined)',
variant: undefined,
onLinkClick: props => {
alert(`Link clicked: ${props.href}`);
},
};
3 changes: 2 additions & 1 deletion chat/rich-links/src/RichLink/RichLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { RichLinkBadge } from './RichLinkBadge';
import { richLinkVariants } from './richLinkVariants';

export const RichLink = forwardRef<HTMLAnchorElement, RichLinkProps>(
({ darkMode: darkModeProp, ...props }, ref) => {
({ darkMode: darkModeProp, onLinkClick, ...props }, ref) => {
const { darkMode, theme } = useDarkMode(darkModeProp);

const richLinkVariantProps =
Expand Down Expand Up @@ -64,6 +64,7 @@ export const RichLink = forwardRef<HTMLAnchorElement, RichLinkProps>(
[imageBackgroundStyles(imageUrl ?? '')]: showImageBackground,
})}
{...conditionalProps}
onClick={() => onLinkClick?.(props)}
>
<Body className={richLinkTextClassName} darkMode={darkMode}>
{children}
Expand Down
5 changes: 5 additions & 0 deletions chat/rich-links/src/RichLink/RichLink.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export interface BaseRichLinkProps
* A URL for the background image of the rich link
*/
imageUrl?: string;

/**
* A callback function that is called when the link is clicked.
*/
onLinkClick?: (props: Omit<BaseRichLinkProps, 'onLinkClick'>) => void;
}

export interface RichLinkVariantControlProps {
Expand Down
9 changes: 8 additions & 1 deletion chat/rich-links/src/RichLinksArea/RichLinksArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ import { type RichLinksAreaProps } from './RichLinksArea.types';
export function RichLinksArea({
links,
darkMode: darkModeProp,
onLinkClick,
...props
}: RichLinksAreaProps) {
const { darkMode } = useDarkMode(darkModeProp);
return (
<LeafygreenProvider darkMode={darkMode}>
<div className={cx(baseStyles)} {...props}>
{links.map(richLinkProps => {
return <RichLink key={richLinkProps.href} {...richLinkProps} />;
return (
<RichLink
key={richLinkProps.href}
onClick={() => onLinkClick?.(richLinkProps)}
{...richLinkProps}
/>
);
})}
</div>
</LeafygreenProvider>
Expand Down
5 changes: 5 additions & 0 deletions chat/rich-links/src/RichLinksArea/RichLinksArea.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ export interface RichLinksAreaProps
extends HTMLElementProps<'div', never>,
DarkModeProps {
links: Array<RichLinkProps>;

/**
* A callback function that is called when any link is clicked.
*/
onLinkClick?: RichLinkProps['onLinkClick'];
}