Quasar is a popular Vue framework with a large component library and best-in-class support for building apps for web/mobile/browser extension/electron/etc. from the same codebase. Yet, despite significant community interest, it has been difficult to find solid, versatile answers for how to create custom component libraries based on Quasar. This tutorial provides those answers, demonstrating how to create a standalone Quasar component library which can be used in any Vue application, whether or not that app runs via the Quasar CLI, the Vue CLI, or another tool.
History of community interest in custom Quasar component libraries (2019-2023)
23 August 2019 - Quasar Issue #4956
OP: Is there any way to build a quasar project as a standalone component (SFC (?)) to be used in another project, using the quasar cli preferably, or if it is not possible with it, any other way?
Maintainer: The purpose of a project folder is not to build a component, but a website/app.
10 September 2019 - Quasar Issue #5063
OP: I built the following scenario without full success:
- Project A: A vue-cli + quasar (as plugin)
- Project B: A plain Vue-cli project that includes Project A as JS dependency
Maintainer: Quasar CLI’s purpose (and even Vue CLI’s purpose) is not to build a component library, but to build a website/app.
1 March 2020 - Quasar Issue #5063
Here is a basic use case : we have multiple Vue apps, and are creating a centralized component library imported by all the apps. These components have dependencies on multiple Quasar components. . After adding Quasar as a plugin, … and importing it in other apps, it results in conflicts
12 March 2020 - Quasar Issue #6604
OP: When having more than one Vue application, a good practice is to put common components in an external component library. But it seems that putting Quasar components in an external library and using it in a Quasar app is not possible currently (or at least is not documented)
Quasar Collaborator: Your components are either
.jsfiles or.vuefiles. You can copy them in any new project. I think you mean something and I don’t get it. Can you explain why the copy/paste of the files with components is not good?OP: Just to be sure : you are suggesting me that, every time we make an update on a component, we should copy / paste it manually on our 15 others apps ?
We are a team of 8, sometimes working on the same components at the same time, it will result in versioning nightmare. Refactoring components in a single, versionable, external lib is a key feature of Vue We were thinking about migrating, but we cannot use Quasar with such a constraint
20 February 2023 - StackOverflow
I am creating a library that contains some company standard components. It is built upon Quasar components. When I try to use the library it gives the following error: [Vue warn]: Failed to resolve component: q-input.
Why build a custom Quasar component library?
For sharing components between multiple Quasar projects, the official recommendation is to create a Quasar App Extension. App Extensions require the Quasar CLI, which beneficially removes tons of pain from creating a web app/mobile app/browser extension/electron app/etc., but unfortunately brings multiple downsides:
- Framework Lock-In: Using the Quasar CLI for your project tightly couples your code to the Quasar framework.
- Reduced Testability: Quasar supports Cypress as an App Extension, but other common testing tools still have significant limitations: Jest has an unsupported beta package , Vitest support is still in Alpha, and the main testing page is full of deprecation notices with no replacements. Community efforts to support popular sandboxed component development tools like Storybook or Histoire have been blocked by the difficulty of replicating Quasar CLI’s significant boot complexity within the sandboxed environment.
- Reduced Ecosystem Interoperability: The Quasar CLI requirement prevents Quasar App Extensions from being used in vanilla Vue projects, further limiting their reach.
- Delayed Access to Ecosystem Improvements: When using the Quasar CLI, access to many ecosystem-wide innovations must wait until the Quasar CLI adds official support. Sometimes that support takes years to arrive. For example, it took until February 2024 for Vite >1.0 support to reach a usable beta stage, nearly two full years after the community raised the need and began developing solutions.
For projects which do not have a strong need to share a nearly identical experience across Web, Mobile, Browser Extension, and/or Electron (probably rare given the dramatically different use cases for each environment), these downsides of the Quasar CLI are significant, even prohibitive.
This tutorial for creating a custom, standalone Quasar component library mitigates those downsides by answering the following questions:
- How can one use Quasar components (including custom derivatives) in a vanilla Vue project?
- How can one improve the testability of a project managed by the Quasar CLI?
How to create a custom Quasar component library
Create a vanilla Vue Component Library
A custom Quasar component library is just a vanilla Vue Component Library with Quasar added on top.
This tutorial assumes you already have an existing Vue Component Library. If not, I strongly recommend following my tutorial Creating a custom Vue component library with Storybook and Vitest. You can also reference the excellent tutorials by FreeCodeCamp and LogRocket.
Install Quasar as a Vue CLI plugin
Quasar’s official docs for their Vue CLI Quasar Plugin start with a scary-looking warning banner. The concerns expressed in that warning refer to building Quasar applications with the Vue CLI, and thus do not apply to our component library.
Using the Vue CLI to manage your component library avoids all the limitations of the Quasar CLI. Additionally, if you use your Vue CLI-managed component library within a larger Quasar CLI managed application, you can still retain the cross-platform benefits of the Quasar CLI.
While Quasar’s docs include instructions for configuring the Vue CLI Quasar Plugin, the installer assumes you are modifying a full Vue application, so it takes many steps which are unnecessary for a component library. As such, this section will detail a manual installation (which is ultimately simpler). If you prefer to install Quasar via the Vue CLI (e.g. to compare the official installation with these manual instructions) feel free to expand the following details note:
How to install Quasar via the Vue CLI (reference-only)
Verify that you have Vue CLI v4.5.11+ installed
If you get a lower version or an error like command 'vue' not found then globally install the latest version of the Vue CLI.
Now you can add install Quasar into your component library with the command vue add quasar. When the setup wizard asks Allow Quasar to replace App.vue, … answer No since those changes don’t apply to a component library. Otherwise, answer the questions according to your preference.
1$ vue add quasar
2
3📦 Installing vue-cli-plugin-quasar...
4
5added 6 packages, and audited 1379 packages in 4s
6
7218 packages are looking for funding
8 run `npm fund` for details
9
10found 0 vulnerabilities
11✔ Successfully installed plugin: vue-cli-plugin-quasar
12
13? Allow Quasar to replace App.vue, About.vue, Home.vue and (if available) router.js? No
14? Pick your favorite CSS preprocessor: Sass
15? Choose Quasar Icon Set Material
16? Default Quasar language pack - one from
17https://github.com/quasarframework/quasar/tree/dev/ui/lang en-US
18? Use RTL support? No
19? Select features: Roboto font, Material, Material Outlined, Material Round, Material
20Sharp, Fontawesome, Ionicons, MDI, Eva
21
22🚀 Invoking generator for vue-cli-plugin-quasar...
23📦 Installing additional dependencies...
24
25added 5 packages, and audited 1384 packages in 6s
26
27221 packages are looking for funding
28 run `npm fund` for details
29
30found 0 vulnerabilities
31⚓ Running completion hooks...
32
33✔ Successfully invoked generator for plugin: vue-cli-plugin-quasar Note that this installation adds several files only applicable to web applications (icons to the public folder, overwriting favicons, adding Quasar to the runtime dependencies, vue.config.ts, etc.) In my experience, the installed Sass styles are also frequently broken.
Manual installation of Quasar as a Vue CLI plugin
Add Quasar and its Vue CLI plugin as a dev dependency
1npm install --save-dev quasar @quasar/extras vue-cli-plugin-quasar sass sass-loaderAdd Quasar to your library’s peerDependencies
Create a stylesheet for your custom Quasar variables (e.g. color themes)
Create a stylesheet importing Quasar’s global styles
Create the quasar-user-options.js for the Quasar Vue CLI plugin’s configuration settings
1import './styles/quasar.variables.sass'
2
3// NOTE: Omit any fonts/icon packs you do not use.
4import '@quasar/extras/roboto-font/roboto-font.css'
5import '@quasar/extras/material-icons/material-icons.css'
6import '@quasar/extras/material-icons-outlined/material-icons-outlined.css'
7import '@quasar/extras/material-icons-round/material-icons-round.css'
8import '@quasar/extras/material-icons-sharp/material-icons-sharp.css'
9import '@quasar/extras/fontawesome-v5/fontawesome-v5.css'
10import '@quasar/extras/ionicons-v4/ionicons-v4.css'
11import '@quasar/extras/mdi-v4/mdi-v4.css'
12import '@quasar/extras/eva-icons/eva-icons.css'
13
14// To be used on app.use(Quasar, { ... })
15export default {
16 config: {},
17 plugins: {
18 }
19}Finally create a setupQuasarApp.ts script with a function to augment a given Vue app with Quasar functionality.
That’s it for the library installation!
If your downstream project uses the Quasar CLI then you may be able to import and use components directly from your component library. Otherwise, (e.g. if your downstream project is vanilla Vue) you will have to import the Quasar stylesheets and the apply the Quasar setup function yourself. Your code might look something like this:
1 import { createApp } from 'vue'
2 import App from './App.vue'
3
4+import "my-component-library/styles/quasar.global.sass"
5+// ^⚠️ WARNING^: Applies global styles to base elements (e.g. h1, h2). You may
6+// have to list your custom styles later to override these Quasar styles.
7
8+import "my-component-library/styles/quasar.variables.sass"
9+import { setupQuasarApp } from "my-component-library/setupQuasarApp"
10
11 const app = createApp(App)
12+setupQuasarApp(app)
13 app.mount(#app)
Add a custom component based on Quasar
For this tutorial let’s create a custom DatePicker
Create a DatePicker.vue
1<template>
2 <QCard class="q-pa-md" style="max-width: min-content;" role="datepicker">
3 <QDate mask="YYYY-MM-DD" :modelValue="modelValue" dense minimal @update:modelValue="onSelectDate"></QDate>
4 <QBtn color="primary" @click="onSelectDate(dashedDate(new Date()))" label="Today" />
5 </QCard>
6</template>
7
8<script lang="ts">
9import { QBtn, QCard, QDate } from "quasar"
10
11export function dashedDate(date: Date) {
12 return date.toISOString().slice(0, 10)
13}
14
15export default {
16 emits: ["update:modelValue"],
17 components: {
18 QBtn,
19 QCard,
20 QDate,
21 },
22 props: {
23 modelValue: {
24 type: String,
25 },
26 },
27 methods: {
28 dashedDate,
29 onSelectDate(date: string) {
30 this.$emit("update:modelValue", date)
31 },
32 },
33}
34</script>Notice that we import all Quasar components used within the template. This differs slightly from a Quasar CLI managed project where all those components are available globally.
Render and test Quasar components in Storybook.js
Create some stories for the DatePicker
1import type { Meta, StoryObj } from '@storybook/vue3';
2import { userEvent, within } from '@storybook/testing-library';
3
4import DatePicker from '../components/DatePicker.vue';
5
6// CONFIGURATION
7const meta = {
8 title: "Date Picker",
9 component: DatePicker,
10 render(args, { argTypes }) {
11 return {
12 template: `<div>
13 Selected Date (in parent): {{ args.modelValue }}
14 <br/>
15 <br/>
16 <DatePicker v-bind="args" v-model="args.modelValue" />
17 </div>`,
18 props: Object.keys(argTypes),
19 components: { DatePicker },
20 data: () => ({ args }),
21 }
22 },
23 tags: ["autodocs"],
24} satisfies Meta<typeof DatePicker>;
25
26export default meta;
27type Story = StoryObj<typeof meta>;
28
29// STORIES
30export const Default: Story = {
31 args: {},
32 play: async ({ canvasElement }) => {
33 const datePicker = within(canvasElement).getByRole('datepicker');
34 await userEvent.click(within(datePicker).getByText("15"));
35 await userEvent.click(within(datePicker).getByText("Today"));
36 }
37}
38
39export const SpecificDate: Story = {
40 args: {
41 modelValue: "2022-05-23",
42 },
43}And update the Storybook Previewer to support our Quasar components
How to use Quasar’s Sass variables in a custom component library
Quasar has tons of Sass variables which control much of the look and feel of a Quasar app. Officially these variables are only available to Quasar CLI managed apps, but supporting them in Vue CLI managed projects (like a component library) only requires configuring a single Vite plugin.
Install the Quasar Vite plugin
1npm install --save-dev @quasar/vite-pluginAdd the Quasar Vite plugin to your vite.config.ts. Make sure to set the Quasar plugin’s sassVariables option to the path to the file containing your overrides of Quasar’s variables.
Conclusion
Quasar is a fabulous tool for building Vue applications. Hopefully this tutorial has helped it become even more useful and testable!
Feel free to comment on your experience improving the testability of Quasar and/or using it in a custom component library!