-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tech: add storybook addon story
source-button
(#8249)
- Loading branch information
1 parent
33400b3
commit 4f84b5d
Showing
4 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
packages/vkui/.storybook/addons/source-button/SourceButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters