Nuxt 3 with TailwindCSS -> heroicons - vue.js

Can someone help with setting up Heroicons in combination with Nuxt 3?
I ran the following command:
yarn add #heroicons/vue
I also added #heroicons/vue as following to my nuxt.config.js:
build: {
transpile: ["#headlessui/vue", "#heroicons/vue"],
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
},
},
But I can't seem to make it work at all.
Could you tell me what I have to do?

Tailwindcss and Nuxt
first you should ,install tailwind package:
npm install -D tailwindcss postcss autoprefixer
then generate tailwind cona fig file:
npx tailwindcss init
Add Tailwind to your PostCSS configuration
// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
},
})
then inside your tailwind.config.js Configure your template paths:
/** #type {import('tailwindcss').Config} */
module.exports = {
content: [
"./components/**/*.{js,vue,ts}",
"./layouts/**/*.vue",
"./pages/**/*.vue",
"./pages/index.vue",
"./plugins/**/*.{js,ts}",
"./nuxt.config.{js,ts}",
"./app.vue",
],
theme: {
extend: {},
},
plugins: [],
}
! if you set srcDir correct the paths
then add the Tailwind directives to your CSS:
add main.css file to your assets with this content:
Add main.css the CSS file globally
main.css file:
#tailwind base;
#tailwind components;
#tailwind utilities;
nuxt.config.js
css: ['~/assets/css/main.css'],
finally you can use tailwind.
final nuxt.config.js file :
export default defineNuxtConfig({
css: ["#/assets/styles/main.scss"],
postcss: {
plugins: {
"postcss-import": {},
"tailwindcss/nesting": {},
tailwindcss: {},
autoprefixer: {},
},
},
})
Heroicons and Nuxt
First, you should install Heroicons package:
npm install #heroicons/vue
then you can use it like this:
<template>
<BeakerIcon class="h-6 w-6 text-blue-500" />
</template>
<script lang="ts" setup>
import { BeakerIcon } from "#heroicons/vue/24/solid";
</script>
The 24x24 outline icons can be imported from #heroicons/vue/24/outline, the 24x24 solid icons can be imported from #heroicons/vue/24/solid, and the 20x20 solid icons can be imported from #heroicons/vue/20/solid.
learn more here: https://github.com/tailwindlabs/heroicons#vue
but try nuxt-icon library :)

Here is how you should set up a nuxt.config.js file together with tailwindcss and nuxt-icon library:
export default defineNuxtConfig({
modules: ['nuxt-icon'],
css: ['~/assets/css/main.css'], // css file with #tailwind base etc.
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
}
})
Like I wrote in comment, nuxt-icon contains all Heroicons together with 100k + more.
Here is the way you can use this icon library: https://stackoverflow.com/a/73810640/6310260

Related

taildwindcss config not applied in nuxt3 app

I am still very much at the start of a new project using nuxt3. I installed tailwindcss for quick themeing and prototyping. I followed the basic setup on the documentation for the tailwindcss plugin for nuxt 3.
The problem is, that the background color for the welcome message is not applied (the CSS property also does not show up on the rendered HTML). When I change the CSS to something like this: class="bg-green-200", then it works.
I could not find any similar issues, however I'm sure I can't be the first/only one stumbling over this. I appreciate any hints.
**Edit: ** The issue was the import syntax in the tailwind configuration file. Changing the import to the require syntax solved the issue.
My code is as follows:
nuxt.config.js
export default defineNuxtConfig({
modules: [
'#nuxtjs/robots',
'#nuxtjs/color-mode',
'#nuxtjs/tailwindcss',
'#nuxtjs/i18n',
],
i18n: {
locales: [
{
code: 'de', name: 'Deutsch', iso: 'en-US', file: 'de.js'
},
{
code: 'en', name: 'English', iso: 'de-CH', file: 'en.js'
},
],
defaultLocale: 'de',
strategy: 'prefix_except_default',
langDir: 'languages',
detectBrowserLanguage: {
useCookie: true,
cookieKey: 'i18n_redirected',
redirectOn: 'root', // recommended
},
},
buildModules: [],
robots: {},
tailwindcss: {
configPath: 'tailwind.config.js',
exposeConfig: false,
injectPosition: 0,
}
})
tailwind.config.js
import defaultTheme from 'tailwindcss/defaultTheme'
export default {
theme: {
extend: {
colors: {
primary: defaultTheme.colors.green
}
}
}
}
app.vue
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
layouts/default.vue
<template>
<div>
<LayoutHeader/>
<slot />
<LayoutFooter/>
</div>
</template>
pages/index.vue
<template>
<div>
<div class="bg-primary">Welcome to the homepage</div>
</div>
</template>
The main issue being that tailwind.config.css is not a valid configuration file because we're using JS here, hence why npx tailwindcss init is the recommended way to safely generate the configuration file (the config itself is a JS file, not a CSS one).
I have successfully installed it with the following tailwind.config.js file
const colors = require('tailwindcss/colors')
/** #type {import('tailwindcss').Config} */
module.exports = {
content: [
"./components/**/*.{js,vue,ts}",
"./layouts/**/*.vue",
"./pages/**/*.vue",
"./plugins/**/*.{js,ts}",
"./nuxt.config.{js,ts}",
],
theme: {
extend: {
colors: {
primary: colors.green
}
},
},
}
Here is a reference to the documentation: https://tailwindcss.com/docs/customizing-colors#naming-your-colors
Here is a working github repo.

Why tailwind ui not rendered correctly in vue?

I have installed tailwind according to the documentation in tailwindcss.com with vue in laravel. But its rendered like this below -
Why ?
Configuration:
Installed tailwindcss using commands
npm install -D tailwindcss
npx tailwindcss init
2)In tailwind.config.js added
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
3)In input.css added
#tailwind base;
#tailwind components;
#tailwind utilities;
For extra plugins ran this command
npm install #headlessui/vue #heroicons/vue
In main.js:
import { createApp } from 'vue'
import store from './store'
import App from './App.vue'
import './input.css'
createApp(App)
.use(store)
.mount('#app')
Try using PostCSS
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
https://tailwindcss.com/docs/installation/using-postcss
on tailwind.config.js:
module.exports = {
purge:
"./src/**/*.html",
"./src/**/*.vue",
"./src/**/*.jsx",
],
//
};
it's simply adding the ones in the purge, after that, the vue is rendered properly, I think that the problem is that we haven't included vue on the tailwind configuration.
There are two tailwind.config.js files, one at the project level and the other at the vue directory. Copy and paste the following at the vue directory level
/** #type {import('tailwindcss').Config} */
module.exports = {
content: [
"./index.html",
"./src/**/*.{html,js,vue,js,ts,jsx,tsx}"
],
theme: {
extend: {},
},
plugins: [
require('#tailwindcss/forms')
],
}

Unable to use swiper/vue dependencies not found

"vue": "^2.6.14"
"swiper": "^7.0.5",
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
i try default import as per example but:
These dependencies were not found:
#/swiper/css in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/Swiper.vue?vue&type=script&lang=js&
swiper/vue in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/Swiper.vue?vue&type=script&lang=js&
To install them, you can run: npm install --save #/swiper/css swiper/vue
i try to install:
npm install --save #/swiper/css swiper/vue
but the following error appears:
npm ERR! code ENOLOCAL
npm ERR! Could not install from "#\swiper\css" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\A262556\AppData\Roaming\npm-cache_logs\2021-09-14T13_57_46_048Z-debug.log
There seems to be an ongoing issue with the latest version of Swiper regarding module import.
At the moment, I would advise you to use Swiper v6 as a quick fix but you should try to see what is causing this issue (might be related to your bundler).
npm i swiper#^6.8.4
Edit: If you want to use Swiper 7 (from Swiper 6), the solution to your issue might be in the migration guide from Swiper.
Use direct style imports as shown in this example for React:
import 'swiper/swiper.scss'; // core Swiper
import 'swiper/modules/navigation/navigation.scss'; // Navigation module
import 'swiper/modules/pagination/pagination.scss'; // Pagination module
The easy solution for Swiper 7.4.1 on Vue 3 + Vite and compile with Webpack
is using alias.
All this files is located on base structure:
vite.config.js
import path from "path";

export default defineConfig({
resolve: {
alias: [
{
find: "#",
replacement: path.resolve(__dirname, "src"),
},
{
find: "#SwiperBundleCss",
replacement: path.resolve(__dirname, "./node_modules/swiper/swiper-bundle.min.css"),
},
{
find: "#SwiperBundle",
replacement: path.resolve(__dirname, "./node_modules/swiper/swiper-bundle.esm.js"),
},
{
find: "#Swiper",
replacement: path.resolve(__dirname, "./node_modules/swiper/swiper.esm.js"),
},
{
find: "#SwiperVue",
replacement: path.resolve(__dirname, "./node_modules/swiper/vue/swiper-vue.js"),
},
],
},
plugins: [ViteRequireContext(), vue()],
});
jsconfig.json
{
"include": [
"./src/**/*"
],
"compilerOptions": {
"baseUrl": ".",
"target": "esnext",
"module": "es2015",
"paths": {
"#SwiperBundleCss": ["./node_modules/swiper/swiper-bundle.min.css"],
"#SwiperBundle": ["./node_modules/swiper/swiper-bundle.esm.js"],
"#Swiper": ["./node_modules/swiper/swiper.esm.js"],
"#SwiperVue": ["./node_modules/swiper/vue/swiper-vue.js"],
}
}
}
vue.config.js
const path = require("path");
module.exports = {
publicPath: "/",
…
configureWebpack: {
resolve: {
alias: {
"#SwiperBundle": path.resolve(__dirname, "./node_modules/swiper/swiper-bundle.esm.js"),
"#SwiperBundleCss": path.resolve(__dirname, "./node_modules/swiper/swiper-bundle.min.css"),
"#Swiper": path.resolve(__dirname, "./node_modules/swiper/swiper.esm.js"),
"#SwiperVue": path.resolve(__dirname, "./node_modules/swiper/vue/swiper-vue.js"),
},
},
},
};
webpack.config.js
const path = require("path");
…
mode: "production",
stats: "errors-only",
resolve: {
alias: {
"#SwiperBundle": path.resolve(__dirname, "./node_modules/swiper/swiper-bundle.esm.js"),
"#SwiperBundleCss": path.resolve(__dirname, "./node_modules/swiper/swiper-bundle.min.css"),
"#Swiper": path.resolve(__dirname, "./node_modules/swiper/swiper.esm.js"),
"#SwiperVue": path.resolve(__dirname, "./node_modules/swiper/vue/swiper-vue.js"),
},
},
…
And finally how to declare in your project
main.js
…
import "#SwiperBundleCss"; //import css
import { Swiper, SwiperSlide } from "#SwiperVue"; //import component
import SwiperCore, { Pagination, Scrollbar } from "swiper"; //import swiper core and plugins
SwiperCore.use([Pagination, Scrollbar]); //declare two plugins
const app = createApp(App)
.use(router)
…
.component("swiper", Swiper) //declare vue component
.component("swiper-slide", SwiperSlide) //declare vue component
…
.use(devtools);
router.isReady().then(() => app.mount("#app"));
Usage in your .vue files
some_view.vue
<template>
<div>
<!—// … //—>
<swiper
:scrollbar="{
hide: false,
}"
:slides-per-view="isMobileScreen"
:space-between="10"
:grabCursor="true"
:loop="true"
>
<swiper-slide>
<img src=“some_image.jpg" alt="" />
</swiper-slide>
<swiper-slide>
<img src=“some_image.jpg" alt="" />
</swiper-slide>
<swiper-slide>
<img src=“some_image.jpg" alt="" />
</swiper-slide>
</swiper>
<!—// … //—>
</div>
</template>
You can read more info about aliases right there:
https://webpack.js.org/configuration/resolve/
https://github.com/vuejs/vue-cli/issues/2398
https://dev.to/alansolitar/webpack-aliases-in-vue-js-41hp
Regards

How to override Vuetify 2 variables without using Vue CLI

The vuetify documentation only provides example for injecting sass variables using vue-cli configuration in vue.config.js
https://vuetifyjs.com/en/customization/sass-variables#markup-js-vue-config-sass-variables
What is the correct way to provide the modified vuetify variables when not using the CLI?
I am upgrading an older project from v1 (stylus) to v2 (sass) and I need to override some variables, lets say I only need to change the font-family to Arial.
I am also using treeshaking with vuetify.
Now I am kind of stuck because I don't know where to import the style overrides... Importing these in src/main.ts obviously does not work.
I have created a minimal repro here: https://github.com/Sharsie/vuetify-theme-repro/
What I have so far is a webpack config in build directory and style overrides in src/styles/main.scss
$body-font-family: Arial;
#import "~vuetify/src/styles/styles.sass";
Running the project creates a simple page that prints out computed styles for the v-app container
<v-app id="app">
<v-container>
<v-content>
<p>Wanted font-family: Arial</p>
<p>Current font-family: {{ fontFamily }}</p>
</v-content>
</v-container>
</v-app>
After digging through the source code of vue-cli, I figured out that the config just gets passed to sass-loader options, so the solution was pretty straightforward:
Instead of providing the stylesheet with variables through vue.config.js as such:
module.exports = {
css: {
loaderOptions: {
sass: {
data: `#import "~#/styles/main.scss"`,
},
},
},
}
You can provide it directly to sass-loader options in webpack config like this:
module.exports = {
...
module: {
rules: [
...
{
test: /\.(s?c|sa)ss$/,
use: [
'vue-style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
implementation: sass,
sassOptions: {
fiber: fibers,
},
prependData: `#import "~/styles/main.scss"`,
},
},
],
}
...
]
}
...
}
or for sass-loader<8.0.0
options: {
implementation: sass,
fiber: fibers,
data: `#import "~/styles/main.scss"`,
},

SCSS alias in Vue SFC via Rollup

When using Webpack is pretty straight forward to add an alias for scss files in a Vue SFC, e.g:
<style lang="scss">
#import "~scss/config/config";
...
</style>
Would be the following in Webpack:
alias: {
sass: path.resolve(__dirname, '../scss/')
}
How would you add the same kind of alias in Rollup via rollup-plugin-vue?
I've tried adding a number of postcss plugins, e.g
import importer from 'postcss-import';
vue({
css: false,
style: {
postcssPlugins: [
importer({
path: null,
addModulesDirectories: [path.resolve(__dirname, '../shared')]
})
]
}
}),
I've also tried: rollup-plugin-alias, rollup-plugin-includepaths and some other postcss plugins.
I don't think you can use postcss plugins within the Vue plugin to accomplish this, because it compiles the scss before it gets passed to postcss.
Using rollup-vue-plugin I've been able to use style.preprocessOptions.scss.includePaths to alias directories, in my case pointing to node_modules:
//rollup.config.js
import VuePlugin from 'rollup-plugin-vue'
...
plugins: [
VuePlugin({
style: {
preprocessOptions: {
scss: {
includePaths: ['node_modules'],
}
}
})
]
...
// some .vue file
<style>
#import 'some-node-module' //resolves to 'node_modules/some-node-module'
</style