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

Docs: using targetted styles, not components to style MDX #19958

Merged
merged 13 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.formatOnSave": true
},
"prettier.ignorePath": "./code/.prettierignore"
}
1 change: 1 addition & 0 deletions code/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.mdx
2 changes: 0 additions & 2 deletions code/addons/docs/src/DocsRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom';
import type { Renderer, Parameters, DocsContextProps, DocsRenderFunction } from '@storybook/types';
import { components as htmlComponents } from '@storybook/components';
import { Docs, CodeOrSourceMdx, AnchorMdx, HeadersMdx } from '@storybook/blocks';
import { MDXProvider } from '@mdx-js/react';

// TS doesn't like that we export a component with types that it doesn't know about (TS4203)
export const defaultComponents: Record<string, any> = {
...htmlComponents,
code: CodeOrSourceMdx,
a: AnchorMdx,
...HeadersMdx,
Expand Down
1 change: 1 addition & 0 deletions code/ui/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { StorybookConfig } from '../../frameworks/react-vite/dist';
const isBlocksOnly = process.env.STORYBOOK_BLOCKS_ONLY === 'true';

const allStories = [
'../../lib/cli/rendererAssets/common/Introduction.stories.mdx',
{
directory: '../manager/src',
titlePrefix: '@storybook-manager',
Expand Down
4 changes: 3 additions & 1 deletion code/ui/blocks/src/blocks/Anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export interface AnchorProps {
}

export const Anchor: FC<AnchorProps> = ({ storyId, children }) => (
<div id={anchorBlockIdFromId(storyId)}>{children}</div>
<div id={anchorBlockIdFromId(storyId)} className="sb-anchor">
{children}
</div>
);
2 changes: 1 addition & 1 deletion code/ui/blocks/src/blocks/Story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const Story: FC<StoryProps> = (props) => {
// FIXME: height/style/etc. lifted from PureStory
const { height } = storyProps;
return (
<div id={storyBlockIdFromId(story.id)}>
<div id={storyBlockIdFromId(story.id)} className="sb-story">
{height ? (
<style>{`#story--${story.id} { min-height: ${height}; transform: translateZ(0); overflow: auto }`}</style>
) : null}
Expand Down
39 changes: 39 additions & 0 deletions code/ui/blocks/src/blocks/Unstyled.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Unstyled } from './Unstyled.tsx';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonniebigodes if you have feedback for this documentation, let me know.

import StoriesMeta, {Undefined} from '../controls/Boolean.stories';
import { Story } from '@storybook/blocks';
import { Meta } from '@storybook/blocks';

<Story of={Undefined} meta={StoriesMeta}/>

# The Unstyled Block

By default most elements in docs have a few default styles applied to ensure the docs look good. This is achieved by applying default styles to most elements like `h1`, `p`, etc..
However sometimes you might want some of your content to not have these styles applied, this is where the `Unstyled` block is useful. Wrap any content you want in the `Unstyled` block to remove the default styles:

```md
import { Unstyled } from '@storybook/blocks';

> This block quote will be styled

... and so will this paragraph.

<Unstyled>
> This block quote will not be styled

... neither will this paragraph, nor the following component:
<MyCustomComponent />
</Unstyled>
```

Yields:

> This block quote will be styled

... and so will this paragraph.

<Unstyled>
> This block quote will not be styled

... neither will this paragraph, nor the following component:
</Unstyled>

3 changes: 3 additions & 0 deletions code/ui/blocks/src/blocks/Unstyled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export const Unstyled: React.FC = (props) => <div {...props} className="sb-unstyled" />;
5 changes: 1 addition & 4 deletions code/ui/blocks/src/components/Description.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FC } from 'react';
import React from 'react';
import Markdown from 'markdown-to-jsx';
import { components, ResetWrapper } from '@storybook/components';

export interface DescriptionProps {
markdown: string;
Expand All @@ -12,7 +11,5 @@ export interface DescriptionProps {
* components docgen docs.
*/
export const Description: FC<DescriptionProps> = ({ markdown }) => (
<ResetWrapper>
<Markdown options={{ forceBlock: true, overrides: components }}>{markdown}</Markdown>
</ResetWrapper>
<Markdown options={{ forceBlock: true }}>{markdown}</Markdown>
);
26 changes: 26 additions & 0 deletions code/ui/blocks/src/components/DocsPage.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import type { ComponentProps } from 'react';
import React from 'react';
import { Global, css } from '@storybook/theming';
Expand Down Expand Up @@ -95,3 +96,28 @@ export const Markdown = () => (
<Source {...source.JSX.args} />
</DocsPageWrapper>
);

export const Html = {
name: 'HTML',
render: () => (
<DocsPageWrapper>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<a>A tag</a>
<pre>pre tag</pre>
<div>
<div>Div</div>
<a>Nested A tag</a>
</div>
<div className="sb-unstyled" style={{ border: '2px solid red' }}>
JReinhold marked this conversation as resolved.
Show resolved Hide resolved
<h1>Unstyled content</h1>
<h2>Heading 2</h2>
<a>A tag</a>
<div>
<div>Div</div>
<a>Nested A tag</a>
</div>
</div>
</DocsPageWrapper>
),
};
Loading