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: Support <StrictMode /> and <Suspense /> in source viewer #19785

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
36 changes: 35 additions & 1 deletion code/renderers/react/src/docs/jsxDecorator.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
import type { FC, PropsWithChildren } from 'react';
import React, { createElement, Profiler } from 'react';
import React, { StrictMode, createElement, Profiler } from 'react';
import PropTypes from 'prop-types';
import { addons, useEffect } from '@storybook/preview-api';
import { SNIPPET_RENDERED } from '@storybook/docs-tools';
Expand Down Expand Up @@ -155,6 +155,40 @@ describe('renderJsx', () => {
`);
});

it('StrictMode', () => {
function StrictModeComponent(props: any) {
return (
<StrictMode>
<div>{props.children}</div>
</StrictMode>
);
}

expect(renderJsx(createElement(StrictModeComponent, {}, 'I am StrictMode'), {}))
.toMatchInlineSnapshot(`
<StrictModeComponent>
I am StrictMode
</StrictModeComponent>
`);
});

it('Suspense', () => {
function SuspenseComponent(props: any) {
return (
<React.Suspense fallback={null}>
<div>{props.children}</div>
</React.Suspense>
);
}

expect(renderJsx(createElement(SuspenseComponent, {}, 'I am Suspense'), {}))
.toMatchInlineSnapshot(`
<SuspenseComponent>
I am Suspense
</SuspenseComponent>
`);
});

it('should not add default props to string if the prop value has not changed', () => {
const Container = ({ className, children }: { className: string; children: string }) => {
return <div className={className}>{children}</div>;
Expand Down
38 changes: 20 additions & 18 deletions code/renderers/react/src/docs/jsxDecorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { logger } from '@storybook/client-logger';

import type { ReactRenderer } from '../types';

import { isMemo, isForwardRef } from './lib';

// Recursively remove "_owner" property from elements to avoid crash on docs page when passing components as an array prop (#17482)
// Note: It may be better to use this function only in development environment.
function simplifyNodeForStringify(node: ReactNode): ReactNode {
Expand Down Expand Up @@ -56,7 +54,7 @@ export const renderJsx = (code: React.ReactElement, options: JSXOptions) => {
const Type = renderedJSX.type;

// @ts-expect-error (Converted from ts-ignore)
for (let i = 0; i < options.skip; i += 1) {
for (let i = 0; i < options?.skip; i += 1) {
if (typeof renderedJSX === 'undefined') {
logger.warn('Cannot skip undefined element');
return null;
Expand All @@ -80,21 +78,25 @@ export const renderJsx = (code: React.ReactElement, options: JSXOptions) => {
}
}

const displayNameDefaults =
typeof options.displayName === 'string'
? { showFunctions: true, displayName: () => options.displayName }
: {
// To get exotic component names resolving properly
displayName: (el: any): string =>
el.type.displayName ||
(el.type === Symbol.for('react.profiler') ? 'Profiler' : null) ||
getDocgenSection(el.type, 'displayName') ||
(el.type.name !== '_default' ? el.type.name : null) ||
(typeof el.type === 'function' ? 'No Display Name' : null) ||
(isForwardRef(el.type) ? el.type.render.name : null) ||
(isMemo(el.type) ? el.type.type.name : null) ||
el.type,
};
let displayNameDefaults;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Major Change: displayNameDefaults.displayName should be undefined if getDocgenSection returns null:

export function getDocgenSection(component: Component, section: string): any {
return hasDocgen(component) ? component.__docgenInfo[section] : null;
}

and then the logic will be handled by react-element-to-jsx-string:

https://github.com/algolia/react-element-to-jsx-string/blob/984f26908c17cea134fa53d17ac51f179e5195b2/src/parser/parseReactElement.js#L47-L77

We don't need to rewrite redundant code here.


if (typeof options?.displayName === 'string') {
displayNameDefaults = { showFunctions: true, displayName: () => options.displayName };
/**
* add `renderedJSX?.type`to handle this case:
*
* https://github.com/zhyd1997/storybook/blob/20863a75ba4026d7eba6b288991a2cf091d4dfff/code/renderers/react/template/stories/errors.stories.tsx#L14
*
* or it show the error message when run `yarn build-storybook --quiet`:
*
* Cannot read properties of undefined (reading '__docgenInfo').
*/
} else if (renderedJSX?.type && getDocgenSection(renderedJSX.type, 'displayName')) {
displayNameDefaults = {
// To get exotic component names resolving properly
displayName: (el: any): string => getDocgenSection(el.type, 'displayName'),
};
}

const filterDefaults = {
filterProps: (value: any, key: string): boolean => value !== undefined,
Expand Down