Skip to content

Commit 851ff2d

Browse files
committed
tabbed code MD component
1 parent 47e8a32 commit 851ff2d

6 files changed

Lines changed: 94 additions & 22 deletions

File tree

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"react/prefer-stateless-function": "off",
66
"react/prop-types": "off",
77
"react/no-danger": "off",
8+
"jsx-a11y/no-noninteractive-element-to-interactive-role": "off",
89
"jsx-a11y/anchor-is-valid": [ "error", { "components": [ "Link" ], "specialLink": [ "to" ] } ]
910
},
1011
"settings": {

content/docs/for-developers/custom-slug.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,48 @@ title: "custom elements"
33
category: "sending-email"
44
---
55

6+
<code-group langs="PHP,Python,JavaScript">
7+
8+
```php
9+
// Create array containing abbreviations of days of week.
10+
$daysOfWeek = array('S','M','T','W','T','F','S');
11+
12+
// What is the first day of the month in question?
13+
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
14+
15+
// How many days does this month contain?
16+
```
17+
18+
```python
19+
static int
20+
_is_legal_capsule(PyCapsule *capsule, const char *invalid_capsule)
21+
{
22+
if (!capsule || !PyCapsule_CheckExact(capsule) || capsule->pointer == NULL) {
23+
PyErr_SetString(PyExc_ValueError, invalid_capsule);
24+
return 0;
25+
}
26+
return 1;
27+
}
28+
```
29+
30+
```javascript
31+
const cat = {
32+
id: `${i}`,
33+
slug: category,
34+
parent: '__SOURCE__',
35+
children: [],
36+
internal: {
37+
type: 'forDeveloperCategories',
38+
},
39+
};
40+
// Get content digest of node. (Required field)
41+
const contentDigest = crypto
42+
.createHash('md5')
43+
.update(JSON.stringify(cat))
44+
.digest('hex');
45+
```
46+
47+
</code-group>
648

749
## Headers
850

@@ -164,7 +206,7 @@ Some text to show that the reference links can follow later.
164206

165207
### Images
166208

167-
<code-group>
209+
168210

169211
```php
170212
// Create array containing abbreviations of days of week.
@@ -204,7 +246,7 @@ Some text to show that the reference links can follow later.
204246
.update(JSON.stringify(cat))
205247
.digest('hex');
206248
```
207-
</code-group>
249+
208250

209251
Here's our logo (hover to see the title text):
210252

plugins/sendgrid-remark-headers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ module.exports = ({ markdownAST }) => {
4545

4646
return markdownAST;
4747
};
48+

src/componentsMarkdown/CodeGroup.jsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,61 @@ export default class CodeGroup extends React.Component {
55
constructor(props) {
66
super(props);
77

8+
this.toggleLang = this.toggleLang.bind(this);
9+
this.langs = this.props.langs.split(',');
10+
811
this.state = {
9-
currentTab: false,
12+
currentTab: this.langs[0].toLowerCase(),
1013
};
1114
}
1215

13-
getLangs() {
14-
const langs = [];
16+
toggleLang(e) {
17+
this.setState({
18+
currentTab: e.target.dataset.lang,
19+
});
20+
}
1521

16-
_.forEach(this.props.children, (val) => {
17-
if (_.isObject(val)) {
18-
_.forEach(val.props.children, (subEl) => {
19-
if (_.isObject(subEl)) {
20-
langs.push(subEl.props.className.replace('language-', ''));
21-
}
22-
});
23-
}
22+
renderTabs() {
23+
const { langs } = this;
24+
const { currentTab } = this.state;
25+
26+
const tabs = langs.map((lang) => {
27+
const isActive = lang.toLowerCase() === currentTab ? 'is-active' : null;
28+
const classes = `tab ${isActive}`;
29+
30+
return <li key={lang} className={classes} data-lang={lang.toLowerCase()} onClick={this.toggleLang} onKeyDown={this.handleClick} role="button">{lang}</li>;
2431
});
2532

26-
return langs;
33+
return tabs;
2734
}
2835

2936
renderCodeBlocks() {
37+
const { langs } = this;
38+
const { currentTab } = this.state;
3039
// Remove empty strings from array
3140
const codeBlocks = this.props.children.filter(block => _.isObject(block));
3241

3342
// return code blocks wrapped parent element
34-
return codeBlocks.map(block => <div className="tabbed-code_block">{block}</div>);
35-
}
43+
return codeBlocks.map((block, i) => {
44+
const lang = langs[i].toLowerCase();
45+
let classes = `tabbed-code__block ${lang}`;
3646

37-
render() {
38-
const langs = this.getLangs();
47+
if (lang === currentTab) {
48+
classes = classes.concat(' show');
49+
}
3950

51+
return <div key={block.key} className={classes} >{block}</div>;
52+
});
53+
}
4054

55+
render() {
4156
return (
42-
4357
<div className="tabbed-code">
44-
{/* {langs.map(lang => <span claName="tabbed-code_tab">{lang}</span>)} */}
45-
{/* {this.renderCodeBlocks()} */}
58+
<ul className="tab-wrapper is-editor">
59+
{this.renderTabs()}
60+
</ul>
61+
{this.renderCodeBlocks()}
4662
</div>
47-
4863
);
4964
}
5065
}

src/scss/components/_tabs.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
display: flex;
33
justify-content: flex-start;
44
border-bottom: 1px solid $slate-10;
5+
padding-left: 0;
56

67
&.is-centered {
78
justify-content: center;

src/templates/doc.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ $aside-width: 300px;
7373
.callout--alert {
7474
border-left: 4px solid $alert-danger-text;
7575
}
76+
77+
.tabbed-code__block {
78+
display: none;
79+
}
80+
81+
.tabbed-code__block.show {
82+
display: block;
83+
}
84+
85+
.tabbed-code .tab {
86+
flex: 1;
87+
}

0 commit comments

Comments
 (0)