Skip to content

Commit

Permalink
feat: support layouts (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
JohnCampionJr and antfu committed Feb 12, 2021
1 parent 8dae6f9 commit 8322503
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -38,6 +38,7 @@
"vite-plugin-md": "^0.4.2",
"vite-plugin-pages": "^0.3.0",
"vite-plugin-pwa": "^0.4.6",
"vite-plugin-vue-layouts": "^0.1.0",
"vite-ssg": "^0.8.4"
},
"eslintConfig": {
Expand Down
24 changes: 17 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions src/App.vue
Expand Up @@ -13,8 +13,5 @@ useHead({
</script>

<template>
<main class="px-4 py-10 text-center text-gray-700 dark:text-gray-200">
<router-view />
<Footer />
</main>
<router-view />
</template>
26 changes: 26 additions & 0 deletions src/layouts/404.vue
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
const router = useRouter()
const { t } = useI18n()
</script>

<template>
<main class="px-4 py-10 text-center text-teal-700 dark:text-gray-200">
<div>
<p class="text-4xl">
<carbon-warning class="inline-block" />
</p>
</div>
<router-view />
<div>
<button
class="btn m-3 text-sm mt-8"
@click="router.back()"
>
{{ t('button.back') }}
</button>
</div>
</main>
</template>
14 changes: 14 additions & 0 deletions src/layouts/README.md
@@ -0,0 +1,14 @@
## Layouts

Vue components in this dir are used as layouts.

By default, `default.vue` will be used unless an alternative is specified in the route meta.

With [`vite-plugin-pages`](https://github.com/hannoeru/vite-plugin-pages) and [`vite-plugin-vue-layouts`](https://github.com/JohnCampionJr/vite-plugin-vue-layouts), you can specify the layout in the page's SFCs like this:

```html
<route lang="yaml">
meta:
layout: home
</route>
```
9 changes: 9 additions & 0 deletions src/layouts/default.vue
@@ -0,0 +1,9 @@
<template>
<main class="px-4 py-10 text-center text-teal-700 dark:text-gray-200">
<router-view />
<Footer />
<div class="mt-5 m-auto text-center opacity-25 text-sm">
[Default Layout]
</div>
</main>
</template>
9 changes: 9 additions & 0 deletions src/layouts/home.vue
@@ -0,0 +1,9 @@
<template>
<main class="px-4 py-10 text-center text-teal-700 dark:text-gray-200">
<router-view />
<Footer />
<div class="mt-5 m-auto text-center opacity-25 text-sm">
[Home Layout]
</div>
</main>
</template>
5 changes: 4 additions & 1 deletion src/main.ts
@@ -1,8 +1,11 @@
import './styles/main.postcss'
import routes from 'pages-generated'
import { ViteSSG } from 'vite-ssg'
import generatedRoutes from 'pages-generated'
import { setupLayouts } from 'layouts-generated'
import App from './App.vue'

const routes = setupLayouts(generatedRoutes)

// https://github.com/antfu/vite-ssg
export const createApp = ViteSSG(
App,
Expand Down
5 changes: 5 additions & 0 deletions src/pages/[...all].vue
Expand Up @@ -9,3 +9,8 @@ const { t } = useI18n()
{{ t('not-found') }}
</div>
</template>

<route lang="yaml">
meta:
layout: 404
</route>
5 changes: 5 additions & 0 deletions src/pages/index.vue
Expand Up @@ -53,3 +53,8 @@ const { t } = useI18n()
</div>
</div>
</template>

<route lang="yaml">
meta:
layout: home
</route>
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -15,7 +15,8 @@
"forceConsistentCasingInFileNames": true,
"types": [
"vite/client",
"vite-plugin-pages/client"
"vite-plugin-pages/client",
"vite-plugin-vue-layouts/client"
],
"paths": {
"~/*": ["src/*"]
Expand Down
4 changes: 4 additions & 0 deletions vite.config.ts
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Pages from 'vite-plugin-pages'
import Layouts from 'vite-plugin-vue-layouts'
import ViteIcons, { ViteIconsResolver } from 'vite-plugin-icons'
import ViteComponents from 'vite-plugin-components'
import Markdown from 'vite-plugin-md'
Expand All @@ -23,6 +24,9 @@ export default defineConfig({
extensions: ['vue', 'md'],
}),

// https://github.com/JohnCampionJr/vite-plugin-vue-layouts
Layouts(),

// https://github.com/antfu/vite-plugin-md
Markdown({
wrapperClasses: 'prose prose-sm m-auto text-left',
Expand Down

0 comments on commit 8322503

Please sign in to comment.