@stoplight/elements
![Storybook](https://cdn.jsdelivr.net/gh/storybookjs/brand@master/badge/badge-storybook.svg)
UI components for composing beautiful developer documentation
Installation
yarn add @stoplight/elements
Usage
import { Page, Provider, TableOfContents } from '@stoplight/elements';
import { Docs } from '@stoplight/elements/components/Docs';
import { TryIt } from '@stoplight/elements/components/TryIt';
<Provider
host="https://stoplight.io/api"
components={{
link: ({ node, children }) => {
// Render a custom link component
return (
<a href={node.url} title={node.title}>
{children}
</a>
);
},
}}
>
<div className="flex">
<TableOfContents srn="gh/stoplightio/studio-demo" />
<Page
className="flex-1"
srn="gh/stoplightio/studio-demo/docs/introduction.md"
tabs={({ node }) => {
const tabs = [{ title: 'Docs', content: <Docs node={node} /> }];
if (node.type === 'http_operation') {
tabs.push({ title: 'Try It', content: <TryIt value={node.data} /> });
}
return tabs;
}}
/>
</div>
</Provider>;
Widgets
Elements can be used as a plain JS library.
Load the elements library:
Add the following script tag to the head tag of your website, BEFORE other css or script tags.
<head>
<link
rel="stylesheet"
href="https://stoplight.io/static/elements/bundle.v1.css"
media="print"
onload="this.media='all'"
/>
<script async defer src="https://stoplight.io/static/elements/bundle.v1.js" onload="__onElementsLoad()"></script>
<script>
function __onElementsLoad() {
SL.config.host = 'https://stoplight.io/api';
window.dispatchEvent(new Event('SL.ready'));
}
</script>
</head>
Global "SL" object:
The global variable made available on the page after the elements library has been loaded.
declare namespace SL {
interface IWidget {
srn: string;
render(htmlId: string, srn: string): void;
remove(): void;
}
const createElement: any;
const config: {
host?: string;
token?: string;
components?: any;
};
const elements: {
hub: IWidget;
page: IWidget;
toc: IWidget;
};
}
Render a table of contents onto the page:
SL.elements.toc.render('toc-container-element-id', 'gh/stoplightio/studio-demo');
Render a specific page:
SL.elements.page.render('page-container-element-id', 'gh/stoplightio/studio-demo/docs/introduction.md');
Contributing
- Clone repo.
- Create / checkout
feature/{name}
, chore/{name}
, or fix/{name}
branch. - Install deps:
yarn
. - Make your changes.
- Run tests:
yarn test.prod
. - Stage relevant files to git.
- Commit:
yarn commit
. NOTE: Commits that don't follow the conventional format will be rejected. yarn commit
creates this format for you, or you can put it together manually and then do a regular git commit
. - Push:
git push
. - Open PR targeting the
master
branch.