Skip to content

Commit

Permalink
Merge pull request #93 from storybookjs/charles/sb-1442-ux-inline-sni…
Browse files Browse the repository at this point in the history
…ppets

Add support for titles in code
  • Loading branch information
cdedreuille committed Jun 19, 2024
2 parents c50de08 + dcd2b0a commit 34ceb62
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
23 changes: 23 additions & 0 deletions apps/frontpage/components/docs/mdx/figcaption.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client';

import { useContext, useEffect, type FC, type ReactNode } from 'react';
import { FigureContext } from './figure-provider';

interface PreProps {
children?: ReactNode;
}

export const Figcaption: FC<PreProps> = (props) => {
const context = useContext(FigureContext);
const { setTitle } = context || {};

useEffect(() => {
setTitle && setTitle(props.children as string);
}, []);

return (
<figcaption {...props} className="hidden">
{props.children}
</figcaption>
);
};
28 changes: 28 additions & 0 deletions apps/frontpage/components/docs/mdx/figure-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use client';

import type { ReactNode } from 'react';
import { createContext, useContext, useState } from 'react';

export interface FigureContextProps {
title: string;
setTitle: (id: string) => void;
}

export const FigureContext = createContext<FigureContextProps | undefined>(
undefined,
);

export function FigureProvider({ children }: { children: ReactNode }) {
const [title, setTitle] = useState<string>('');

return (
<FigureContext.Provider
value={{
title,
setTitle,
}}
>
{children}
</FigureContext.Provider>
);
}
14 changes: 14 additions & 0 deletions apps/frontpage/components/docs/mdx/figure.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { FC, ReactNode } from 'react';
import { FigureProvider } from './figure-provider';

interface PreProps {
children?: ReactNode;
}

export const Figure: FC<PreProps> = (props) => {
return (
<FigureProvider>
<figure {...props}>{props.children}</figure>
</FigureProvider>
);
};
2 changes: 2 additions & 0 deletions apps/frontpage/components/docs/mdx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export { Table } from './table';
export { Th } from './th';
export { Tr } from './tr';
export { Td } from './td';
export { Figure } from './figure';
export { Figcaption } from './figcaption';
8 changes: 8 additions & 0 deletions apps/frontpage/components/docs/mdx/pre.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { Meta, StoryObj } from '@storybook/react';
import { Pre } from './pre';
import { FigureProvider } from './figure-provider';

const meta = {
title: 'Pre',
component: Pre,
decorators: [
(Story) => (
<FigureProvider>
<Story />
</FigureProvider>
),
],
} satisfies Meta<typeof Pre>;

export default meta;
Expand Down
10 changes: 8 additions & 2 deletions apps/frontpage/components/docs/mdx/pre.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type { FC, ReactNode } from 'react';
'use client';

import { useContext, type FC, type ReactNode } from 'react';
import { CodeWrapper } from './code-snippets/wrapper';
import { FigureContext } from './figure-provider';

interface PreProps {
children?: ReactNode;
raw?: string;
}

export const Pre: FC<PreProps> = ({ children, raw }) => {
const context = useContext(FigureContext);
const { title } = context || {};

return (
<CodeWrapper copy={raw}>
<CodeWrapper copy={raw} title={title || ''}>
<pre>{children}</pre>
</CodeWrapper>
);
Expand Down
4 changes: 4 additions & 0 deletions apps/frontpage/lib/get-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
Tr,
Td,
OrderedList,
Figure,
Figcaption,
} from '../components/docs/mdx';
import { generateDocsTree } from './get-tree';
import { rehypePrettyCodeOptions } from './rehype-pretty-code-options';
Expand Down Expand Up @@ -150,6 +152,8 @@ export const getPageData = async (
th: Th,
tr: Tr,
td: Td,
figure: Figure,
figcaption: Figcaption,
img: (props) => <Img activeVersion={activeVersion.id} {...props} />,
Video: (props) => <Video activeVersion={activeVersion.id} {...props} />,
CodeSnippets: (props) => (
Expand Down

0 comments on commit 34ceb62

Please sign in to comment.