Skip to content

Commit a8178a1

Browse files
committed
Code highlightjs toggle theme
1 parent 51ea27a commit a8178a1

File tree

10 files changed

+130
-105
lines changed

10 files changed

+130
-105
lines changed

resources/js/assets/code.css

Lines changed: 0 additions & 66 deletions
This file was deleted.

resources/js/assets/main.css

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
@import './code.css';
2-
31
h1 {
42
float: left;
53
width: 100%;
@@ -8,7 +6,7 @@ h1 {
86

97
.btn {
108
cursor: pointer;
11-
display: inline-block;
9+
display: inline-block;
1210
font-weight: 500;
1311
line-height: 1.5;
1412
color: #212529;
@@ -17,26 +15,26 @@ h1 {
1715
vertical-align: middle;
1816
user-select: none;
1917
background-color: transparent;
20-
border: 1px solid transparent;
21-
padding: .375rem .75rem;
18+
border: 1px solid transparent;
19+
padding: 0.375rem 0.75rem;
2220
font-size: 1rem;
23-
border-radius: .25rem;
24-
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
21+
border-radius: 0.25rem;
22+
transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
2523
}
2624

2725
.btn-primary {
2826
color: #fff;
2927
background-color: var(--accent-primary); /* #C51B2A; */
3028
border-color: var(--accent-primary); /* #C51B2A; */
31-
padding: .5rem 2rem;
29+
padding: 0.5rem 2rem;
3230
min-width: 130px;
3331
}
3432

3533
.btn-secondary {
3634
color: #fff;
3735
background-color: #212529;
3836
border-color: #212529;
39-
padding: .5rem 2rem;
37+
padding: 0.5rem 2rem;
4038
min-width: 130px;
4139
}
4240

@@ -46,10 +44,10 @@ h1 {
4644
}
4745

4846
.mx-2 {
49-
margin-right: .5rem !important;
50-
margin-left: .5rem !important;
47+
margin-right: 0.5rem !important;
48+
margin-left: 0.5rem !important;
5149
}
5250

5351
.btn:hover {
5452
filter: brightness(1.2);
55-
}
53+
}

resources/js/components/utils/CodeHighlight/CodeHighlight.vue

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import 'highlight.js/styles/github.min.css'
1111
// import 'highlight.js/styles/mono-blue.min.css'
1212
1313
// Dark set from current dir css
14-
import './css/hybrid-dark.css'
15-
// import './css/atom-one-dark.css'
16-
// import './css/an-old-hope-dark.css'
17-
// import './css/srcery-dark.css'
18-
// import './css/kimbie-dark.css'
14+
import './css/dark/hybrid-dark.css'
15+
// import './css/dark/atom-one-dark.css'
16+
// import './css/dark/an-old-hope-dark.css'
17+
// import './css/dark/srcery-dark.css'
18+
// import './css/dark/kimbie-dark.css'
1919
2020
import hljs from 'highlight.js/lib/common'
2121
import hljsVuePlugin from '@highlightjs/vue-plugin'
@@ -24,6 +24,7 @@ const props = defineProps({
2424
code: { default: '<?php echo "Insert code here...";' },
2525
language: { type: String, default: 'php' },
2626
theme: { type: String, default: '' },
27+
filename: { type: String, default: '' },
2728
})
2829
2930
const highlightjs = hljsVuePlugin.component
@@ -42,30 +43,14 @@ function toggleTheme() {
4243
</script>
4344

4445
<template>
45-
<div class="code-topbar">
46-
<button class="toggle-code-theme" @click="toggleTheme">{{ $t('Change Theme') }}</button>
46+
<div class="hljs-code-topbar">
47+
<span class="hljs-code-filename">{{ props.filename }}</span>
48+
<button v-if="theme == ''" class="hljs-code-toggle-theme" @click="toggleTheme"><i class="fa-solid fa-toggle-off"></i></button>
49+
<button v-else class="hljs-code-toggle-theme" @click="toggleTheme"><i class="fa-solid fa-toggle-on"></i></button>
4750
</div>
4851
<highlightjs :class="theme" :code="props.code" :language="props.language" />
4952
</template>
5053

5154
<style>
52-
.code-topbar {
53-
display: block;
54-
overflow: hidden;
55-
margin: 10px auto;
56-
}
57-
.toggle-code-theme {
58-
float: right;
59-
font-size: 14px;
60-
padding: 10px 20px;
61-
color: var(--text-primary);
62-
border-radius: var(--border-radius);
63-
background: transparent;
64-
border: 2px solid var(--code-border);
65-
cursor: pointer;
66-
}
67-
.toggle-code-theme:hover {
68-
color: var(--accent-primary);
69-
border: 2px solid var(--accent-primary);
70-
}
55+
@import url('./css/code.css');
7156
</style>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&display=swap');
2+
3+
:root {
4+
/* code highlighterjs */
5+
--code-font-family: 'JetBrains Mono', monospace, consolas;
6+
--code-font-size: 14px;
7+
--code-border: #00339922;
8+
--code-line-numbers: #6885ba;
9+
--code-line-border: #ff2233aa;
10+
--code-line-underline: #00339922;
11+
--code-filename: var(--accent-primary); /* #d73a49; #d30040; */
12+
}
13+
14+
pre {
15+
margin: 20px 0px;
16+
}
17+
18+
code,
19+
code.hljs {
20+
font-size: var(--code-font-size);
21+
font-family: var(--code-font-family);
22+
border: 1px solid var(--code-border);
23+
border-radius: var(--border-radius);
24+
padding: 10px !important;
25+
overflow-y: hidden !important;
26+
overflow-x: auto !important;
27+
}
28+
29+
/* for block of numbers */
30+
.hljs-ln {
31+
float: left;
32+
width: 100%;
33+
font-size: var(--code-font-size);
34+
font-family: var(--code-font-family);
35+
}
36+
37+
.hljs-ln tbody {
38+
float: left;
39+
width: 100% !important;
40+
border: 1px solid var(--code-border);
41+
}
42+
43+
.hljs-ln tbody tr {
44+
display: flex-table;
45+
border-bottom: 1px solid var(--code-line-underline);
46+
}
47+
48+
.hljs-ln-numbers {
49+
-webkit-touch-callout: none;
50+
-webkit-user-select: none;
51+
-khtml-user-select: none;
52+
-moz-user-select: none;
53+
-ms-user-select: none;
54+
user-select: none;
55+
text-align: center;
56+
color: var(--code-line-numbers);
57+
border-right: 1px solid var(--code-line-border);
58+
vertical-align: top;
59+
padding: 5px 10px !important;
60+
61+
/* your custom style here */
62+
/*min-width: 60px;*/
63+
}
64+
65+
/* for block of code */
66+
.hljs-ln-code {
67+
width: 100% !important;
68+
padding-left: 10px !important;
69+
line-height: 24px;
70+
}
71+
72+
/* toggle theme button */
73+
.hljs-code-topbar {
74+
float: left;
75+
width: 100%;
76+
padding: 5px;
77+
margin: 20px auto 10px auto;
78+
border-radius: var(--border-radius);
79+
border: 1px solid var(--code-border);
80+
background: transparent;
81+
overflow: hidden;
82+
}
83+
84+
.hljs-code-filename {
85+
float: left;
86+
padding: 10px 20px;
87+
color: var(--code-filename);
88+
font-family: var(--code-font-family);
89+
font-weight: 700;
90+
}
91+
92+
.hljs-code-toggle-theme {
93+
float: right;
94+
font-size: 14px;
95+
padding: 10px 20px;
96+
color: var(--placeholder);
97+
border-radius: var(--border-radius);
98+
border: 1px solid var(--code-border);
99+
background: transparent;
100+
cursor: pointer;
101+
}
102+
103+
.hljs-code-toggle-theme:hover {
104+
color: var(--accent-primary);
105+
border: 1px solid var(--accent-primary);
106+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

resources/js/views/demo/CodeView.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script setup>
22
import CodeHighlight from '@/components/utils/CodeHighlight/CodeHighlight.vue'
3+
import TopMenu from '@/views/page/menu/TopMenu.vue'
34
45
const code = `<?php
56
@@ -52,8 +53,9 @@ class User extends Authenticatable
5253
</script>
5354

5455
<template>
56+
<TopMenu />
5557
<div class="section">
5658
<!-- <CodeHighlight lang="php" :code="code" theme="dark-theme" /> -->
57-
<CodeHighlight lang="php" :code="code" />
59+
<CodeHighlight lang="php" :code="code" filename="App\Models\User.php" />
5860
</div>
5961
</template>

0 commit comments

Comments
 (0)