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

React: Don't show decorators in JSX snippets #21907

Merged
merged 6 commits into from
Apr 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion code/lib/preview-api/src/modules/addons/hooks.ts
Expand Up @@ -134,7 +134,7 @@ function hookify<TRenderer extends Renderer>(
decorator: DecoratorFunction<TRenderer>
): DecoratorFunction<TRenderer>;
function hookify<TRenderer extends Renderer>(fn: AbstractFunction) {
return (...args: any[]) => {
const hookified = (...args: any[]) => {
const { hooks }: { hooks: HooksContext<TRenderer> } =
typeof args[0] === 'function' ? args[1] : args[0];

Expand Down Expand Up @@ -172,6 +172,9 @@ function hookify<TRenderer extends Renderer>(fn: AbstractFunction) {
hooks.currentDecoratorName = prevDecoratorName;
return result;
};

hookified.originalFn = fn;
return hookified;
}

// Counter to prevent infinite loops.
Expand Down
19 changes: 19 additions & 0 deletions code/renderers/react/src/applyDecorators.ts
@@ -0,0 +1,19 @@
import { defaultDecorateStory } from '@storybook/preview-api';
import type { LegacyStoryFn, DecoratorFunction } from '@storybook/types';

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

export const applyDecorators = (
storyFn: LegacyStoryFn<ReactRenderer>,
decorators: DecoratorFunction<ReactRenderer>[]
): LegacyStoryFn<ReactRenderer> => {
// @ts-expect-error originalFn is not defined on the type for decorator. This is a temporary fix
// that we will remove soon (likely) in favour of a proper concept of "inner" decorators.
const jsxIndex = decorators.findIndex((d) => d.originalFn === jsxDecorator);
shilman marked this conversation as resolved.
Show resolved Hide resolved

const reorderedDecorators =
jsxIndex === -1 ? decorators : [...decorators.splice(jsxIndex, 1), ...decorators];

return defaultDecorateStory(storyFn, reorderedDecorators);
};
2 changes: 2 additions & 0 deletions code/renderers/react/src/config.ts
Expand Up @@ -5,3 +5,5 @@ export const parameters = { renderer: 'react', ...docsParams };
export { decorators, argTypesEnhancers } from './docs/config';

export { render, renderToCanvas } from './render';

export { applyDecorators } from './applyDecorators';
22 changes: 21 additions & 1 deletion code/renderers/react/template/stories/decorators.stories.tsx
@@ -1,11 +1,12 @@
import type { FC } from 'react';
import React from 'react';
import React, { useContext, createContext } from 'react';
import type { StoryObj, Meta } from '@storybook/react';

const Component: FC = () => <p>Story</p>;

export default {
component: Component,
tags: ['autodocs'],
decorators: [
(Story) => (
<>
Expand All @@ -26,3 +27,22 @@ export const All: StoryObj<typeof Component> = {
),
],
};

// This story will error if `parameters.docs.source.excludeDecorators` is true:
// See https://github.com/storybookjs/storybook/issues/21900
const TestContext = createContext<boolean>(false);
export const Context: StoryObj<typeof Component> = {
// parameters: { docs: { source: { excludeDecorators: true } } },
decorators: [
(Story) => (
<TestContext.Provider value>
<Story />
</TestContext.Provider>
),
],
render: function Render(args, context) {
const value = useContext(TestContext);
if (!value) throw new Error('TestContext not set, decorator did not run!');
return <p>Story</p>;
},
};