Skip to content

Commit 95446b9

Browse files
committed
refactor: make the props computed values
when props change in the parent element, it will change here too
1 parent 0eb0a87 commit 95446b9

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

Diff for: src/components/code-block/CodeBlock.vue

+39-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ref="codeBlock"
88
:is="props.asElement || 'pre'"
99
v-bind="$attrs">
10-
<!-- Filename Component -->
10+
<!-- Header Component -->
1111
<div class=""></div>
1212

1313
<!-- Line Component -->
@@ -32,9 +32,15 @@
3232
</template>
3333

3434
<script setup lang="ts">
35-
import { defineComponent, onMounted, Ref, ref, toRefs, watch } from 'vue';
36-
// @ts-ignore
37-
35+
import {
36+
computed,
37+
defineComponent,
38+
onMounted,
39+
Ref,
40+
ref,
41+
toRefs,
42+
watch
43+
} from 'vue';
3844
import {
3945
codeBlockProps,
4046
codeBlockPropsTypes,
@@ -46,17 +52,29 @@
4652
import { addThemeToCodeBlock, themes } from '../themes';
4753
4854
const props = defineProps(codeBlockProps());
49-
const code = ref<string[]>(parseCodeIntoLines(props.code, props.language));
5055
const codeBlock: Ref<HTMLDivElement | null> = ref(null);
5156
57+
const code = computed(() => parseCodeIntoLines(props.code, props.language));
58+
const currentTheme = computed(() => themes[props.theme]);
59+
5260
onMounted(() => {
53-
addThemeToCodeBlock(codeBlock.value, themes[props.theme]);
61+
addThemeToCodeBlock(codeBlock.value, currentTheme.value);
5462
});
5563
56-
watch(props, () => {
57-
code.value = parseCodeIntoLines(props.code, props.language);
58-
addThemeToCodeBlock(codeBlock.value, themes[props.theme]);
59-
});
64+
watch(
65+
() => props.theme,
66+
() => {
67+
addThemeToCodeBlock(codeBlock.value, currentTheme.value);
68+
}
69+
);
70+
71+
// watch(
72+
// () => props.language,
73+
// () => {
74+
// addThemeToCodeBlock(codeBlock.value, currentTheme.value);
75+
// }
76+
// );
77+
6078
defineComponent<codeBlockInstance>({
6179
name: 'CodeBlock',
6280
props: codeBlockProps(),
@@ -68,6 +86,17 @@
6886
</script>
6987

7088
<style>
89+
*,
90+
*::after,
91+
*::before {
92+
box-sizing: border-box;
93+
padding: 0;
94+
margin: 0;
95+
}
96+
body {
97+
padding: 15px;
98+
}
99+
71100
.vuejs-code-block pre {
72101
padding: 1rem;
73102
border-radius: 0.25rem;

0 commit comments

Comments
 (0)