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

Addon-docs: Fix scroll behavior on page navigation #9331

Merged
merged 2 commits into from
Jan 6, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions addons/docs/src/blocks/Meta.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { FunctionComponent } from 'react';
import React, { FC, useContext } from 'react';
import { document } from 'global';
import { Anchor } from './Anchor';
import { DocsContext, DocsContextProps } from './DocsContext';
import { getDocsStories } from './utils';

type Decorator = (...args: any) => any;

Expand All @@ -9,9 +13,27 @@ interface MetaProps {
parameters?: any;
}

function getFirstStoryId(docsContext: DocsContextProps): string {
const stories = getDocsStories(docsContext);

return stories.length > 0 ? stories[0].id : null;
}

function renderAnchor() {
const context = useContext(DocsContext);
// eslint-disable-next-line react/destructuring-assignment
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.
* It doesn't actually render anything.
*/
export const Meta: FunctionComponent<MetaProps> = props => null;
export const Meta: FC<MetaProps> = () => {
const params = new URL(document.location).searchParams;
const isDocs = params.get('viewMode') === 'docs';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to do this @shilman ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet!


return isDocs ? renderAnchor() : null;
};
5 changes: 5 additions & 0 deletions addons/docs/src/blocks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { StoryData, Component } from './shared';

export const getDocsStories = (context: DocsContextProps): StoryData[] => {
const { storyStore, selectedKind } = context;

if (!storyStore) {
return [];
}

return storyStore
.getStoriesForKind(selectedKind)
.filter((s: any) => !(s.parameters && s.parameters.docs && s.parameters.docs.disable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import BaseButton from '../../components/BaseButton';
import { ButtonGroup } from '../../components/ButtonGroup';

export default {
title: 'Addons/Docs/stories docs bocks',
title: 'Addons/Docs/stories docs blocks',
component: DocgenButton,
parameters: {
docs: {
Expand Down