Custom Components
Automatic Component Registration
Create components folder, write any Vue components. They will be registered automatically and you can even use them in your Markdown files.
If there are components with the same name as the theme and Valaxy, the order of overriding is user directory -> theme directory -> Valaxy client directory.
This also means that you can cover one component of the theme to achieve customizing the local theme!
Disable Default Registration
You can place components in folders other than components or in the components/.exclude folder.
You can also customize the exclusion rules.
import { defineValaxyConfig } from 'valaxy'
export default defineValaxyConfig({
/**
* @see https://github.com/unplugin/unplugin-vue-components#configuration
* `/[\\/]node_modules[\\/]/, ` Don't exclude components under node_modules/valaxy/client/components
*/
components: {
exclude: [/[\\/]\.git[\\/]/, /[\\/]\.exclude[\\/]/],
},
})Custom Override Theme Component
Based on this, you can easily customize the theme anywhere!
For example, custom footer:
In the components directory of the blog folder, create a new YunFooter.vue to overwrite the footer file of your theme.
You can replace the footer directly:
<template>
<div>Footer content</div>
</template>You can also inherit and extend the previous footer:
<script lang="ts" setup>
import YunFooter from 'valaxy-theme-yun/components/YunFooter.vue'
</script>
<template>
<YunFooter>
Customize footer content
</YunFooter>
</template>More Examples
Browser Template Compilation
Valaxy uses Vue’s runtime-only build by default. HTML excerpts (excerpt_type: html) and decrypted content load the template compiler when they are rendered. Pages without dynamic templates do not download it. Server-rendered excerpts remain visible while the compiler loads during hydration.
Use ValaxyDynamicComponent to render a template string with this behavior:
<template>
<ValaxyDynamicComponent :template-str="html" :data="{ frontmatter }" />
</template>The rendered event fires after the compiled component mounts, for work that depends on its DOM. As with Vue templates generally, only compile trusted content authored for your site.
If your own components use Vue’s template option or call compile() from vue directly, enable the eager compiler for compatibility:
export default defineValaxyConfig({
vue: {
// 'lazy' (default): built-in dynamic templates load the compiler on demand.
// true: include the compiler in the initial bundle.
// false: disable browser compilation, including dynamic excerpts/decryption.
browserTemplateCompilation: true,
},
})Lazy Components
Any auto-registered .vue component can be used with a Lazy prefix (for example, <MyChart /> -> <LazyMyChart />). Valaxy generates an on-demand wrapper that splits the target component into its own chunk and defers loading until it approaches the viewport (or is activated manually). If you define an explicit LazyMyChart.vue component file, your explicit component takes precedence over the generated wrapper.
All standard props, attributes, and non-placeholder slots are forwarded to the resolved component. The wrapper also accepts optional lazy-* props:
lazy-min-height: Minimum CSS height reserved before the component loads to avoid layout shift.lazy-root-margin:IntersectionObserverroot margin (default'600px 0px').lazy-eager: Load immediately on mount instead of waiting for viewport intersection.lazy-label: Accessible label displayed in the cold placeholder.lazy-inline: Render an inline<span>placeholder/trigger instead of a block card.
You can provide a custom #placeholder slot receiving { activate, status }:
<template>
<LazyMyChart lazy-min-height="240px" lazy-label="Traffic Chart" :data="chartData">
<template #placeholder="{ activate, status }">
<p>Interactive chart ({{ status }})</p>
<button type="button" @click="activate">
Load now
</button>
</template>
</LazyMyChart>
</template>SSR and Print Behavior
- SSR / SSG: Before hydration and intersection,
<Lazy* />renders its lightweight placeholder (#placeholderif provided, or the default load card / inline trigger), keeping the target component’s JS chunk out of the initial payload (and deferring its CSS chunk as well when Vitebuild.cssCodeSplitis enabled; by default Valaxy setsvite.build.cssCodeSplit: falseto bundle styles into a single CSS file). - Activation: Loading triggers automatically via
IntersectionObserver, manually on click/keyboard (Enter/Space), or onbeforeprint. - Print (
@media print): Unloadedmin-heightreservations collapse; bare unloaded placeholders are hidden;#placeholderslots hide the built-in header and placeholder<button>controls so their static content prints cleanly; static text inside the default slot renders as a print-only fallback; andlazy-inlinecontent remains visible inline. - HTML structure: Each
<Lazy* />renders a<div>wrapper (or<span>whenlazy-inlineis set). Do not place<Lazy* />directly inside structurally restricted HTML containers such as<table>,<tbody>,<thead>,<tr>,<ul>, or<ol>; wrap the outer table or list component instead.
Insert Busuanzi Statistics
Take valaxy-theme-yun as an example:
By default, operate in your blog folder.
Create a new YunFooter.vue under the components/ folder to customize the footer and display Busuanzi Statistics. You can customize its style freely according to your needs.
<script lang="ts" setup>
import { useScriptTag } from '@vueuse/core'
import YunFooter from 'valaxy-theme-yun/components/YunFooter.vue'
useScriptTag('//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js')
</script>
<template>
<YunFooter>
<!-- customize footer content -->
<div>Total visits of this site <span id="busuanzi_value_site_pv" /></div>
<div>Number of visitors <span id="busuanzi_value_site_uv" /></div>
</YunFooter>
</template>Create a new YunPostMeta.vue under the components/ folder to customize the information of each article and display the statistics of each article.
<script lang="ts" setup>
import type { Post } from 'valaxy'
import { useLayout } from 'valaxy'
import YunPostMeta from 'valaxy-theme-yun/components/YunPostMeta.vue'
defineProps<{
frontmatter: Post
}>()
// Display only in Post layout
const isPost = useLayout('post')
</script>
<template>
<YunPostMeta :frontmatter="frontmatter">
<span v-if="isPost" id="busuanzi_container_page_pv">
Total reading of this article <span id="busuanzi_value_page_pv" />
</span>
</YunPostMeta>
</template>The principle is to cover components. You can also freely cover any other components of the theme.
Other
valaxy-addon-components is also a plugin that makes full use of this mechanism. You can also refer to its implementation method and freely publish your custom components in the form of Valaxy plugin.
Because Vue components published is native, it will be completely on-demand when packaging, without your extra worry.
Example of using valaxy-addon-components to insert public components
To Be Continued.