-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storybook: Add link to component folder on GitHub, retire Storysource (…
…#45727) * Storybook: Add source link button * Remove storysource addon * Update docs * Add link to relevant plugin
- Loading branch information
Showing
9 changed files
with
146 additions
and
125 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,14 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import styled from '@emotion/styled'; | ||
import { Icons } from '@storybook/components'; | ||
|
||
const StyledIcons = styled( Icons )` | ||
display: inline-block !important; | ||
width: 14px; | ||
`; | ||
|
||
export const InlineIcon = ( props ) => ( | ||
<StyledIcons aria-hidden={ true } { ...props } /> | ||
); |
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
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
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
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,67 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const babel = require( '@babel/core' ); | ||
const path = require( 'path' ); | ||
|
||
const REPO_ROOT = path.resolve( __dirname, '../../' ); | ||
|
||
/** | ||
* Adds a `sourceLink` parameter to the story metadata, based on the file path. | ||
* | ||
* @see https://storybook.js.org/addons/storybook-source-link | ||
*/ | ||
function addSourceLinkPlugin() { | ||
return { | ||
visitor: { | ||
ExportDefaultDeclaration( visitorPath, state ) { | ||
const componentPath = getComponentPathFromStoryPath( | ||
state.file.opts.filename | ||
); | ||
const properties = | ||
// When default export is anonymous, the declaration is an object expression | ||
visitorPath.node.declaration.properties ?? | ||
// When default export is named, the declaration is an identifier, usually the previous node | ||
visitorPath.getPrevSibling().node.declarations[ 0 ].init | ||
.properties; | ||
|
||
alterParameters( properties, componentPath ); | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function getComponentPathFromStoryPath( storyPath ) { | ||
const componentRoot = path.resolve( storyPath, '../../' ); | ||
return path.relative( REPO_ROOT, componentRoot ); | ||
} | ||
|
||
function alterParameters( properties, componentPath ) { | ||
const sourceLink = babel.types.objectProperty( | ||
babel.types.identifier( 'sourceLink' ), | ||
babel.types.stringLiteral( componentPath ) | ||
); | ||
|
||
let parameters = properties.find( ( op ) => op.key.name === 'parameters' ); | ||
|
||
if ( ! parameters ) { | ||
parameters = babel.types.objectProperty( | ||
babel.types.identifier( 'parameters' ) | ||
); | ||
properties.push( parameters ); | ||
} | ||
|
||
parameters.value.properties = [ | ||
sourceLink, | ||
...parameters.value.properties, | ||
]; | ||
} | ||
|
||
module.exports = function ( source, { sources } ) { | ||
const output = babel.transform( source, { | ||
plugins: [ addSourceLinkPlugin ], | ||
filename: sources[ 0 ], | ||
sourceType: 'module', | ||
} ); | ||
return output.code; | ||
}; |