Skip to content

Commit

Permalink
scoped style (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkorland authored Dec 8, 2024
1 parent dcb21b4 commit e3e7de9
Showing 1 changed file with 89 additions and 88 deletions.
177 changes: 89 additions & 88 deletions _includes/code_tabs.html
Original file line number Diff line number Diff line change
@@ -1,98 +1,99 @@
<style>
.tabs input[type="radio"] {
position: absolute;
opacity: 0;
pointer-events: none;
}

.tabs label {
display: inline-block;
padding: 10px;
cursor: pointer;
}

.tabs label:hover {
background: #31343f;
}

.tabs input[type="radio"]:checked+label {
background: #31343f;
border-bottom: 1px solid transparent;
}

.tabs .tab-content {
background: #31343f;
padding: 10px;
}

.tabs .tab-content pre {
margin: 0;
overflow-x: auto;
}

.tabs .tab-content code {
display: block;
white-space: pre;
}

.tabs input[type="radio"]:nth-of-type(1):checked~.tab-content:nth-of-type(1),
.tabs input[type="radio"]:nth-of-type(2):checked~.tab-content:nth-of-type(2),
.tabs input[type="radio"]:nth-of-type(3):checked~.tab-content:nth-of-type(3),
.tabs input[type="radio"]:nth-of-type(4):checked~.tab-content:nth-of-type(4),
.tabs input[type="radio"]:nth-of-type(5):checked~.tab-content:nth-of-type(5),
.tabs input[type="radio"]:nth-of-type(6):checked~.tab-content:nth-of-type(6) {
display: block;
}

.tabs .tab-content {
display: none;
}

.code-container {
position: relative;
}

.copy-btn {
position: absolute;
top: 10px;
right: 10px;
padding: 5px 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
display: none;
}

.code-container:hover .copy-btn {
display: inline-block;
}

.copy-btn:hover {
background-color: #0056b3;
}
</style>

<div class="tabs">
{% assign codes = "python,javascript,cpp,rust,java,bash" | split: "," %}

{% for code_name in codes %}
{% if include[code_name] %}
<input type="radio" id="tab{{ include.id }}{{ forloop.index }}" name="tab" {% if forloop.first %}checked{% endif %}>
<label for="tab{{ include.id }}{{ forloop.index }}">{{ code_name }}</label>
{% endif %}
{% if include[code_name] %}
<input type="radio" id="tab{{ include.id }}{{ forloop.index }}" name="tab" {% if forloop.first %}checked{% endif %}>
<label for="tab{{ include.id }}{{ forloop.index }}">{{ code_name }}</label>
{% endif %}
{% endfor %}

{% for code_name in codes %}
{% if include[code_name] %}
<div class="tab-content">
<div class="code-container">
<button class="copy-btn"
onclick="navigator.clipboard.writeText(this.nextElementSibling.firstElementChild.innerText)">Copy</button>
<pre><code class="language-{{ code_name }}">{{ include[code_name] | escape }}</code></pre>
</div>
{% if include[code_name] %}
<div class="tab-content">
<div class="code-container">
<button class="copy-btn"
onclick="navigator.clipboard.writeText(this.nextElementSibling.firstElementChild.innerText)">Copy</button>
<pre><code class="language-{{ code_name }}">{{ include[code_name] | escape }}</code></pre>
</div>
{% endif %}
</div>
{% endif %}
{% endfor %}

<style scoped>
input[type="radio"] {
position: absolute;
opacity: 0;
pointer-events: none;
}

label {
display: inline-block;
padding: 10px;
cursor: pointer;
}

label:hover {
background: #31343f;
}

input[type="radio"]:checked+label {
background: #31343f;
border-bottom: 1px solid transparent;
}

.tab-content {
background: #31343f;
padding: 10px;
}

.tab-content pre {
margin: 0;
overflow-x: auto;
}

.tab-content code {
display: block;
white-space: pre;
}

input[type="radio"]:nth-of-type(1):checked~.tab-content:nth-of-type(1),
input[type="radio"]:nth-of-type(2):checked~.tab-content:nth-of-type(2),
input[type="radio"]:nth-of-type(3):checked~.tab-content:nth-of-type(3),
input[type="radio"]:nth-of-type(4):checked~.tab-content:nth-of-type(4),
input[type="radio"]:nth-of-type(5):checked~.tab-content:nth-of-type(5),
input[type="radio"]:nth-of-type(6):checked~.tab-content:nth-of-type(6) {
display: block;
}

.tab-content {
display: none;
}

.code-container {
position: relative;
}

.copy-btn {
position: absolute;
top: 10px;
right: 10px;
padding: 5px 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
display: none;
}

.code-container:hover .copy-btn {
display: inline-block;
}

.copy-btn:hover {
background-color: #0056b3;
}
</style>

</div>

0 comments on commit e3e7de9

Please sign in to comment.