Skip to content

Commit

Permalink
Merge pull request #622 from dragon-slayer875/master
Browse files Browse the repository at this point in the history
Feat: Separate Markdown Renderer for Tooltips
  • Loading branch information
leecalcote committed May 20, 2024
2 parents 770fb15 + c20b368 commit 0fbd2ca
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/custom/CustomTooltip/customTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { Tooltip, type TooltipProps } from '@mui/material';
import React from 'react';
import { CHARCOAL, WHITE } from '../../theme';
import RenderMarkdown from '../Markdown';
import { RenderMarkdownTooltip } from '../Markdown';

type CustomTooltipProps = {
title: string | React.ReactNode | JSX.Element;
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
children: React.ReactNode;
fontSize?: string;
fontWeight?: number;
variant?: 'standard' | 'small';
} & Omit<TooltipProps, 'title' | 'onClick'>;

function CustomTooltip({
title,
onClick,
placement,
children,
fontSize = '1rem',
fontSize,
fontWeight = 400,
variant = 'small',
...props
}: CustomTooltipProps): JSX.Element {
return (
Expand All @@ -27,10 +29,10 @@ function CustomTooltip({
sx: {
background: CHARCOAL,
color: WHITE,
fontSize: { fontSize },
fontSize: fontSize || (variant === 'standard' ? '1rem' : '0.75rem'),
fontWeight: { fontWeight },
borderRadius: '0.5rem',
padding: '0.9rem'
padding: variant === 'standard' ? '0.9rem' : '0.5rem 0.75rem'
}
},
popper: {
Expand All @@ -39,7 +41,7 @@ function CustomTooltip({
}
}
}}
title={<RenderMarkdown content={typeof title === 'string' ? title : ''} />}
title={<RenderMarkdownTooltip content={typeof title === 'string' ? title : ''} />}
placement={placement}
arrow
onClick={onClick}
Expand Down
32 changes: 30 additions & 2 deletions src/custom/Markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ import {
StyledMarkdownH5,
StyledMarkdownH6,
StyledMarkdownLi,
StyledMarkdownP,
StyledMarkdownTd,
StyledMarkdownTh,
StyledMarkdownTooltipP,
StyledMarkdownUl
} from './style';
export interface RenderMarkdownProps {
content: string;
}

const RenderMarkdown: React.FC<RenderMarkdownProps> = ({ content }) => {
export const RenderMarkdown: React.FC<RenderMarkdownProps> = ({ content }) => {
return (
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={{
p: ({ ...props }) => <StyledMarkdownP>{props.children}</StyledMarkdownP>,
a: ({ ...props }) => <StyledMarkdown>{props.children}</StyledMarkdown>,
h1: ({ ...props }) => <StyledMarkdownH1>{props.children}</StyledMarkdownH1>,
h2: ({ ...props }) => <StyledMarkdownH2>{props.children}</StyledMarkdownH2>,
Expand All @@ -44,4 +47,29 @@ const RenderMarkdown: React.FC<RenderMarkdownProps> = ({ content }) => {
);
};

export default RenderMarkdown;
export const RenderMarkdownTooltip: React.FC<RenderMarkdownProps> = ({ content }) => {
return (
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={{
p: ({ ...props }) => <StyledMarkdownTooltipP>{props.children}</StyledMarkdownTooltipP>,
a: ({ ...props }) => <StyledMarkdown>{props.children}</StyledMarkdown>,
h1: ({ ...props }) => <StyledMarkdownH1>{props.children}</StyledMarkdownH1>,
h2: ({ ...props }) => <StyledMarkdownH2>{props.children}</StyledMarkdownH2>,
h3: ({ ...props }) => <StyledMarkdownH3>{props.children}</StyledMarkdownH3>,
h4: ({ ...props }) => <StyledMarkdownH4>{props.children}</StyledMarkdownH4>,
h5: ({ ...props }) => <StyledMarkdownH5>{props.children}</StyledMarkdownH5>,
h6: ({ ...props }) => <StyledMarkdownH6>{props.children}</StyledMarkdownH6>,
blockquote: ({ ...props }) => (
<StyledMarkdownBlockquote>{props.children}</StyledMarkdownBlockquote>
),
ul: ({ ...props }) => <StyledMarkdownUl>{props.children}</StyledMarkdownUl>,
li: ({ ...props }) => <StyledMarkdownLi>{props.children}</StyledMarkdownLi>,
th: ({ ...props }) => <StyledMarkdownTh>{props.children}</StyledMarkdownTh>,
td: ({ ...props }) => <StyledMarkdownTd>{props.children}</StyledMarkdownTd>
}}
>
{content}
</ReactMarkdown>
);
};
5 changes: 5 additions & 0 deletions src/custom/Markdown/style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export const StyledMarkdownP = styled('p')(({ theme }) => ({
...theme.typography.textB1Regular
}));

export const StyledMarkdownTooltipP = styled('p')(({ theme }) => ({
color: theme.palette.text.default,
marginBlock: '0px'
}));

export const StyledMarkdownH1 = styled('h1')(({ theme }) => ({
color: theme.palette.text.default
}));
Expand Down
2 changes: 1 addition & 1 deletion src/custom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { FlipCard, FlipCardProps } from './FlipCard';
import { useWindowDimensions } from './Helpers/Dimension';
import { useNotificationHandler } from './Helpers/Notification';
import { LearningCard } from './LearningCard';
import RenderMarkdown from './Markdown';
import { RenderMarkdown } from './Markdown';
import { ModalCard } from './ModalCard';
import PopperListener, { IPopperListener } from './PopperListener';
import ResponsiveDataTable, { ResponsiveDataTableProps } from './ResponsiveDataTable';
Expand Down

0 comments on commit 0fbd2ca

Please sign in to comment.