-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clayui.com): Collapsible code in Markup/CSS tab
- Loading branch information
1 parent
8801e43
commit 3fb3531
Showing
3 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
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,61 @@ | ||
function closestAncestor(node, s) { | ||
const el = node; | ||
let ancestor = node; | ||
|
||
if (!document.documentElement.contains(el)) { | ||
return null; | ||
} | ||
|
||
do { | ||
if (ancestor.matches(s)) { | ||
return ancestor; | ||
} | ||
|
||
ancestor = ancestor.parentElement; | ||
} while (ancestor !== null); | ||
|
||
return null; | ||
} | ||
|
||
if (document.body) { | ||
document.body.addEventListener('click', event => { | ||
const target = event.target; | ||
const targetClassList = target.classList; | ||
|
||
if ( | ||
targetClassList.contains('btn-collapse') || | ||
targetClassList.contains('icon-collapse') || | ||
(target.tagName === 'use' && | ||
target.parentNode.classList.contains('icon-collapse')) | ||
) { | ||
const codeContainer = closestAncestor(target, '.code-container'); | ||
|
||
if (codeContainer) { | ||
const gatsbyCode = codeContainer.querySelector( | ||
'code[class^="gatsby-code-"]' | ||
); | ||
|
||
if (gatsbyCode) { | ||
const btnCollapse = codeContainer.querySelector( | ||
'.btn-collapse--collapse' | ||
); | ||
const btnExpand = codeContainer.querySelector( | ||
'.btn-collapse--expand' | ||
); | ||
|
||
if (gatsbyCode.style.display === 'none') { | ||
btnCollapse.classList.remove('hide'); | ||
btnExpand.classList.add('hide'); | ||
|
||
gatsbyCode.style.display = 'block'; | ||
} else { | ||
btnCollapse.classList.add('hide'); | ||
btnExpand.classList.remove('hide'); | ||
|
||
gatsbyCode.style.display = 'none'; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} |