Skip to content

Commit 3fb3531

Browse files
committed
feat(clayui.com): Collapsible code in Markup/CSS tab
1 parent 8801e43 commit 3fb3531

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

clayui.com/plugins/gatsby-remark-use-clipboard/index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,28 @@ module.exports = ({markdownAST}) => {
1212
return;
1313
}
1414

15-
node.value = `<div class="code-container">
16-
<button class="btn btn-sm btn-copy" title="Copy"><svg class="lexicon-icon"><use xlink:href="/images/icons/icons.svg#paste"></use></svg></button>
17-
${node.value}
18-
</div>`;
15+
node.value = `
16+
<div class="code-container">
17+
<button class="btn btn-sm btn-copy btn-monospaced btn-unstyled" title="Copy">
18+
<svg class="lexicon-icon">
19+
<use xlink:href="/images/icons/icons.svg#paste"></use>
20+
</svg>
21+
</button>
22+
23+
<button class="btn btn-sm btn-collapse btn-collapse--collapse btn-monospaced btn-unstyled" title="Collapse">
24+
<svg class="lexicon-icon icon-collapse">
25+
<use xlink:href="/images/icons/icons.svg#angle-down"></use>
26+
</svg>
27+
</button>
28+
29+
<button class="btn btn-sm btn-collapse btn-collapse--expand btn-monospaced btn-unstyled hide" title="Expand">
30+
<svg class="lexicon-icon icon-collapse">
31+
<use xlink:href="/images/icons/icons.svg#angle-right"></use>
32+
</svg>
33+
</button>
34+
35+
${node.value}
36+
</div>
37+
`;
1938
});
2039
};

clayui.com/src/html.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default props => {
4343
id="___gatsby"
4444
/>
4545
{props.postBodyComponents}
46+
<script src="/js/code-collapse.js" />
4647
<script src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js" />
4748
</body>
4849
</html>

clayui.com/static/js/code-collapse.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function closestAncestor(node, s) {
2+
const el = node;
3+
let ancestor = node;
4+
5+
if (!document.documentElement.contains(el)) {
6+
return null;
7+
}
8+
9+
do {
10+
if (ancestor.matches(s)) {
11+
return ancestor;
12+
}
13+
14+
ancestor = ancestor.parentElement;
15+
} while (ancestor !== null);
16+
17+
return null;
18+
}
19+
20+
if (document.body) {
21+
document.body.addEventListener('click', event => {
22+
const target = event.target;
23+
const targetClassList = target.classList;
24+
25+
if (
26+
targetClassList.contains('btn-collapse') ||
27+
targetClassList.contains('icon-collapse') ||
28+
(target.tagName === 'use' &&
29+
target.parentNode.classList.contains('icon-collapse'))
30+
) {
31+
const codeContainer = closestAncestor(target, '.code-container');
32+
33+
if (codeContainer) {
34+
const gatsbyCode = codeContainer.querySelector(
35+
'code[class^="gatsby-code-"]'
36+
);
37+
38+
if (gatsbyCode) {
39+
const btnCollapse = codeContainer.querySelector(
40+
'.btn-collapse--collapse'
41+
);
42+
const btnExpand = codeContainer.querySelector(
43+
'.btn-collapse--expand'
44+
);
45+
46+
if (gatsbyCode.style.display === 'none') {
47+
btnCollapse.classList.remove('hide');
48+
btnExpand.classList.add('hide');
49+
50+
gatsbyCode.style.display = 'block';
51+
} else {
52+
btnCollapse.classList.add('hide');
53+
btnExpand.classList.remove('hide');
54+
55+
gatsbyCode.style.display = 'none';
56+
}
57+
}
58+
}
59+
}
60+
});
61+
}

0 commit comments

Comments
 (0)