Skip to content

Commit 3cb2581

Browse files
committed
feat: improve code block styles
1 parent bf17e6c commit 3cb2581

File tree

6 files changed

+143
-71
lines changed

6 files changed

+143
-71
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
# Changelog
44

5+
## 0.11.2
6+
7+
- Improved code block styles.
8+
59
## 0.11.1
610

711
- Fixed loading error on Figma slides and site pages.

components/Code.vue

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ const props = defineProps<{
1212
link?: string
1313
}>()
1414
15+
const content = useTemplateRef('content')
16+
useScrollbar(content, {
17+
overflow: {
18+
y: 'hidden'
19+
},
20+
scrollbars: {
21+
autoHideDelay: 0,
22+
clickScroll: true
23+
}
24+
})
25+
1526
const prismAlias: Record<string, string> = {
1627
vue: 'html'
1728
}
@@ -57,6 +68,7 @@ const highlighted = computed(() => {
5768
})
5869
5970
const code = computed(() => props.code)
71+
const lines = computed(() => props.code.split('\n').length)
6072
const copy = useCopy(code)
6173
6274
function handleCopy() {
@@ -74,40 +86,55 @@ function handleClick(event: MouseEvent) {
7486
<template>
7587
<section class="tp-code">
7688
<header class="tp-row tp-row-justify tp-code-header">
77-
{{ title }}
89+
<h3 class="tp-code-title">{{ title }}</h3>
7890
<div class="tp-row tp-gap">
7991
<slot name="actions" />
80-
<IconButton variant="secondary" title="Copy" @click="handleCopy">
92+
<IconButton title="Copy" @click="handleCopy">
8193
<Copy />
8294
</IconButton>
8395
</div>
8496
</header>
85-
<pre class="tp-code-content"><code v-html="highlighted" @click="handleClick"/></pre>
97+
<div class="tp-code-block">
98+
<div class="tp-code-line-numbers">
99+
<div class="tp-code-line-number" v-for="i in lines" :key="i">{{ i }}</div>
100+
</div>
101+
<pre
102+
class="tp-code-content"
103+
ref="content"
104+
><code v-html="highlighted" @click="handleClick"/></pre>
105+
</div>
86106
</section>
87107
</template>
88108

89109
<style scoped>
90-
.tp-code {
91-
border-radius: 2px;
92-
background-color: var(--color-bg-secondary);
93-
}
94-
95110
.tp-code-header {
96-
height: 40px;
97-
padding: 4px 4px 4px 12px;
98-
font-weight: 500;
99-
color: var(--color-text-secondary);
100-
user-select: none;
101-
cursor: default;
102-
min-width: 0;
111+
width: 100%;
112+
min-height: 32px;
113+
border-radius: var(--radius-medium) var(--radius-medium) 0 0;
114+
padding: 0;
115+
background-color: var(--color-bg);
103116
text-overflow: ellipsis;
104117
overflow: hidden;
105118
white-space: nowrap;
119+
user-select: none;
120+
cursor: default;
106121
}
107122
108-
.tp-code-content {
109-
border-top: 1px solid var(--color-border, rgba(0, 0, 0, 0.1));
110-
padding: 4px 12px;
123+
.tp-code-title {
124+
padding: 8px 0;
125+
font-weight: 400;
126+
color: var(--color-text);
127+
margin: 0;
128+
}
129+
130+
.tp-code-block {
131+
display: flex;
132+
flex-direction: row;
133+
background-color: transparent;
134+
border-radius: var(--radius-medium, 2px);
135+
box-shadow: 0 0 0 1px var(--color-border-code-well);
136+
margin: 1px 0;
137+
padding: 8px 0 0;
111138
-webkit-font-smoothing: antialiased;
112139
font-family:
113140
Roboto Mono,
@@ -118,9 +145,39 @@ function handleClick(event: MouseEvent) {
118145
line-height: 18px;
119146
font-size: 11px;
120147
letter-spacing: 0.005em;
148+
}
149+
150+
.tp-code-line-numbers {
151+
display: flex;
152+
flex-direction: column;
153+
min-width: 20px;
154+
border-radius: 2px 0 0 2px;
155+
flex-shrink: 0;
156+
margin-top: -8px;
157+
padding-top: 8px;
158+
border-right: 1px solid var(--color-border-code-well);
159+
}
160+
161+
.tp-code-line-number {
162+
min-width: 12px;
163+
color: var(--color-text-tertiary);
164+
display: flex;
165+
align-items: center;
166+
justify-content: end;
167+
padding-right: 4px;
168+
padding-left: 4px;
169+
}
170+
171+
.tp-code-content {
172+
padding-left: 6px;
173+
padding-bottom: 8px;
121174
overflow-x: auto;
122175
}
123176
177+
.tp-code-content:has(.os-scrollbar-visible) {
178+
padding-bottom: 16px;
179+
}
180+
124181
.tp-code-content::selection {
125182
background-color: var(--color-texthighlight, #0d99ff);
126183
}
@@ -139,7 +196,8 @@ function handleClick(event: MouseEvent) {
139196
color: var(--color-text);
140197
}
141198
142-
.tp-code-content .token.plain {
199+
.tp-code-content .token.plain,
200+
.tp-code-content .token.color {
143201
color: var(--color-codevalue);
144202
}
145203

components/Panel.vue

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
<script setup lang="ts">
2+
import { useScrollbar } from '@/composables/scrollbar'
23
import { ui } from '@/ui/figma'
34
import { options } from '@/ui/state'
45
import { useDraggable, useWindowSize, watchDebounced } from '@vueuse/core'
5-
import {
6-
OverlayScrollbars,
7-
ScrollbarsHidingPlugin,
8-
SizeObserverPlugin,
9-
ClickScrollPlugin
10-
} from 'overlayscrollbars'
11-
12-
OverlayScrollbars.plugin([ScrollbarsHidingPlugin, SizeObserverPlugin, ClickScrollPlugin])
136
147
const panel = useTemplateRef('panel')
158
const header = useTemplateRef('header')
169
const main = useTemplateRef('main')
1710
11+
useScrollbar(main, {
12+
overflow: {
13+
x: 'hidden'
14+
},
15+
scrollbars: {
16+
autoHide: 'leave',
17+
autoHideDelay: 0,
18+
clickScroll: true
19+
}
20+
})
21+
1822
const position = options.value.panelPosition
1923
const { x, y } = useDraggable(panel, {
2024
initialValue: {
@@ -68,29 +72,6 @@ if (position) {
6872
function toggleMinimized() {
6973
options.value.minimized = !options.value.minimized
7074
}
71-
72-
let os: OverlayScrollbars
73-
74-
onMounted(() => {
75-
if (!main.value) {
76-
return
77-
}
78-
79-
os = OverlayScrollbars(main.value, {
80-
overflow: {
81-
x: 'hidden'
82-
},
83-
scrollbars: {
84-
autoHide: 'leave',
85-
autoHideDelay: 0,
86-
clickScroll: true
87-
}
88-
})
89-
})
90-
91-
onUnmounted(() => {
92-
os?.destroy()
93-
})
9475
</script>
9576

9677
<template>
@@ -150,23 +131,3 @@ onUnmounted(() => {
150131
border-radius: var(--radius-large);
151132
}
152133
</style>
153-
154-
<style>
155-
.os-scrollbar {
156-
--os-track-bg-hover: var(--color-scrollbartrackhover);
157-
--os-track-bg-active: var(--color-scrollbartrackdrag);
158-
--os-handle-bg: var(--color-scrollbar);
159-
--os-handle-bg-hover: var(--color-scrollbar);
160-
--os-handle-bg-active: var(--color-scrollbar);
161-
}
162-
163-
.os-scrollbar-vertical:hover::before {
164-
content: '';
165-
position: absolute;
166-
top: 0;
167-
bottom: 0;
168-
left: -1px;
169-
width: 1px;
170-
background-color: var(--color-border);
171-
}
172-
</style>

composables/scrollbar.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
OverlayScrollbars,
3+
ScrollbarsHidingPlugin,
4+
SizeObserverPlugin,
5+
ClickScrollPlugin
6+
} from 'overlayscrollbars'
7+
8+
OverlayScrollbars.plugin([ScrollbarsHidingPlugin, SizeObserverPlugin, ClickScrollPlugin])
9+
10+
export function useScrollbar(
11+
container: MaybeRefOrGetter<HTMLElement | null>,
12+
scrollOptions: Parameters<typeof OverlayScrollbars>[1]
13+
) {
14+
let os: OverlayScrollbars | null = null
15+
16+
watchEffect(
17+
(onCleanup) => {
18+
const el = toValue(container)
19+
if (!el) {
20+
return
21+
}
22+
23+
os = OverlayScrollbars(el, scrollOptions)
24+
25+
onCleanup(() => {
26+
os?.destroy()
27+
})
28+
},
29+
{ flush: 'post' }
30+
)
31+
}

entrypoints/ui/style.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,21 @@ tempad ::-webkit-scrollbar-thumb {
165165
~ tempad {
166166
display: none;
167167
}
168+
169+
.os-scrollbar {
170+
--os-track-bg-hover: var(--color-scrollbartrackhover);
171+
--os-track-bg-active: var(--color-scrollbartrackdrag);
172+
--os-handle-bg: var(--color-scrollbar);
173+
--os-handle-bg-hover: var(--color-scrollbar);
174+
--os-handle-bg-active: var(--color-scrollbar);
175+
}
176+
177+
.os-scrollbar-vertical:hover::before {
178+
content: "";
179+
position: absolute;
180+
top: 0;
181+
bottom: 0;
182+
left: -1px;
183+
width: 1px;
184+
background-color: var(--color-border);
185+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tempad-dev",
33
"description": "Inspect panel on Figma, for everyone.",
44
"private": true,
5-
"version": "0.11.1",
5+
"version": "0.11.2",
66
"type": "module",
77
"scripts": {
88
"dev": "wxt",

0 commit comments

Comments
 (0)