Properly installed tailwind dosn't work in React app - reactjs

I initialized React application using Vite, and know I'm trying to add tailwind to this app.
I follewed the steps straight from documentation, and simply the classes are not applied to my components.
https://tailwindcss.com/docs/guides/create-react-app
Couple of days ago I was initializing React app the same way, followed the same instructions, and tailwind was working just fine. Now it does not and I really can't find any difference.
Here is my tailwind.config.js:
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Here is package.json:
{
"name": "megak-group-project-frontend",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"#vitejs/plugin-react": "^1.0.7",
"autoprefixer": "^10.4.2",
"postcss": "^8.4.6",
"tailwindcss": "^3.0.22",
"vite": "^2.8.0"
}
}
Here is index.css:
#tailwind base;
#tailwind components;
#tailwind utilities;
Here is component that doesn't work:
function App() {
return (
<div className="App">
<h1 className="text-3xl font-bold underline">
Hello world!
</h1>
</div>
)
}
export default App

After some time I find out that npx tailwindcss init did not initialize postcss.config.js.
The reason it was not warking was this file missing with content:
const tailwindcss = require("tailwindcss");
module.exports = {
plugins: [tailwindcss("./tailwind.config.js"), require("autoprefixer")],
};

Related

Did the svelte.config.js file replace the rollup.config.js file in SvelteKit?

I'm trying to get PostCSS set up in my SvelteKit project to start playing with container queries. I installed 'svelte-preprocess' in my svelte.config.js file and made a postcss.config.cjs file (see below)
svelte.config.js
import adapter from '#sveltejs/adapter-auto';
import sveltePreprocess from 'svelte-preprocess';
/** #type {import('#sveltejs/kit').Config} */
const config = {
preprocess: sveltePreprocess({ postcss: true }),
kit: {
adapter: adapter(),
},
};
export default config;
postcss.config.cjs
const postcssPresetEnv = require('postcss-preset-env');
const config = {
plugins: [
postcssPresetEnv({
stage: 3,
features: {
'nesting-rules': true,
'custom-media-queries': true,
'media-query-ranges': true,
},
}),
],
};
module.exports = config;
I get the following error at my root level +page.svelte file:
*")" is expected
If you expect this syntax to work, here are some suggestions:
If you use less/SCSS with svelte-preprocess, did you add lang="scss"/lang="less" to your style tag? If you use SCSS, it may be necessary to add the path to your NODE runtime to the setting svelte.language-server.runtime, or use sass instead of node-sass.
Did you setup a svelte.config.js?
See https://github.com/sveltejs/language-tools/tree/master/docs#using-with-preprocessors for more info.*
Here's my +page.svelte file html and css (I'm trying to learn the container query syntax)
<div class="displayContainer">
<div class="queryWrapper">
<div class="imgWrapper">
//custom image component goes here
</div>
<div class="summaryContainer">
<h2>sample header text</h2>
<p>
sample paragraph text
</p>
</div>
</div>
</div>
<style lang="postcss">
.displayContainer {
container: homeDisplay / inline-size;
}
.queryWrapper {
#container homeDisplay (min-width: 450px) { /*line with error*/
display: grid;
grid-template-column: 350px 1fr;
gap: 20px;
}
}
.summaryContainer {
max-width: 600px;
}
</style>
package.json file
{
"name": "algomancy-site",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
},
"devDependencies": {
"#sveltejs/adapter-auto": "^1.0.0",
"#sveltejs/kit": "^1.0.0",
"#types/postcss-preset-env": "^8.0.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte3": "^4.0.0",
"postcss-load-config": "^4.0.1",
"postcss-preset-env": "^8.0.1",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.8.1",
"svelte": "^3.54.0",
"svelte-preprocess": "^5.0.1",
"vite": "^4.0.0"
},
"type": "module"
}
It's expecting a ")" after min-width.
I searched the documentation and recommended GitHub repos for preprocess configuration. Many of these resources/blogs reference a rollup.config.js file.
I don't see a rollup.config.js file in my skeleton SvelteKit project or in the SvelteKit documentation.
Is my issue from lacking a rollup file or is there an error in my file setup above?
After further digging I found this is a known issue with Svelte's compiler has with the '#container' syntax and is being worked on. Github issue 6969
A simple work-around is to place container queries (#container ...) in a separate .css file and import it where needed.

My Tailwind classes are not being used in Next.JS

I installed Tailwind CSS, but the classes are not working for me.
tailwind.config.js
module.exports = {
content: ["./pages/*.{html,js,jsx}", "./src/components/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
postcss.config
module.exports = {
plugins: {
"postcss-import": {},
tailwindcss: {},
autoprefixer: {},
},
};
global.css
#import "tailwindcss/base"; /* I tried #tailwind as well */
#import "tailwindcss/components";
#import "tailwindcss/utilities";
package.json
{
"name": "events-blog",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"#next/font": "13.1.2",
"next": "13.1.2",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"autoprefixer": "^10.4.13",
"postcss": "^8.4.21",
"postcss-import": "^15.1.0",
"tailwindcss": "^3.2.4"
}
}
I can not understand what the problem is. It seems to not be including Tailwind, which is confusing.
Change
#import "tailwindcss/base";
#import "tailwindcss/components";
#import "tailwindcss/utilities";
to
#import "tailwind/base";
#import "tailwind/components";
#import "tailwind/utilities";
And make sure that "./pages/*.{html,js,jsx}", "./src/components/**/*.{js,ts,jsx,tsx}" these are the places where you want your tailwind to work.
Anything outside this scope tailwind-css wont work.

React/Tailwindcss modules name obfuscation breaks postcss nesting

So after extensive research I am here, I have a difficult to search problem which I'm hoping can be solved.
I've booted up a new project with Vite/ReactJS/Tailwindcss and I'm trying to use postcss to create nested CSS rules, they seem to be partially working but when using the ampersand (as in SCSS) things get a bit weird.
I have a bog standard Button React component with the following Button.module.css:
.Button {
#apply bg-primary;
&--disabled {
#apply opacity-50;
}
}
Styles are applied like so:
import styles from './Button.module.css';
const Button = ({ label, disabled }) => {
return (
<button type="button" className={[
styles.Button,
disabled ? 'Button--disabled' : ''
]}>
{label}
</button>
);
};
export default Button;
postcss.config.cjs:
module.exports = {
plugins: {
'postcss-import': {},
'tailwindcss/nesting': {},
tailwindcss: {},
autoprefixer: {},
},
}
However in the browser the button looks like, note the obfuscated classes:
<button type="button" class="Button _Button_1fcg7_1 Button--disabled">Button</button>
While the (successfully, I note) compiled CSS looks like:
._Button_1fcg7_1 {
--tw-bg-opacity: 1;
background-color: rgb(29 161 242 / var(--tw-bg-opacity))
}
._Button--disabled_1fcg7_1 {
opacity: 0.5
}
Because the class name in the HTML compiles to .Button--disabled and the CSS selector gets obfuscated to ._Button--disabled_1fcg7_1, the styles are never applied. What gives?
Some further info as requested, package.json:
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook"
},
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"postcss-nesting": "^10.1.10",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sass": "^1.53.0"
},
"devDependencies": {
"#babel/core": "^7.18.6",
"#storybook/addon-actions": "^6.5.9",
"#storybook/addon-essentials": "^6.5.9",
"#storybook/addon-interactions": "^6.5.9",
"#storybook/addon-links": "^6.5.9",
"#storybook/builder-vite": "^0.2.0",
"#storybook/react": "^6.5.9",
"#storybook/testing-library": "^0.0.13",
"#types/react": "^18.0.15",
"#types/react-dom": "^18.0.6",
"#vitejs/plugin-react": "^2.0.0",
"autoprefixer": "^10.4.7",
"babel-loader": "^8.2.5",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.6",
"typescript": "^4.6.4",
"vite": "^3.0.0"
}
}
vite.config.ts:
import { defineConfig } from 'vite'
import react from '#vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})
tailwind.config.cjs:
/** #type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {
colors: {
primary: '#1da1f2',
},
}
},
plugins: [],
}
It's funny because different people with similar setups have the opposite problem.
Is there a way to obfuscate TailwindCSS classnames using PostCSS for React project in Vitejs?
I think that your problem comes from another place.
Can you share your package.json, tailwind.config.js, vite.config.js and rollup.config.js (if you have this last file) ?
Maybe you have some settings or packages configured related to obfuscation.
I don't know the vite ecosystem, but there are some packages that generate the behavior you want to avoid:
vite-plugin-tailwind-ofuscar
rollup-plugin-postcss

Why is Ant design less styles not applied in nextjs app with Webpack5?

I am building a sample Nextjs app with webpack5 and for UI I am using Antd framework. I do not see the styles applied at all. When I go in the dev tools I do see ant classes applied to buttons but they are not styled. Here are my files:
globals.less:
#import 'antd/dist/antd.less';
_app.js:
import "../styles/globals.less";
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
next.config.js: (for providing loaders and to use webpack5)
module.exports = {
future: {
webpack5: true,
},
webpack: (config, options) => {
config.module.rules.push({
test: /\.less$/,
use: [
{
loader: "css-loader", // translates CSS into CommonJS
},
{
loader: "less-loader", // compiles Less to CSS
options: {
lessOptions: {
// If you are using less-loader#5 please spread the lessOptions to options directly
modifyVars: {
"primary-color": "#1DA57A",
"link-color": "#1DA57A",
"border-radius-base": "2px",
},
javascriptEnabled: true,
sourceMap: true,
},
},
},
],
});
return config;
},
};
index.js:
import { Button } from 'antd'
import Head from 'next/head'
export default function Home() {
return (
<div>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main >
<h1 >
Welcome to Nextjs
</h1>
<div>
<Button type="primary">Primary Test</Button>
</div>
</main>
</div>
);
}
package.json:
{
"name": "hello",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"#zeit/next-less": "^1.0.1",
"antd": "^4.15.2",
"babel-plugin-import": "^1.13.3",
"css-loader": "^5.2.4",
"less": "^4.1.1",
"less-loader": "^8.1.1",
"next": "10.1.3",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {}
}
When I look at the page through dev tools I don't even see the script tag.
Edit:
for anyone saying use ~ in the path to less file please note that starting ver 8.0.0 it's been depracated. Here is the link to changelog:
https://github.com/webpack-contrib/less-loader/blob/master/CHANGELOG.md
Prior to less-loader v8.0.0, you'll need to add a ~ to your antd.less import to tell the webpack loader to resolve the import from the node_modules directory.
#import '~antd/dist/antd.less';

Font.loadAsync() can't resolve module for custom fonts (React Native & Expo)

My app uses CRNA and Expo, and my issue is that the Font.loadAsync() asynchronous function can't locate a .otf font file in the assets/fonts/ folder in my project directory. I am absolutely sure that the directory and file names are correct. I receive this error.
link to image of my error screen
Here is my code:
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Font, AppLoading } from 'expo'
import Root from './js/Root';
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
fontLoaded: false
}
}
async componentDidMount() {
await Font.loadAsync({
"Light": require('./assets/fonts/SemplicitaPro-Light.otf')
})
this.setState({ fontLoaded: true })
}
render() {
if (!this.state.fontLoaded) {
return <AppLoading />
}
return <Root />;
}
}
Here is my package.json:
{
"name": "Zumer",
"version": "0.1.0",
"private": true,
"devDependencies": {
"flow-bin": "^0.40.0",
"jest-expo": "^0.4.0",
"react-native-scripts": "0.0.28",
"react-test-renderer": "16.0.0-alpha.6"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js --watch",
"flow": "flow"
},
"packagerOpts": {
"assetExts": ["ttf", "otf"]
},
"jest": {
"preset": "jest-expo",
"tranformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation)"
]
},
"dependencies": {
"#expo/vector-icons": "^4.0.0",
"expo": "^16.0.0",
"native-base": "^2.1.2",
"react": "16.0.0-alpha.6",
"react-native": "^0.43.4",
"react-native-elements": "^0.11.1",
"react-navigation": "^1.0.0-beta.8",
"react-redux": "^5.0.4",
"redux": "^3.6.0",
"redux-observable": "^0.14.1",
"redux-persist": "^4.6.0",
"rxjs": "^5.3.0"
}
}
Could it be that the .otf file format isn't supported by Expo?
I fixed this issue by converting the otf files to ttf
I had a similar issue and I was able to resolve it by removing the quotes around the key that requires the font source.
Before:
await Font.loadAsync({
"Light": require('./assets/fonts/SemplicitaPro-Light.otf')
})
After
await Font.loadAsync({
Light: require('./assets/fonts/SemplicitaPro-Light.otf')
})
And using it in the stylesheet like so
const styles = StyleSheet.create({
text: {
fontFamily: 'Light'
}
});
Everything worked fine after that.

Resources