Recently, more and more of my blogs have been including code as part of what they are talking about. An example of this was a blog about aligning the colors of a Tableau file with a field, where I had two code snippets related to overwriting files using Python.
Before, I had been using markdown formatting to display the code, but I found it a bit harder to read due to word wrapping. It also was more annoying when wanting to copy it, as the word wrapping could lead to issues there. So I wanted a better way to display my code.
A lot of other websites that talk about code and share what they've made usually use some kind of a container to hold the code, with the ability to copy it at ease. And given that it would be written in a combination of HTML, CSS and Javascript, I should be able to create a code block and imbed it into my blogs.
To start, I looked at what features I needed for these code blocks: titles for the code, a button to copy, and a container to hold it that doesn't wrap. Having the ability to format it to fit in better with our website's style would be good as well.
The first avenue I tried was Github's Gists, which is a service that allows for smaller snippets of code to be published rather than needing an entire repository to share it. It's simple to set up, with the ability to limit access and helps to promote my own portfolio on Github.
View this gist on gist.github.comAbove should be the Gists with the code that I'll be sharing in the rest of this blog. I found that it sometimes worked and sometimes didn't, and also didn't have an easy way to copy the code to clipboard. The Gists also didn't fit as well into the style of our website. So I decided to try my hand at writing my own. I used some inspiration from other websites for how I should format it and let myself learn some more HTML, CSS and Javascript.
With plenty of resources online to learn the key terms and properties that I wanted to use for my code block, I was able to slowly put it together. Trying to get it to look nice isn't one of my strong suits, so I focused on the functionality first. Javascript was the first programming language I ever started to learn all the way back at the end of 5th grade, so I was nice to get into that again.
A lot of tinkering was needed for the formatting, especially for the more interactive bits related to the copy button. The issue I ran into there was that there wasn't a way to update the style of the button's hover pseudo-class when it's been clicked. The way I got around it was to not use the hover styling in CSS and rather use Javascript functions to edit the style when it's hovering or not, reading the test of the button to determine if it has been clicked or not. It helps to make the interaction with the code block just that little bit nicer.
Putting it all together, I was able to get to the following code block, holding the code that generates itself.
<style>
.code-block {
margin: 15px;
position: relative;
border-radius: 10px;
box-shadow: 0px 0px 10px #333333;
overflow: hidden;
}
.code-header {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #CCCCCC;
border-top-left-radius: inherit;
border-top-right-radius: inherit;
padding: 10px 15px 10px 15px;
}
.code-body {
text-align: left;
}
.code-body-holder {
border-bottom-left-radius: inherit;
border-bottom-right-radius: inherit;
margin: 0;
overflow-x: auto;
padding: 0px 10px 0px 10px;
background-color: #FFFFFF;
}
.copy-button {
border-radius: 5px;
border: none;
padding: 4px;
background-color: #0194FE;
min-width: 80px;
color: #FFFFFF;
}
</style>
<script>
function copyCode(self) {
var code = self.parentNode.parentNode.getElementsByTagName("pre")[0].getElementsByTagName("code")[0];
navigator.clipboard.writeText(code.innerText);
self.style.backgroundColor = "#E97D32";
self.innerText = "Copied!";
setTimeout(() => {
self.innerText = "Copy Code";
self.style.backgroundColor = "#0194FE";
}, 2500);
}
function hoveringButton(self) {
if(self.innerText == "Copy Code") {
self.style.backgroundColor = "#40aefe";
} else {
self.style.backgroundColor = "#e8a678";
}
}
function resetButtonColor(self) {
if(self.innerText == "Copy Code") {
self.style.backgroundColor = "#0194FE";
} else {
self.style.backgroundColor = "#E97D32";
}
}
</script>
<div class="code-block">
<div class="code-header">
<span class="code-title">Your Code Title Here</span>
<button class="copy-button" onclick="copyCode(this)" onmouseover="hoveringButton(this)" onmouseout="resetButtonColor(this)">Copy Code</button>
</div>
<pre class="code-body-holder">
<code class="code-body">
Your Code Here
</code>
</pre>
</div>
Unfortunately, when I went to put it into my blogs, I ran into some more issues. The blog had a hard time handling the script and style tags when embedded, and attempting to put them in the blog's code injection didn't work either. So I needed another solution.
This was to contain the style and script in the HTML elements themselves. Luckily, I had written the Javascript to work relative to the elements, rather than trying to pull it from an element ID. This meant that the transfer to putting the style and script into the different elements directly was smooth. In the end, I ended up with the code block below, which is what I use when I put the code blocks into these blogs.
<div style="
margin: 15px;
position: relative;
border-radius: 10px;
box-shadow: 0px 0px 10px #333333;
overflow: hidden;
">
<div style="
display: flex;
justify-content: space-between;
align-items: center;
background-color: #CCCCCC;
border-top-left-radius: inherit;
border-top-right-radius: inherit;
padding: 10px 15px 10px 15px;">
<span>Your Code Title Here</span>
<button style="border-radius: 5px;
border: none;
padding: 4px;
background-color: #0194FE;
min-width: 80px;
color: #FFFFFF;"
onclick="
var code = this.parentNode.parentNode.getElementsByTagName('pre')[0].getElementsByTagName('code')[0];
navigator.clipboard.writeText(code.innerText);
this.style.backgroundColor = '#E97D32';
this.innerText = 'Copied!';
setTimeout(() => {
this.innerText = 'Copy Code';
this.style.backgroundColor = '#0194FE';
}, 2500);"
onmouseover="
if(this.innerText == 'Copy Code') {
this.style.backgroundColor = '#40aefe';
} else {
this.style.backgroundColor = '#e8a678';
}"
onmouseout="
if(this.innerText == 'Copy Code') {
this.style.backgroundColor = '#0194FE';
} else {
this.style.backgroundColor = '#E97D32';
}"
>Copy Code</button>
</div>
<pre style="
border-bottom-left-radius: inherit;
border-bottom-right-radius: inherit;
margin: 0;
overflow-x: auto;
padding: 0px 10px 0px 10px;
background-color: #FFFFFF;
">
<code style="text-align: left;">
Your Code Here
</code>
</pre>
</div>

