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

Where these classes come from? #214

Open
digoburigo opened this issue Apr 19, 2023 · 24 comments
Open

Where these classes come from? #214

digoburigo opened this issue Apr 19, 2023 · 24 comments

Comments

@digoburigo
Copy link

digoburigo commented Apr 19, 2023

In the documentation there is a code in the installation part in Configure styles section:

@layer base {
  * {
    @apply border-border;
  }
  body {
    @apply bg-background text-foreground;
    font-feature-settings: "rlig" 1, "calt" 1;
  }
}

But where is defined these classes? I cannot find it in TailwindCSS either. Is there something I'm missing?

@multiwebinc
Copy link
Contributor

That goes in your globals.css file

@digoburigo
Copy link
Author

digoburigo commented Apr 20, 2023

Yeah, already did that. But it's throwing a error in the nextjs app. I'm using the t3 monorepo create-t3-turbo with a separated UI package containing the shadcn stuff

 error - ./src/styles/globals.css:1:1
@etop/nextjs:dev: Syntax error: /Users/rodrigoburigo/Documents/Projects/eTopocart/etopocart-turbo/apps/nextjs/src/styles/globals.css The `border-border` class does not exist. If `border-border` is a custom class, make sure it is defined within a `@layer` directive.
@etop/nextjs:dev:
@etop/nextjs:dev: > 1 | @tailwind base;
@etop/nextjs:dev:     | ^
@etop/nextjs:dev:   2 | @tailwind components;
@etop/nextjs:dev:   3 | @tailwind utilities;

@timidri
Copy link

timidri commented Apr 20, 2023

I have encountered the same issue.

@mikebuilds
Copy link

This looks like an eslint issue… either you haven’t added the custom classes to tailwind config, or the eslint tailwindcss plug-in isn’t detecting your tailwind config file

@digoburigo
Copy link
Author

digoburigo commented Apr 20, 2023

@MJtH so, that's my question. Where this classes are coming from so I can define them. I cannot find the definitions of these classes in the shadcn/ui docs either in TailwindCSS docs. So I guess must be a tailwind plugin? If so, what's the plugin?

@mikebuilds
Copy link

@digoburigo The classes (border-border, bg-background, etc) are defined in step 3, in your tailwind.config.js file

https://ui.shadcn.com/docs/installation#configure-tailwindconfigjs

@digoburigo
Copy link
Author

Oh wow. I see, it's colors named border, background and foreground applied to the TailwindCSS classes border, bg and text. Thanks, that was a little confusing 😅

@digoburigo
Copy link
Author

digoburigo commented Apr 20, 2023

So, to fix my issue, instead of putting the shadcn tailwindcss configuration in the UI package tailwind.config I put in the tailwind config package of the monorepo and used the presets prop in both tailwind.config of the UI package and the NextJS app

@Sridatta19
Copy link

@digoburigo Are you using a tailwind prefix for tailwind.config of UI package (as suggested in the with-tailwind example)?

@timidri
Copy link

timidri commented Apr 24, 2023

I'm still unsure what configuration to change so @apply border-border; and the default font configuration work. Can anyone provide a pointer please?

@mikebuilds
Copy link

@timidri Have you followed step 3 and added these options to tailwind.config?

https://ui.shadcn.com/docs/installation#configure-tailwindconfigjs

@timidri
Copy link

timidri commented Apr 24, 2023

@MJtH Yes, I did. As a matter of fact, now @apply border-border; does not give an error anymore for some reason.
The only issue I still have is that "var(--font-sans)" doesn't work and makes everything render using the default browser font. If I remove that variable like so:

      fontFamily: {
        // sans: ["var(--font-sans)", ...fontFamily.sans],
        sans: [...fontFamily.sans],
      },

the rendering is done using the sans fontFamily.

@mikebuilds
Copy link

--font-sans is the Inter font

You can use the below settings in Next JS, but change the --font-inter to --font-sans in the inter variable.

https://beta.nextjs.org/docs/optimizing/fonts#with-tailwind-css

@elidotexe
Copy link

I faced the same error but couldn't grasp the solution mentioned by others above. To resolve the issue, you can select the 'no' option for CSS variables when executing the npx shadcn-ui@latest init command.

Screenshot 2023-08-28 at 18 34 19

@unxok
Copy link

unxok commented Oct 29, 2023

If anyone else is having issues with this and none of the above worked, I found it's because I had a tailwind.config.js file and shadcn created a tailwind.config.ts file and only the .js file was being read. The solution being to just delete the .js config file.

@brt-yilmaz
Copy link

Sometimes such errors are caused by eslint. It can also happen sometimes when using Typescript. Leave a line or two of space in the tailwind.config.js file, then delete it and save it. Eslint is restarted and the error goes away. I got the same error and it was solved this way without doing anything .

@sangdth
Copy link

sangdth commented Nov 19, 2023

I can't believe the solution to fix this issue came exactly like @unxok comment! Thanks for saving my 8 hours of sleep! <3

@kmiterror
Copy link

kmiterror commented Dec 1, 2023

I noticed that if you are using tailwind with custom prefix, then you also have to adjust those border-border, bg-background and text-foreground class names
So if your tailwind.config.ts file looks like below


module.exports = {
  prefix: 'your-custom-prefix-',
}

then you also have to adjust those layer entries in your index.css like below


@layer base {
  * {
    @apply your-custom-prefix-border-border;
  }
  body {
    @apply your-custom-prefix-bg-background your-custom-prefix-text-foreground;
  }
}  

and then it's best to adjust utils.js so prefix is automatically applied (I don't know how to dig out the tailwind prefix automatically so I hardcoded it)

EDIT: my bad, below code doesn't work well with tailwind, prefix is not properly recognized, I have to add prefix to "classes" directly inside component to make it work.

If someone could guide me how to actually do it so I won't have to adjust all the component files.

import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
 
export function cn(...inputs: ClassValue[]) {
    const tailwindPrefix = 'your-custom-prefix-';
    return twMerge(clsx(...inputs).split(' ').map(c => {
        // Check if the class contains a pseudo-class prefix and insert the Tailwind prefix accordingly
        const parts = c.split(':');
        if (parts.length > 1) {
            return parts.map((part, index) => index === 1 ? `${tailwindPrefix}${part}` : part).join(':');
        }
        return `${tailwindPrefix}${c}`;
    }).join(' '));
}

@haikelareff
Copy link

I want to use CSS variable so whats the solution now?

@haikelareff
Copy link

@digoburigo The classes (border-border, bg-background, etc) are defined in step 3, in your tailwind.config.js file

https://ui.shadcn.com/docs/installation#configure-tailwindconfigjs

@timidri Have you followed step 3 and added these options to tailwind.config?

https://ui.shadcn.com/docs/installation#configure-tailwindconfigjs

where is the step 3 you mention??

@0xNegative
Copy link

0xNegative commented Feb 6, 2024

@haikelareff Check the prefix within your Tailwind config. If you have a prefix set there, then you will need to make sure you also apply that prefix to the classes being used here.

For example, if your prefix is ui-:

@layer base {
  * {
    @apply ui-border-border;
  }
  body {
    @apply ui-bg-background ui-text-foreground;
  }
}

@ROHAN13498
Copy link

If anyone else is having issues with this and none of the above worked, I found it's because I had a tailwind.config.js file and shadcn created a tailwind.config.ts file and only the .js file was being read. The solution being to just delete the .js config file.

THIS WORKED THANKS!!!

@sharukhi
Copy link

sharukhi commented Mar 5, 2024

If anyone else is having issues with this and none of the above worked, I found it's because I had a tailwind.config.js file and shadcn created a tailwind.config.ts file and only the .js file was being read. The solution being to just delete the .js config file.

This worked for me

@Linux-NDOB
Copy link

Hi, i wanna share mi solution.

Before start, so sorry for my bad english, and i am just learning web programming

I used a fresh Astrojs install and after followed all the steps, step by step, in the Astro guide, i finally got 2 config files:
tailwind.config.js
tailwind.config.mjs

And both with different content.

The only solution i got was:

To delete the tailwind.config.mjs

Replace the tailwind.config.js content from: .

content: [ './pages/**/*.{ts,tsx}', './components/**/*.{ts,tsx}', './app/**/*.{ts,tsx}', './src/**/*.{ts,tsx}', ],

to:

content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"]

And:

change the module.exports to export default and the content section with the code below to your tailwind.config.mjs file:

as the guide says.

So i got something like this in my tailwind.config.js:

/** @type {import('tailwindcss').Config} */ export default { darkMode: ["class"], // content: [ // './pages/**/*.{ts,tsx}', // './components/**/*.{ts,tsx}', // './app/**/*.{ts,tsx}', // './src/**/*.{ts,tsx}', // ], content: ["./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}"], prefix: "".....

Finally rename the tailwind.config.js to tailwind.config.mjs.
This is because in the components.json file asks for a .mjs filetype.

"config": "tailwind.config.mjs".

Maybe this is a dumb comment but i guess may be useful for any new web dev who's learning for first time.

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