Skip to content

Commit

Permalink
tech: add storybook addon story source-button (#8249)
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuhamethanov authored Feb 10, 2025
1 parent 33400b3 commit 4f84b5d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/vkui/.storybook/addons/source-button/SourceButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IconButton } from '@storybook/components';
import { useStorybookState } from '@storybook/manager-api';
import { GithubIcon } from '@storybook/icons';
import * as React from 'react';
import { BASE_COMPONENTS_URL } from './constants';

const getComponentUrl = (importPath: string): string => {
const pathWithoutFile = importPath.replace(/\/[^/]+\.stories\.tsx$/, '');
const cleanPath = pathWithoutFile.replace(/^\.\//, '');
return `${BASE_COMPONENTS_URL}/${cleanPath}/`;
};

export const SourceButton = () => {
const { index, storyId } = useStorybookState();

const story = index?.[storyId];
const importPath = story && 'importPath' in story && story.importPath;

if (!importPath) {
return null;
}

const sourceUrl = getComponentUrl(importPath);

return (
<a href={sourceUrl} target="_blank" rel="noreferrer">
<IconButton>
<GithubIcon />
</IconButton>
</a>
);
};
2 changes: 2 additions & 0 deletions packages/vkui/.storybook/addons/source-button/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ADDON_ID = 'storybook/source';
export const BASE_COMPONENTS_URL = 'https://github.com/VKCOM/VKUI/tree/master/packages/vkui';
11 changes: 11 additions & 0 deletions packages/vkui/.storybook/addons/source-button/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { addons, types } from '@storybook/manager-api';
import { ADDON_ID } from './constants';
import { SourceButton } from './SourceButton';

addons.register(ADDON_ID, () => {
addons.add(ADDON_ID, {
title: 'Source',
type: types.TOOL,
render: SourceButton,
});
});
1 change: 1 addition & 0 deletions packages/vkui/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const config: StorybookConfig = {
'./addons/colorScheme',
'./addons/pointer',
'./addons/customPanelHeaderAfter',
'./addons/source-button',
'./addons/storybook-theme',
getAbsolutePath('@storybook/addon-webpack5-compiler-swc'),
],
Expand Down

0 comments on commit 4f84b5d

Please sign in to comment.