-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Meta.tsx
34 lines (26 loc) · 1.04 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
34
import React, { FC, useContext } from 'react';
import global from 'global';
import { Args, BaseAnnotations, BaseMeta } from '@storybook/addons';
import { Anchor } from './Anchor';
import { DocsContext, DocsContextProps } from './DocsContext';
import { Component } from './types';
const { document } = global;
type MetaProps = BaseMeta<Component> & BaseAnnotations<Args, any>;
function getFirstStoryId(docsContext: DocsContextProps<any>): string {
const stories = docsContext.componentStories();
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;
};