-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Meta.tsx
33 lines (26 loc) · 1.05 KB
/
Meta.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import React, { FC, useContext } from 'react';
import { document } from 'global';
import { Args, BaseAnnotations, BaseMeta } from '@storybook/addons';
import { Anchor } from './Anchor';
import { DocsContext, DocsContextProps } from './DocsContext';
import { getDocsStories } from './utils';
import { Component } from './types';
type MetaProps = BaseMeta<Component> & BaseAnnotations<Args, any>;
function getFirstStoryId(docsContext: DocsContextProps): string {
const stories = getDocsStories(docsContext);
return stories.length > 0 ? stories[0].id : null;
}
function renderAnchor() {
const context = useContext(DocsContext);
const anchorId = getFirstStoryId(context) || context.id;
return <Anchor storyId={anchorId} />;
}
/**
* This component is used to declare component metadata in docs
* and gets transformed into a default export underneath the hood.
*/
export const Meta: FC<MetaProps> = () => {
const params = new URL(document.location).searchParams;
const isDocs = params.get('viewMode') === 'docs';
return isDocs ? renderAnchor() : null;
};