Skip to content

Commit e7d506f

Browse files
authored
Add hiveMdxComponents for use in Hive-rebranding themed websites migrated to Nextra 4 (#1977)
1 parent 05d3623 commit e7d506f

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed

.changeset/tidy-hotels-wave.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@theguild/components': minor
3+
---
4+
5+
Add hiveMdxComponents for use in Hive-rebranding themed websites
6+
7+
## Usage
8+
9+
```js file=mdx-components.js
10+
export { useHiveMDXComponents } from '@theguild/components/server';
11+
import { WebsiteSpecificComponent } from './components/WebsiteSpecificComponent';
12+
13+
export const useMDXComponents = components => {
14+
return useHiveMDXComponents({
15+
...components,
16+
WebsiteSpecificComponent,
17+
});
18+
};
19+
```

packages/components/src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Must be in `server` folder because can contain server-only components or imports Node.js builtin
2-
export { useMDXComponents } from './mdx-components.js';
2+
export * from './mdx-components/index.js';
33
// Must be in `server` folder because contains import of `useMDXComponents`
44
export { MDXRemote } from 'nextra/mdx-remote';
55

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { MDXComponents } from 'nextra/mdx-components';
2+
import { useMDXComponents } from './mdx-components';
3+
import { MDXLink } from './mdx-link';
4+
5+
/**
6+
* MDX components used in Hive-rebranded websites.
7+
*/
8+
export const useHiveMDXComponents = (components?: MDXComponents): MDXComponents =>
9+
useMDXComponents({
10+
a: MDXLink,
11+
...components,
12+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './mdx-components';
2+
export * from './hive-mdx-components';
3+
4+
export * from './mdx-link';

packages/components/src/server/mdx-components.tsx renamed to packages/components/src/server/mdx-components/mdx-components.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const docsComponents = getDocsMDXComponents({
1515
await fs.access(filePath);
1616
} catch (error) {
1717
const relativePath = path.relative(process.cwd(), filePath);
18-
if ((error as any).code === 'ENOENT') {
18+
if (typeof error === 'object' && error && 'code' in error && error.code === 'ENOENT') {
1919
throw new Error(`File doesn't exist: ${relativePath}`);
2020
}
2121
throw new Error(`Error checking file: ${relativePath}`);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Meta, StoryObj } from '@storybook/react';
2+
import { hiveThemeDecorator } from '../../../../../.storybook/hive-theme-decorator';
3+
import { MDXLink, MDXLinkProps } from './mdx-link';
4+
5+
export default {
6+
title: 'Components/MDXLink',
7+
component: MDXLink,
8+
decorators: [hiveThemeDecorator],
9+
parameters: {
10+
padding: true,
11+
},
12+
} satisfies Meta<MDXLinkProps>;
13+
14+
export const Default: StoryObj<MDXLinkProps> = {
15+
args: {
16+
href: 'https://the-guild.dev/graphql/stitching',
17+
children: 'Schema stitching',
18+
},
19+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { forwardRef } from 'react';
2+
import { cn } from '../../cn';
3+
import { Anchor } from '../../components/anchor';
4+
5+
export interface MDXLinkProps
6+
extends Omit<React.ComponentProps<typeof Anchor>, 'href' | 'children'> {
7+
href?: string;
8+
children?: React.ReactNode;
9+
}
10+
11+
export const MDXLink = forwardRef<HTMLAnchorElement, MDXLinkProps>(
12+
({ className, href, children, ...rest }, ref) => {
13+
return (
14+
<Anchor
15+
ref={ref}
16+
// we remove `text-underline-position` from default Nextra link styles, because Neue Montreal font
17+
// has a different underline position than system fonts, and it looks bad in Safari.
18+
className={cn(
19+
'hive-focus -mx-1 -my-0.5 rounded px-1 py-0.5 font-medium text-blue-700 underline underline-offset-2 hover:no-underline focus-visible:no-underline focus-visible:ring-current focus-visible:ring-offset-blue-200 dark:text-primary/90 dark:focus-visible:ring-primary/50',
20+
className,
21+
)}
22+
href={href || ''}
23+
{...rest}
24+
>
25+
{children}
26+
</Anchor>
27+
);
28+
},
29+
);
30+
31+
MDXLink.displayName = 'MDXLink';

0 commit comments

Comments
 (0)