Skip to content

Commit b70635d

Browse files
committed
wip simplified mdxComponents
1 parent dd0d435 commit b70635d

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.heading {
2+
position: relative;
3+
4+
&:hover {
5+
& a {
6+
opacity: 1;
7+
}
8+
}
9+
& a {
10+
position: absolute;
11+
height: 20px;
12+
width: 20px;
13+
left: 0;
14+
top: 50%;
15+
transform: translateY(-50%);
16+
opacity: 0;
17+
color: inherit;
18+
19+
& span {
20+
display: block;
21+
width: 100%;
22+
height: 100%;
23+
&::after {
24+
--link-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='currentColor' fill-rule='evenodd' d='M14.909 5.152a2.25 2.25 0 0 1 3.182 0l.757.757a2.25 2.25 0 0 1 0 3.182l-4.964 4.965a1.25 1.25 0 0 1-1.768 0L11.03 12.97a.75.75 0 1 0-1.06 1.06l1.085 1.086a2.75 2.75 0 0 0 3.89 0l4.964-4.964a3.75 3.75 0 0 0 0-5.304l-.757-.757-.53.53.53-.53a3.75 3.75 0 0 0-5.304 0l-.878.879a.75.75 0 0 0 1.06 1.06zm-1.964 3.732a2.75 2.75 0 0 0-3.89 0l-4.964 4.964a3.75 3.75 0 0 0 0 5.304l.513-.514-.513.514.757.757a3.75 3.75 0 0 0 5.304 0l.878-.879a.75.75 0 0 0-1.06-1.06l-.879.878a2.25 2.25 0 0 1-3.182 0l-.757-.757a2.25 2.25 0 0 1 0-3.182l4.964-4.965a1.25 1.25 0 0 1 1.768 0l1.086 1.086a.75.75 0 0 0 1.06-1.06z' clip-rule='evenodd'%3E%3C/path%3E%3C/svg%3E");
25+
26+
content: '';
27+
width: 100%;
28+
height: 100%;
29+
display: block;
30+
mask: center / contain no-repeat var(--link-icon);
31+
background: currentcolor;
32+
}
33+
}
34+
}
35+
}
36+
37+
.tableWrapper {
38+
overflow-x: auto;
39+
max-width: 100%;
40+
41+
@media (max-width: 768px) {
42+
max-width: 340px;
43+
}
44+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import {
2+
Card,
3+
Details,
4+
DetailsContent,
5+
DetailsSummary,
6+
Heading,
7+
Link,
8+
ListOrdered,
9+
ListUnordered,
10+
Paragraph,
11+
type ParagraphProps,
12+
Table,
13+
TableBody,
14+
TableCell,
15+
TableFoot,
16+
TableHead,
17+
TableHeaderCell,
18+
type TableProps,
19+
TableRow,
20+
} from '@digdir/designsystemet-react';
21+
import { getMDXComponent } from 'mdx-bundler/dist/client';
22+
import { type ComponentType, type JSX, useMemo } from 'react';
23+
import { useTranslation } from 'react-i18next';
24+
import { Link as RRLink } from 'react-router';
25+
import { CodeBlock } from '../code-block/code-block';
26+
import classes from './mdx-components.module.css';
27+
28+
const defaultComponents = {
29+
Details,
30+
DetailsContent,
31+
DetailsSummary,
32+
Card,
33+
Table: (props: TableProps) => (
34+
<div className={classes.tableWrapper}>
35+
<Table {...props} />
36+
</div>
37+
),
38+
TableHead,
39+
TableRow,
40+
TableHeaderCell,
41+
TableBody,
42+
TableFoot,
43+
TableCell,
44+
h1: (props: JSX.IntrinsicElements['h1']) => (
45+
<Heading className={classes.heading} level={1} data-size='xl' {...props} />
46+
),
47+
h2: (props: JSX.IntrinsicElements['h2']) => (
48+
<Heading className={classes.heading} level={2} data-size='md' {...props} />
49+
),
50+
h3: (props: JSX.IntrinsicElements['h3']) => (
51+
<Heading className={classes.heading} level={3} data-size='sm' {...props} />
52+
),
53+
h4: (props: JSX.IntrinsicElements['h4']) => (
54+
<Heading className={classes.heading} level={4} data-size='xs' {...props} />
55+
),
56+
h5: (props: JSX.IntrinsicElements['h5']) => (
57+
<Heading className={classes.heading} level={5} data-size='xs' {...props} />
58+
),
59+
h6: (props: JSX.IntrinsicElements['h6']) => (
60+
<Heading className={classes.heading} level={6} data-size='xs' {...props} />
61+
),
62+
ol: (props: JSX.IntrinsicElements['ol']) => <ListOrdered {...props} />,
63+
ul: (props: JSX.IntrinsicElements['ul']) => <ListUnordered {...props} />,
64+
p: (props: ParagraphProps) => <Paragraph {...props} />,
65+
Link: ({ href, ...props }: JSX.IntrinsicElements['a']) => (
66+
<Link {...props} asChild>
67+
<RRLink to={href || ''}>{props.children}</RRLink>
68+
</Link>
69+
),
70+
a: ({ href, ...props }: JSX.IntrinsicElements['a']) => (
71+
<Link {...props} asChild>
72+
<RRLink to={href || ''}>{props.children}</RRLink>
73+
</Link>
74+
),
75+
pre: ({
76+
children: {
77+
props: { children = '', className = '' },
78+
},
79+
}) => {
80+
return (
81+
<CodeBlock language={className.replace('language-', '')}>
82+
{children}
83+
</CodeBlock>
84+
);
85+
},
86+
table: (props: TableProps) => (
87+
<div className={classes.tableWrapper}>
88+
<Table data-color='neutral' border zebra {...props} />
89+
</div>
90+
),
91+
};
92+
93+
export const MDXComponents = ({
94+
components,
95+
code,
96+
}: {
97+
components?: Record<string, ComponentType<unknown>>;
98+
code?: string;
99+
}) => {
100+
const { t } = useTranslation();
101+
const Component = useMemo(() => {
102+
if (!code) return null;
103+
return getMDXComponent(code);
104+
}, [code]);
105+
106+
return (
107+
<>
108+
{Component ? (
109+
<Component
110+
components={{
111+
...defaultComponents,
112+
...components,
113+
}}
114+
/>
115+
) : (
116+
t('mdx.error.loading')
117+
)}
118+
</>
119+
);
120+
};

0 commit comments

Comments
 (0)