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: Fix style bleeding #21150

Merged
5 changes: 1 addition & 4 deletions code/addons/docs/src/DocsRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ export class DocsRenderer<TRenderer extends Renderer> {
element: HTMLElement,
callback: () => void
): void => {
// Use a random key to force the container to re-render each time we call `renderDocs`
// TODO: do we still need this? It was needed for angular (legacy) inline rendering:
// https://github.com/storybookjs/storybook/pull/16149
const components = {
...defaultComponents,
...docsParameter?.components,
Expand All @@ -33,7 +30,7 @@ export class DocsRenderer<TRenderer extends Renderer> {
import('@mdx-js/react').then(({ MDXProvider }) => {
ReactDOM.render(
<MDXProvider components={components}>
<Docs key={Math.random()} context={context} docsParameter={docsParameter} />
<Docs context={context} docsParameter={docsParameter} />
</MDXProvider>,
element,
callback
Expand Down
10 changes: 4 additions & 6 deletions code/ui/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Channel } from '@storybook/channels';

import { DocsContext } from '@storybook/blocks';

import { DocsContent, DocsWrapper } from '../blocks/src/components';
import { DocsPageWrapper } from '../blocks/src/components';

const { document } = global;

Expand Down Expand Up @@ -150,11 +150,9 @@ export const decorators = [
* Activated with parameters.docsStyles = true
*/ (Story, { parameters: { docsStyles } }) =>
docsStyles ? (
<DocsWrapper className="sbdocs sbdocs-wrapper">
<DocsContent className="sbdocs sbdocs-content">
<Story />
</DocsContent>
</DocsWrapper>
<DocsPageWrapper>
<Story />
</DocsPageWrapper>
) : (
<Story />
),
Expand Down
6 changes: 2 additions & 4 deletions code/ui/blocks/src/blocks/DocsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { global } from '@storybook/global';
import type { ThemeVars } from '@storybook/theming';
import { ThemeProvider, ensure as ensureTheme } from '@storybook/theming';
import type { Renderer } from '@storybook/types';
import { DocsWrapper, DocsContent } from '../components';
import { DocsPageWrapper } from '../components';
import type { DocsContextProps } from './DocsContext';
import { DocsContext } from './DocsContext';
import { SourceContainer } from './SourceContainer';
Expand Down Expand Up @@ -45,9 +45,7 @@ export const DocsContainer: FunctionComponent<DocsContainerProps> = ({
<DocsContext.Provider value={context}>
<SourceContainer channel={context.channel}>
<ThemeProvider theme={ensureTheme(theme)}>
<DocsWrapper className="sbdocs sbdocs-wrapper">
<DocsContent className="sbdocs sbdocs-content">{children}</DocsContent>
</DocsWrapper>
<DocsPageWrapper>{children}</DocsPageWrapper>
</ThemeProvider>
</SourceContainer>
</DocsContext.Provider>
Expand Down
2 changes: 1 addition & 1 deletion code/ui/blocks/src/blocks/Subtitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const Subtitle: FunctionComponent<SubtitleProps> = ({ children }) => {
if (!text) {
text = parameters?.componentSubtitle;
}
return text ? <PureSubtitle className="sbdocs-subtitle">{text}</PureSubtitle> : null;
return text ? <PureSubtitle className="sbdocs-subtitle sb-unstyled">{text}</PureSubtitle> : null;
};
2 changes: 1 addition & 1 deletion code/ui/blocks/src/blocks/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export const Title: FunctionComponent<TitleProps> = ({ children }) => {
if (!text) {
text = extractTitle(context.storyById().title);
}
return text ? <PureTitle className="sbdocs-title">{text}</PureTitle> : null;
return text ? <PureTitle className="sbdocs-title sb-unstyled">{text}</PureTitle> : null;
};
2 changes: 1 addition & 1 deletion code/ui/blocks/src/components/ArgsTable/ArgsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export const ArgsTable: FC<ArgsTableProps> = (props) => {
<TableWrapper
aria-hidden={isLoading}
{...{ compact, inAddonPanel, isLoading }}
className="docblock-argstable"
className="docblock-argstable sb-unstyled"
>
<thead className="docblock-argstable-head">
<tr>
Expand Down
2 changes: 1 addition & 1 deletion code/ui/blocks/src/components/ColorPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ interface ColorPaletteProps {
*/
export const ColorPalette: FunctionComponent<ColorPaletteProps> = ({ children, ...props }) => (
<ResetWrapper>
<List {...props} className="docblock-colorpalette">
<List {...props} className="docblock-colorpalette sb-unstyled">
<ListHeading>
<ListName>Name</ListName>
<ListSwatches>Swatches</ListSwatches>
Expand Down
42 changes: 12 additions & 30 deletions code/ui/blocks/src/components/DocsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,22 @@ import React from 'react';

/**
* This selector styles all raw elements inside the DocsPage like this example with a `<div/>`:
* :where(div:not(.sb-unstyled, .sbdocs-preview... :where(.sb-unstyled, .sbdocs-preview...) div))
* :where(div:not(.sb-unstyled, .sb-anchor, .sb-unstyled div, .sb-unstyled div))
*
* 1. first ':where': ensures this has a specificity of 0, making it easy to override.
* 2. 'div:not(...)': selects all div elements that are not...
* 3. '.sb-unstyled, .sbdocs-preview...': any of the elements we don't want to style
* 3. ':where(.sb-unstyled, .sbdocs-preview...) div': or are descendants of an .sb-unstyled or .sbdocs-preview, etc. It is a shorthand for '.sb-unstyled div, sbdocs-preview div...'
* 4. .sb-unstyled is an escape hatch that allows the user to opt-out of the default styles
* by wrapping their content in an element with this class: <Unstyled />
* 5. the other UNSTYLED_SELECTORS are elements we don't want the styles to bleed into, like canvas, story and source blocks.
* 1. ':where': ensures this has a specificity of 0, making it easier to override.
* 3. 'div:not(...)': selects all div elements that are not...
* 4. '.sb-anchor': Ensures anchors are not styled, which would have led to inheritable styles bleeding all the way down to stories
* 5. '.sb-unstyled, .sb-unstyled div': any element with sb-unstyled class, or descendants thereof
* 6. .sb-unstyled is an escape hatch that allows the user to opt-out of the default styles
* by wrapping their content in an element with the 'sb-unstyled' class or the <Unstyled /> block.
*
* Most Storybook doc blocks has the sb-unstyled class to opt-out of the default styles.
*/
const UNSTYLED_SELECTORS = [
'.sb-unstyled',
'.sbdocs-preview',
'.sbdocs-pre',
'.sb-story',
'.docblock-source',
'.docblock-argstable',
'.sbdocs-title',
'.sbdocs-subtitle',
'.docblock-icongallery',
'.docblock-emptyblock',
'.docblock-typeset',
'.docblock-colorpalette',
].join(', ');
const toGlobalSelector = (element: string): string =>
`& :where(${element}:not(${UNSTYLED_SELECTORS}, :where(${UNSTYLED_SELECTORS}) ${element}))`;
`& :where(${element}:not(.sb-anchor, .sb-unstyled, .sb-unstyled ${element}))`;

const breakpoint = 600;

export interface DocsPageProps {
title: string;
subtitle?: string;
}

export const Title = styled.h1(withReset, ({ theme }) => ({
color: theme.color.defaultText,
fontSize: theme.typography.size.m3,
Expand Down Expand Up @@ -455,7 +437,7 @@ interface DocsPageWrapperProps {
}

export const DocsPageWrapper: FC<DocsPageWrapperProps> = ({ children }) => (
<DocsWrapper>
<DocsContent>{children}</DocsContent>
<DocsWrapper className="sbdocs sbdocs-wrapper">
<DocsContent className="sbdocs sbdocs-content">{children}</DocsContent>
</DocsWrapper>
);
4 changes: 3 additions & 1 deletion code/ui/blocks/src/components/EmptyBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ const Wrapper = styled.div(withReset, ({ theme }) => ({
fontSize: theme.typography.size.s2,
}));

export const EmptyBlock = (props: any) => <Wrapper {...props} className="docblock-emptyblock" />;
export const EmptyBlock = (props: any) => (
<Wrapper {...props} className="docblock-emptyblock sb-unstyled" />
);
2 changes: 1 addition & 1 deletion code/ui/blocks/src/components/IconGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ interface IconGalleryProps {
*/
export const IconGallery: FunctionComponent<IconGalleryProps> = ({ children, ...props }) => (
<ResetWrapper>
<List {...props} className="docblock-icongallery">
<List {...props} className="docblock-icongallery sb-unstyled">
{children}
</List>
</ResetWrapper>
Expand Down
2 changes: 1 addition & 1 deletion code/ui/blocks/src/components/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const Preview: FC<PreviewProps> = ({
const [expanded, setExpanded] = useState(isExpanded);
const { source, actionItem } = getSource(withSource, expanded, setExpanded);
const [scale, setScale] = useState(1);
const previewClasses = [className].concat(['sbdocs', 'sbdocs-preview']);
const previewClasses = [className].concat(['sbdocs', 'sbdocs-preview', 'sb-unstyled']);

const defaultActionItems = withSource ? [actionItem] : [];
const [additionalActionItems, setAdditionalActionItems] = useState(
Expand Down
2 changes: 1 addition & 1 deletion code/ui/blocks/src/components/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const Source: FunctionComponent<SourceProps> = ({
copyable
format={format}
language={language}
className="docblock-source"
className="docblock-source sb-unstyled"
{...rest}
>
{code}
Expand Down
2 changes: 1 addition & 1 deletion code/ui/blocks/src/components/Story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const Story: FunctionComponent<StoryProps> = (props) => {
const { inline } = props;

return (
<div id={storyBlockIdFromId(props)} className="sb-story">
<div id={storyBlockIdFromId(props)} className="sb-story sb-unstyled">
{inline ? (
<InlineStory {...(props as InlineStoryProps)} />
) : (
Expand Down
2 changes: 1 addition & 1 deletion code/ui/blocks/src/components/Typeset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const Typeset: FC<TypesetProps> = ({
sampleText,
...props
}) => (
<Wrapper {...props} className="docblock-typeset">
<Wrapper {...props} className="docblock-typeset sb-unstyled">
{fontSizes.map((size) => (
<TypeSpecimen key={size}>
<Label>{size}</Label>
Expand Down