Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-docs: DocsPage Heading and Subheading anchor links #9060

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 15 additions & 2 deletions addons/docs/src/blocks/Heading.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import React, { FunctionComponent } from 'react';
import { H2 } from '@storybook/components/html';
import { HeaderMdx } from './mdx';

interface HeadingProps {
export interface HeadingProps {
children: JSX.Element | string;
disableAnchor?: boolean;
}
export const Heading: FunctionComponent<HeadingProps> = ({ children }) => <H2>{children}</H2>;

export const Heading: FunctionComponent<HeadingProps> = ({ children, disableAnchor }) => {
if (disableAnchor || typeof children !== 'string') {
return <H2>{children}</H2>;
}
const tagID = children.toLowerCase().replace(/[^a-z0-9]/gi, '-');
return (
<HeaderMdx as="h2" id={tagID}>
{children}
</HeaderMdx>
);
};
17 changes: 13 additions & 4 deletions addons/docs/src/blocks/Subheading.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import React, { FunctionComponent } from 'react';
import { H3 } from '@storybook/components/html';
import { HeaderMdx } from './mdx';
import { HeadingProps } from './Heading';

interface SubheadingProps {
children: JSX.Element | string;
}
export const Subheading: FunctionComponent<SubheadingProps> = ({ children }) => <H3>{children}</H3>;
export const Subheading: FunctionComponent<HeadingProps> = ({ children, disableAnchor }) => {
if (disableAnchor || typeof children !== 'string') {
return <H3>{children}</H3>;
}
const tagID = children.toLowerCase().replace(/[^a-z0-9]/gi, '-');
return (
<HeaderMdx as="h3" id={tagID}>
{children}
</HeaderMdx>
);
};
2 changes: 1 addition & 1 deletion addons/docs/src/blocks/mdx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ interface HeaderMdxProps {
id: string;
}

const HeaderMdx: FC<HeaderMdxProps> = props => {
export const HeaderMdx: FC<HeaderMdxProps> = props => {
const { as, id, children, ...rest } = props;

// An id should have been added on every header by the "remark-slug" plugin.
Expand Down