⏲ Time invest: 15 Minutes ––– 👩🎓 Level: Expert
Documentation for your components is important, but often is not connected to them very well.
patternplate
provides widgets that can embed your components directly into your text documents.
We will …
patternplate
widgetsHave a rough grasp on Markdown
Understand the basics of JSX
Amongst other formatting tools Markdown provides a neat way to
display code blocks. patternplate
hightlights code blocks automatically
for a number of common web languages. Let's try this:
Make sure you have patternplate
running on localhost:1337
Open ./README.md
with your text editor. We recommend opening the patternplate interface
next to your text editor.
Append the following code to ./README.md
. Don't worry about the contents too much for
now, we just test out syntax highlighting with this:
## My first code block
```js
const React = require("react");
const {ComponentList} = require("@patternplate/widgets");
module.exports = () => {
return (
<ComponentList query="hello"/>
);
};
```
patternplate
updates automatically and renders your code block like this at the bottom
of the my-patternplate
rendering. Notice the lovely syntax highlighting. 💅
The idea of fusing code and docs is powerful, can we take it even further?
What if you could execute code inside our docs? Turns out patternplate
lets
you do that!
Copy your new code block again and replace its language with widget
.
## My first code block ```js const React = require("react"); const {ComponentList} = require("@patternplate/widgets"); module.exports = () => { return ( <ComponentList query="hello"/> ); }; ``` ## My first widget ```widget const React = require("react"); const {ComponentList} = require("@patternplate/widgets"); module.exports = () => { return ( <ComponentList query="hello"/> ); }; ```
## My first code block```jsconst React = require("react");const {ComponentList} = require("@patternplate/widgets");module.exports = () => {return (<ComponentList query="hello"/>);};```## My first widget```widgetconst React = require("react");const {ComponentList} = require("@patternplate/widgets");module.exports = () => {return (<ComponentList query="hello"/>);};```
This renders a ComponentList
widget into the document. The
result should look like this:
ComponentList
creates a list of components matching the search query
given in its query
prop.
Let's make all components show up in the list next.
Change the query
prop of ComponentList
to "is=pattern"
## My first widget ```widget const React = require("react"); const {ComponentList} = require("@patternplate/widgets"); module.exports = () => { return ( <ComponentList query="is=pattern"/> ); }; ```
## My first widget```widgetconst React = require("react");const {ComponentList} = require("@patternplate/widgets");module.exports = () => {return (<ComponentList query="is=pattern"/>);};```
We expect ComponentList
to list all items that are a "pattern"
now:
Create a second widget block in ./README.md
by adding this code at the end of the file:
## My second widget
```widget
const React = require("react");
const {ComponentDemo} = require("@patternplate/widgets");
module.exports = () => {
return (
<ComponentDemo id="button"/>
);
};
```
Scroll to the very end of ./README.md
and see a live demo of Button
embedded directly:
js
, html
, css
) are syntax-highlightedwidget
code block enables inline code executionComponentList
displays lists of patternsComponentDemo
shows a pattern demo