Skip to content

Commit

Permalink
chore(storybook): Fix display of overview copy buttons (#551)
Browse files Browse the repository at this point in the history
* chore(storybook): Fix display of overview copy buttons

* Update DOMLoaded handler to use interval
  • Loading branch information
ethanWallace authored Jun 12, 2024
1 parent d98f671 commit e4304a3
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions packages/web/.storybook/preview-head.html
Original file line number Diff line number Diff line change
Expand Up @@ -705,17 +705,18 @@
createCopyBtn(toggleCode, stringReact, '', 'React', 'React');
} else {
// If not expanded, remove existing copy buttons
const copyBtns = document.querySelectorAll('.copy-code-btn');
const copyBtns = toggleCode.closest('.css-25bv1u').querySelectorAll('.copy-code-btn');
copyBtns.forEach(btn => btn.remove());
}
}

// Add DOMContentLoaded event
function domContentLoadedHandler() {
setTimeout(() => {
const domLoadInterval = setInterval(() => {
const toggleCodes = document.querySelectorAll('.docblock-code-toggle');

if (toggleCodes.length > 0) {
clearInterval(domLoadInterval);
// Toggle the display of copy buttons based on the toggle code button state
toggleCodes.forEach(toggleCode => {
toggleCopyBtnsDisplay(toggleCode);
Expand All @@ -726,19 +727,11 @@
});
});
}
}, 1000);

// Remove DOMContentLoaded event listener to avoid execution for each story on the page
document.removeEventListener('DOMContentLoaded', domContentLoadedHandler);
}, 100);
}

// Add event listener to execute domContentLoadedHandler function when DOM content is loaded
document.addEventListener('DOMContentLoaded', domContentLoadedHandler);

// Add event listener to execute domContentLoadedHandler for click events
document.addEventListener('click', function () {
document.addEventListener('DOMContentLoaded', domContentLoadedHandler);
});

// ----- End custom copy code buttons -----
</script>

0 comments on commit e4304a3

Please sign in to comment.