diff --git a/code/ui/blocks/src/blocks/Source.stories.tsx b/code/ui/blocks/src/blocks/Source.stories.tsx index ffd2eb90db0e..00a9ea2d3741 100644 --- a/code/ui/blocks/src/blocks/Source.stories.tsx +++ b/code/ui/blocks/src/blocks/Source.stories.tsx @@ -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 }, }; diff --git a/code/ui/blocks/src/blocks/Source.tsx b/code/ui/blocks/src/blocks/Source.tsx index f63d497af048..4957ad5aa55b 100644 --- a/code/ui/blocks/src/blocks/Source.tsx +++ b/code/ui/blocks/src/blocks/Source.tsx @@ -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 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';