Skip to content

Commit

Permalink
Merge pull request #20807 from storybookjs/20780-unattached-source-code
Browse files Browse the repository at this point in the history
Docs: Allow using `<Source code=".." />` unattached.
  • Loading branch information
JReinhold authored Jan 27, 2023
2 parents 95f4adc + 3ef1144 commit 817666d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions code/ui/blocks/src/blocks/Source.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export const Code: Story = {
args: { code },
};

export const CodeUnattached: Story = {
args: { code },
parameters: { attached: false },
};

export const CodeParameters: Story = {
args: { of: ParametersStories.Code },
};
Expand Down
10 changes: 8 additions & 2 deletions code/ui/blocks/src/blocks/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ export const useSourceProps = (
const resolved = docsContext.resolveOf(props.of, ['story']);
stories = [resolved.story];
} else if (stories.length === 0) {
stories = [docsContext.storyById()];
try {
// Always fall back to the primary story for source parameters, even if code is set.
stories = [docsContext.storyById()];
} catch (err) {
// You are allowed to use <Story code="..." /> unattached.
if (!props.code) throw err;
}
}

const sourceParameters = (stories[0].parameters.docs?.source || {}) as SourceParameters;
const sourceParameters = (stories[0]?.parameters?.docs?.source || {}) as SourceParameters;
let { code } = props; // We will fall back to `sourceParameters.code`, but per story below
let format = props.format ?? sourceParameters.format;
const language = props.language ?? sourceParameters.language ?? 'jsx';
Expand Down

0 comments on commit 817666d

Please sign in to comment.