Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Use Vuetify Sass Variables #115

Closed
mostafaznv opened this issue Oct 7, 2023 · 19 comments
Closed

Unable to Use Vuetify Sass Variables #115

mostafaznv opened this issue Oct 7, 2023 · 19 comments

Comments

@mostafaznv
Copy link

Hi.
Thank you for this excellent library. It surpasses the official vite-plugin-vuetify plugin by miles.

However, I've encountered an issue. I'm not sure if it's a bug or if something in my configuration is incorrect. I hope someone can help me.

I want to use some of Vuetify's SASS variables (such as $display-breakpoints) in my project, but I'm facing difficulties.

This is my nuxt.config.ts file:

import {defineNuxtConfig} from 'nuxt/config'


export default defineNuxtConfig({
    ssr: true,
    
    css: [
        '@/assets/styles/main.scss'
    ],
    
    experimental: {
        inlineSSRStyles: false
    },
    
    modules: [
        'vuetify-nuxt-module'
    ],
    
    vuetify: {
        moduleOptions: {
            styles: {
                configFile: 'assets/styles/vuetify.scss' // <--------- NOTICE
            }
        },
        vuetifyOptions: './vuetify.config.ts'
    },
    
    vite: {
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: `
                        @use "@/assets/styles/variables/_colors.scss" as *;
                        @use "@/assets/styles/variables/_variables.scss" as *;
                        @use "vuetify/lib/styles/settings/variables" as *; // <--------- NOTICE
                    `
                }
            }
        }
    }
})

And this is assets/styles/vuetify.scss:

$font-family: 'Poppins', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
"Noto Color Emoji";

$size-scales: (
    'x-small': -1.75,
    'small': -1,
    'default': 0,
    'large': 1,
    'x-large': 2
);


@use "vuetify/settings" with (
    $color-pack: false,
    $body-font-family: $font-family,
    $font-size-root: 18px,
);

But I encountered this error:

500
[vite-node] [plugin:vite:css] [VITE_ERROR] ./node_modules/vuetify/lib/styles/settings/_index.sass:1:1

Error: This module was already loaded, so it can't be configured using "with".
    ┌──> node_modules/vuetify/lib/styles/settings/_index.sass
1   │   @forward './variables'
    │   ^^^^^^^^^^^^^^^^^^^^^^ new load
    ╵
    ┌──> assets/styles/vuetify.scss
4   │                           @use "vuetify/lib/styles/settings/variables" as *;
    │                           ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ original load
... │
205 │ ┌ @use "vuetify/settings" with (
@userquin
Copy link
Owner

userquin commented Oct 7, 2023

@mostafaznv I'm not a sass/scss expert, you can check https://github.com/Pinegrow/pg-nuxt-vuetify or ask in nuxt channel in vuetify discord server

@mostafaznv
Copy link
Author

mostafaznv commented Oct 7, 2023

I don't believe it's directly related to Vuetify itself.
In one of my projects using vite-plugin-vuetify, this configuration (loading Sass variables via Vite configuration) works perfectly.
It appears that files inside Vite's additionalData are loading after your package configFile, and I'm unsure how to reverse this.

Regardless, thank you for sharing that repository. I’ll take a look and hope it provides a solution.

@userquin
Copy link
Owner

userquin commented Oct 7, 2023

@mostafaznv maybe adding a custom module reordering vite config entry...

@mostafaznv
Copy link
Author

No, neither the pg-nuxt-vuetify solution nor the custom module worked.
It seems that Vite's preprocessorOptions runs before vuetify-nuxt-module, and when we attempt to define our custom config file through vuetify.moduleOptions.styles.configFile, it can't do it because those variables are already defined.

@TechAkayy
Copy link

Hi @mostafaznv, do you have a sample repo I can have a look? I think I encountered this in the past.

@TechAkayy
Copy link

If you look closely into https://github.com/Pinegrow/pg-nuxt-vuetify, you will notice the there are two sass files & they are configured differently to how you have in your nuxt.config.ts. Also take a note of @forward 'vuetify/settings' .. and @use 'vuetify'.. and compare with yours.

@mostafaznv
Copy link
Author

Hi @mostafaznv, do you have a sample repo I can have a look? I think I encountered this in the past.

Hi @TechAkayy
Thank you for your reply.

this is a reproduction link:
https://stackblitz.com/edit/nuxt-starter-nbwlts?file=nuxt.config.ts

@TechAkayy
Copy link

TechAkayy commented Oct 11, 2023

Hey mate,

I'm not a sass expert either, my efforts have been purely trial & error. This is the best I could do - https://stackblitz.com/edit/nuxt-starter-sndsr7?file=nuxt.config.ts.

Refer to these files I have updated and compare with yours:

  • nuxt.config.ts
  • main.scss
  • settings.scss

The below variables.scss is a new addition when compared to https://github.com/Pinegrow/pg-nuxt-vuetify. Again, this is based on my assumption that when imported via app.vue, it overrides the css/scss imports of nuxt config. Not sure if it really works. If you are an sass expert, you might probably know better than me :-)

  • variables.scss (imported in app.vue)
  • app.vue

You can find comment with my initial AK: in variables.scss. All the best!

@TechAkayy
Copy link

In settings.scss, try updating $button-height: 61px, to $button-height: 40px,. HMR updates the sass variable customization.

@TechAkayy
Copy link

Also, check my reply here - nuxt/nuxt#15412 (comment)

@mostafaznv
Copy link
Author

Thank you very much, @TechAkayy.
I checked it, and it works.
However, by removing vite.css.preprocessorOptions.scss.additionalData from nuxt.config.ts, I lost access to using Vuetify Sass variables in my own SCSS files or components.

For example, in one of my Vue components, I want to write a small piece of CSS to handle responsive styling on mobile. I used to do it using this technique, but I can't do it now:

<style lang="scss">
.v-btn {
  @media #{map-get($display-breakpoints, 'sm-and-down')} {
    font-size: 12px;
  }
}
</style>

@TechAkayy
Copy link

TechAkayy commented Oct 11, 2023

Ohh okay.. I'm guessing there should be a way to acheive what you are trying to do.

I have updated variables.scss - https://stackblitz.com/edit/nuxt-starter-sndsr7?file=assets%2Fvariables.scss. Not sure how to test responsive sizes on stackblitz. Again, not sure if it works! You are not alone in this sassy ordeal ;-)

@mostafaznv
Copy link
Author

mostafaznv commented Oct 11, 2023

Finally, it worked. Thanks ❤️

However, there is a setback here. Both vite and webpack have an option to define variables globally (using additionalData), and I'm used to defining Vuetify Sass variables globally and using them wherever I want.

However, I can't do it now, and I have to import ~/assets/settings wherever I want to use one of Vuetify's Sass variables. It requires a significant refactor in all my components.

But I think it's okay. It's nothing to stop me from using this incredible module.


I have an off-topic question too 😄.
How can I tell Vuetify that my template is RTL? I'm defining the locale property in vuetify.config.ts like this:

locale: {
    locale: 'fa',
    fallback: 'en',
    rtl: {
        fa: true
    },
},

However, I think it's being ignored, and my layout renders LTR.

@userquin
Copy link
Owner

@mostafaznv it seems you're using the rtl properly (from docs: https://vuetifyjs.com/en/features/internationalization/#creating-a-custom-locale), can you check virtual vuetify module configuration and check if the options are ok?

You can use Nuxt DevTools or browser dev tools (in browser check the const options = JSON.parse(...):

imagen

@mostafaznv
Copy link
Author

locale is empty.
Could it be because I'm using nuxt-i18n at the same time? I saw a warning here, but I thought it's about loadMessages, not UI direction.

{
  "defaults": {
    "VAppBar": {
      "elevation": 0
    },
    "VBtn": {
      "color": "primary",
      "variant": "flat",
      "size": "default"
    },
  },
  "locale": {},
  "theme": {
    "defaultTheme": "light",
    "themes": {
      "light": {
        "dark": false,
        "colors": {
          "background": "#FFF",
          "surface": "#FFF",
          "primary": "#000066",
          "secondary": "#35373D",
          "error": "#FA4343",
          "info": "#345191",
          "success": "#00966D",
          "warning": "#FAB005"
        }
      },
      "dark": {}
    },
    "variations": {
      "colors": [
        "primary",
        "secondary"
      ],
      "lighten": 3,
      "darken": 3
    }
  },
  "ssr": true
}

@userquin
Copy link
Owner

userquin commented Oct 11, 2023

@mostafaznv can you file a new issue (with a simple repo 🙏)?

uhmm, ok, you're using i18n, check the playground... you need to configure i18n module properly

https://vuetify-nuxt-module.netlify.app/guide/i18n.html

@userquin
Copy link
Owner

userquin commented Oct 11, 2023

@mostafaznv you also need to add the vuetify translations in your i18n files (json/ts/js) under $vuetify key.

Here a few entries used in the playground: https://github.com/userquin/vuetify-nuxt-module/blob/main/playground/locales/single/en-US.json#L3

Just copy/paste this to your fa i18n file (update the entries, some missing and maybe wrongly translated): https://github.com/vuetifyjs/vuetify/blob/master/packages/vuetify/src/locale/fa.ts

@mostafaznv
Copy link
Author

I don't think there's a need to create a new issue. The problem has been solved. I was using nuxt-i18n, which was the main reason for not loading UI with RTL styles. After checking the playground and configuring i18n properly, the issue is resolved.

Thank you to you and @TechAkayy for your helps.
I really appreciate it 🔥

@userquin userquin pinned this issue Oct 11, 2023
@userquin
Copy link
Owner

issue pinned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants