Skip to content

docs: add links to storybook #367

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

Merged
merged 2 commits into from
Jun 12, 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
30 changes: 30 additions & 0 deletions .storybook/addons/button/githubButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Button } from '@storybook/components'
import { GithubIcon } from '@storybook/icons'
import { addons, types } from '@storybook/manager-api'
import React from 'react'

const ADDON_ID = 'github-link'
const TOOL_ID = `${ADDON_ID}/tool`

const REPO_URL = 'https://github.com/rustic-ai/rustic-ui-components'

function GitHubButton() {
return (
<Button
title="View on GitHub"
onClick={() => window.open(REPO_URL, '_blank')}
size="small"
>
<GithubIcon />
</Button>
)
}

addons.register(ADDON_ID, () => {
addons.add(TOOL_ID, {
type: types.TOOL,
title: 'View on GitHub',
match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
render: () => <GitHubButton />,
})
})
104 changes: 104 additions & 0 deletions .storybook/addons/button/logo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { addons } from '@storybook/manager-api'

const logoDataAttribute = 'data-storybook-logo-id'
const logoId = 'storybook-sidebar-custom-logo'
const navSelector = 'nav'
const scrollContainerSelector = 'nav > div'
const scrollContainerPaddingClass = 'storybook-scroll-padding'

const initialCheckInterval = 100
const uiSettleTimeout = 300

const bottomPadding = 10

const logoUrl = '/images/dragonscale-logo.svg'
const websiteUrl = 'https://www.dragonscale.ai'

const addLogoToSidebar = () => {
const sidebar = document.querySelector(navSelector)
if (!sidebar) {
return false
}

const existingLogo = sidebar.querySelector(
`[${logoDataAttribute}="${logoId}"]`
)
if (existingLogo) {
;(existingLogo as HTMLElement).style.display = 'flex'
return true
}

const logoContainer = document.createElement('div')
Copy link
Contributor

Choose a reason for hiding this comment

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

we could add a class for this and use css styling in style tag in the html

logoContainer.setAttribute(logoDataAttribute, logoId)
logoContainer.className = 'storybook-logo-container'

const logo = document.createElement('img')
logo.src = window.location.origin + logoUrl
logo.alt = 'Build by Dragonscale'
logo.className = 'storybook-logo-image'

logo.onclick = () => {
window.open(websiteUrl, '_blank')
}

logoContainer.appendChild(logo)
sidebar.appendChild(logoContainer)

const scrollContainer = document.querySelector(
scrollContainerSelector
) as HTMLElement
if (scrollContainer) {
const logoHeight = logoContainer.offsetHeight
scrollContainer.classList.add(scrollContainerPaddingClass)
document.documentElement.style.setProperty(
'--scroll-padding-bottom',
`${logoHeight + bottomPadding}px`
)
}

return true
}

const init = () => {
const checkInterval = setInterval(() => {
if (addLogoToSidebar()) {
clearInterval(checkInterval)

const bodyObserver = new MutationObserver(() => {
addLogoToSidebar()
})

bodyObserver.observe(document.body, {
childList: true,
subtree: true,
})

window.addEventListener('resize', () => {
setTimeout(() => {
addLogoToSidebar()

const sidebar = document.querySelector(navSelector)
const logoContainer = sidebar?.querySelector(
`[${logoDataAttribute}="${logoId}"]`
)
const scrollContainer = document.querySelector(
scrollContainerSelector
) as HTMLElement

if (scrollContainer && logoContainer) {
const currentLogoHeight = (logoContainer as HTMLElement)
.offsetHeight
document.documentElement.style.setProperty(
'--scroll-padding-bottom',
`${currentLogoHeight + bottomPadding}px`
)
}
}, uiSettleTimeout)
})
}
}, initialCheckInterval)
}

addons.register('sidebar-logo', () => {
init()
})
2 changes: 2 additions & 0 deletions .storybook/addons/button/manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './viewSourceButton.tsx'
import './githubButton.tsx'
65 changes: 65 additions & 0 deletions .storybook/addons/button/viewSourceButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Button } from '@storybook/components'
import { addons, type API, types } from '@storybook/manager-api'
import React, { useEffect, useState } from 'react'
import type { API_LeafEntry } from 'storybook/internal/types'

const ADDON_ID = 'github-source-link'
const TOOL_ID = `${ADDON_ID}/tool`

function ViewSourceButton({ api }: Readonly<{ api: API }>) {
const [storyData, setStoryData] = useState<API_LeafEntry>()

useEffect(() => {
const updateStoryData = () => {
const uiSettleTimeout = 100
setTimeout(() => {
try {
const currentStoryData = api.getCurrentStoryData()
if (currentStoryData && currentStoryData.importPath) {
setStoryData(currentStoryData)
}
} catch (error) {
console.error('Error getting story data:', error)
}
}, uiSettleTimeout)
}

updateStoryData()

const removeStoryChangedListener = api.on('storyChanged', updateStoryData)
const removeRenderedListener = api.on('STORY_RENDERED', updateStoryData)

return () => {
removeStoryChangedListener()
removeRenderedListener()
}
}, [api])

if (!storyData || !storyData.importPath) {
return null
}

const url = [
'https://github.com/rustic-ai/rustic-ui-components/blob/main',
storyData.importPath.replace(/\.\//, ''),
].join('/')

return (
<Button
title="View source on GitHub"
onClick={() => window.open(url, '_blank')}
size="small"
>
View source
</Button>
)
}

addons.register(ADDON_ID, (api) => {
addons.add(TOOL_ID, {
type: types.TOOL,
title: 'View source on GitHub',
match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
render: () => <ViewSourceButton api={api} />,
})
})
5 changes: 4 additions & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable storybook/no-uninstalled-addons */
import type { StorybookConfig } from '@storybook/react-webpack5'
const path = require('path')
import path from 'path'

const config: StorybookConfig = {
stories: ['../src/**/*.stories.@(tsx)', '../docs/*.mdx'],
addons: [
Expand All @@ -11,6 +13,7 @@ const config: StorybookConfig = {
options: { transcludeMarkdown: true },
},
'@storybook/addon-webpack5-compiler-swc',
'./addons/button',
],
framework: {
name: '@storybook/react-webpack5',
Expand Down
1 change: 1 addition & 0 deletions .storybook/manager-head.html
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<link rel="shortcut icon" href="images/favicon.jpg" />
<link rel="stylesheet" href="css/logo.css" />
3 changes: 3 additions & 0 deletions .storybook/manager.js → .storybook/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import './addons/button/logo.tsx'

import { addons } from '@storybook/manager-api'

import rusticTheme from './rusticTheme'

addons.setConfig({
Expand Down
22 changes: 22 additions & 0 deletions public/css/logo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.storybook-logo-container {
position: absolute;
bottom: 15px;
left: 0;
right: 0;
display: flex;
justify-content: center;
align-items: center;
padding-top: 10px;
z-index: 1;
pointer-events: auto;
}

.storybook-logo-image {
max-width: 80%;
height: 20px;
cursor: pointer;
}

.storybook-scroll-padding {
padding-bottom: var(--scroll-padding-bottom, 30px);
}
Loading