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

tech: add storybook addon story source-button #8249

Merged
merged 2 commits into from
Feb 10, 2025
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
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
Loading