My inertia app does not detect some specific colors from Tailwind (Laravel Vue inertia tailwind) - vuejs3

My app does not detect some colors of the Tailwind palette.
In this case, text-green-500 called primaryColorGreen and text-indigo-600 called secondaryColor are working properly.
However, text-red-300 as primaryColorRed is not working, but it does work if I change text-red-300 to `text-red-600.
If I change text-green-500 for text-green-300 it doesn't work.
The same for the indigo color...in general only some colors are working.
Layout.js:
export const useLayoutStore = defineStore('layout', {
state: () => (
{
navigation: [],
currentModels: [],
currentRoute: 'x',
theme: {
classes: {
primaryColorGreen: 'text-green-500',
},
},
color: {
primary: {
primaryColorRed: 'text-red-300',
}
},
textColor: {
secondary: {
secondaryColor: 'text-indigo-600',
}
}
}
),
});
HandleInertiaRequest.php:
'theme'=>[
'classes'=>[
'primaryColorGreen' => 'text-green-500',
],
],
'color' => [
'primary' => [
'primaryColorRed' => 'text-red-300',
]
],
'textColor' => [
'secondary' => [
'secondaryColor' => 'text-indigo-600',
]
],
Vue page:
<script setup>
import {useLayoutStore} from '#/Stores/layout';
const theme = useLayoutStore().theme //
const color = useLayoutStore().color
const textColor = useLayoutStore().textColor
</script>
<template>
<h2 class="mx-auto max-w-2xl text-4xl font-bold tracking-tight">
<span:class="theme.classes.primaryColorGreen">hello</span>
<span:class="color.primary.primaryColorRed">how are you</span>
<span :class="textColor.secondary.secondaryColor"> Raul</span>
</h2>

If you are using Laravel Jetstream with Intertia stack, then the location of your file containing Tailwind classes are probably aren't scanned by tailwind. which normally scan php and vue files inside resources directory
check the tailwind.config.js and add those files which contains tailwind css classes.
or move those files to somewhere that are scan by tailwind default config

Related

Purge-css seems to remove every bootstrap css in production mode

I am using Purgecss to remove unused Css on my next application. Other third party libraries am using are Bootstrap, react-bootstrap and bootstrap-icons. I followed the instructions from
https://purgecss.com/guides/next.html but it does not work in production mode.
Below are links to screenshots for both dev mode and production mode.
dev mode
and
production mode
postcss.config.js
module.exports = {
"plugins": [
"postcss-flexbugs-fixes",
[
"postcss-preset-env",
{
"autoprefixer": {
"flexbox": "no-2009"
},
"stage": 3,
"features": {
"custom-properties": false
}
}
],
[
'#fullhuman/postcss-purgecss',
{
content: [
'./pages/**/*.{js,jsx,ts,tsx}',
'./components/**/*.{js,jsx,ts,tsx}',
'./node_modules/bootstrap/dist/**/*.css"'
],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || [],
safelist: ["html", "body"]
}
],
]
}
next.config.js
/** #type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}
module.exports = nextConfig
_app.js
import 'bootstrap/dist/css/bootstrap.css'
import "bootstrap-icons/font/bootstrap-icons.css";
import '../styles/globals.css'
import Head from "next/head";
function MyApp({Component, pageProps}) {
return (
<>
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</Head>
<Component {...pageProps} />
</>
);
}
export default MyApp;
why is it behaving that way?
Modify your next.config.js by following
purgeCssPaths: [
'node_modules/react-bootstrap/**/*.js'
],
// also you can try to make white lists
purgeCss: {
whitelist: () => [
'modal',
'modal-dialog',
'modal-xl',
...
],
whitelistPatterns: () => [/modal*/, /accordion*/, /card*/],
whitelistPatternsChildren: () => [/modal*/, /accordion*/, /card*/]
}
If some classes are removed anyway, try following:
This is not entirely correct, but you can make a separate fake component with all the necessary classes that the PurgeCSS removes (modal window, carousel, and so on). You can see the missing classes in the Bootstrap documentation
You can tell PurgeCss to look at Bootstrap js files for needed classes
As an example, my vite.config.js looks like this:
purgecss({
content: ['resources/**/*.php',
'resources/**/*.js',
'resources/**/*.vue',
'node_modules/bootstrap/js/**/*.js']
}),
As you can see I added all Bootstrap js source files to look for needed classes.

Tailwind: How to create a loading: modifier/variant?

I'm trying to create a modifier for a button for when it's in a loading state.
Based on the documentation here, I added the following in my tailwind.config.js
// I assume this is included in tailwindcss
// and doesn't need to be installed separately
const plugin = require('tailwindcss/plugin')
module.exports = {
// ...
plugins: [
plugin(function({ addVariant }) {
addVariant('loading', '&:loading')
})
],
};
I assume this allows me to add a string of loading in the class such that it will apply those styles. This doesn't seem to work though, what am I doing wrong?
<!-- I assume this should be blue-600 -->
<button className="bg-blue-600 loading:bg-blue-100">
This is a normal button
</button>
<!-- I assume this should be blue-100 since it has className, "loading" -->
<button className="loading bg-blue-600 loading:bg-blue-100">
This is a loading button
</button>
& sign points to an element with THIS variant applied, so it should be translated in CSS as "this element with class .loading". In your example :loading will be translated as loading state which is not valid
So it should be addVariant('loading', '&.loading') not addVariant('loading', '&:loading')
const plugin = require('tailwindcss/plugin')
module.exports = {
plugins: [
plugin(function({ addVariant }) {
addVariant('loading', '&.loading') // here
})
],
};

Nextjs does not load tailwind css classes without re-building everytime

I am using tailwind css in my nextjs project, and have achieved quite so far, though i am facing an issue which is that when i change the tailwind inbuilt className from "bg-fuchsia-600" to "bg-fuchsia-100", the css class doesnt gets applied, but when i run the script
"build-css": "tailwindcss build src/styles/global.css -o public/styles.css"
then the css gets applied, so everytime i change the tailwind inbuilt css class , i have to run this script which shouldnt be the case.
Below is the setup for this
global.css
#import "tailwindcss/base";
#import "tailwindcss/components";
#import "tailwindcss/utilities";
button.css - My custom css file
.button{
color: orange
}
the script extracts file in public folder by the name styles.css
and there i created the main.css file with the following content
#import './styles.css';
#import '../src/styles/button.css';
and then i am using it in the main _app.tsx file
import '../public/main.css'
Now when i render my component
const Login: React.FC<IProps> = (props) => {
const formik: FormikProps<FormValues> = useFormik<FormValues>({
initialValues: {
email: "",
password: "",
},
validationSchema: null,
onSubmit: (values) => {
console.log(values);
},
});
return (
<>
<p className="bg-fuchsia-100 button">hello</p>
<Navbar />
</>
);
};
export default Login;
for the P tag, the className "button" which is my custom css, the change is reflected in the browser ,but for the tailwind inbuilt classname bg-fuchsia-100 i had to run the script again to make it working if i change it to bh-fuchsia-500 or anything
tailwind.config.js
module.exports = {
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {
colors: {
primary: "#fff",
},
},
},
plugins: [],
};

JSX styles are not working for Storybook after an upgrade

I upgraded my project from NextJS 10 to NextJS 12. Everything is working except for Storybook, which has no styles now.
I am using styled-jsx library to generate embedded css, using it as:
const SearchResult = ({ onClick, vehicle }: SearchResultProps): JSX.Element => {
return (
<div className="search-result" data-testid="search-result" onClick={onClick}>
<style jsx>{styles}</style>
...
</div>
);
};
It generates styles like class="jsx-2615582530 search-result". Before the update, it would also embed a style search-result.jsx-2615582530 into the generated storybook. Now, the jsx style names are there, yet the styles are gone.
I upgraded styled-jsx from 3.3.1 to 5.0.0 and NextJS to 12.0.3. Did not change any loaders or anything. My guess, webpack might have easily gotten upgraded.
My config:
const webpack = require('webpack');
module.exports = ({ config }) => {
// Fill in process.env on the client
config.plugins.push(
new webpack.DefinePlugin({
'process.serializedEnv': {},
})
);
// Sentry requires different packages for front and back end,
// but in storybook we know it's always client side
config.resolve.alias = {
'sentry-alias': '#sentry/browser',
'#/remoteConfig': './standardRemoteConfig',
};
config.module.rules.push({
test: /\.md$/,
loader: "raw-loader",
}),
config.externals = [...(config.externals || []), 'fs', 'net'];
config.resolve.extensions.push('.md');
config.resolve.fallback = {
"https": false,
"stream": false,
};
return config;
};
and main
module.exports = {
core: {
builder: 'webpack5',
},
stories: ['../stories/**/*.stories.tsx'],
addons: [
'#storybook/addon-actions',
'#storybook/addon-links',
'#storybook/addon-backgrounds',
'#storybook/addon-knobs',
'#storybook/addon-viewport',
'#storybook/addon-a11y',
'storybook-addon-paddings',
],
};
Further, if I include styles as <style>{styles}</style> without the jsx prop, they are included in the produced storybook.
Only, the text is displayed weird:
When jsx is there, <style /> is missing from the resulting markup, altogether.
Suggestions?
Newer styled-jsx required the following:
+import { StyleRegistry } from 'styled-jsx';
and
-export const decorators = [withPaddings, withRedux(), (story: Function) => <I18nextProvider i18n={i18n}>{story()}</I18nextProvider>]
+export const decorators = [(Story) => (
+ <StyleRegistry>
+ <Story />
+ </StyleRegistry>
+), withPaddings, withRedux(), (story: Function) => <I18nextProvider i18n={i18n}>{story()}</I18nextProvider>]
This yet again makes embedded jsx styles present.

Use tailwind #apply in CSS modules in Next.js

I've setup tailwind with my Next.js site as per the official guide here: https://github.com/tailwindcss/setup-examples/tree/master/examples/nextjs
However, when I try and use the #apply method, in a CSS module on a component level, eg:
.container {
#apply rows-span-3;
}
I get the following error:
Syntax error: #apply cannot be used with .rows-span-3 because .rows-span-3 either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that .rows-span-3 exists, make sure that any #import statements are being properly processed before Tailwind CSS sees your CSS, as #apply can only be used for classes in the same CSS tree.
This is my postcss.config.js:
const purgecss = [
'#fullhuman/postcss-purgecss',
{
content: ['./components/**/*.jsx', './pages/**/*.jsx'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || [],
},
]
module.exports = {
plugins: [
'postcss-flexbugs-fixes',
'postcss-import',
'postcss-mixins',
'postcss-calc',
'postcss-extend',
['postcss-color-mod-function', {
importFrom: [
'./assets/styles/vars.css',
],
}],
['postcss-preset-env', {
stage: 1,
preserve: false,
importFrom: [
'./assets/styles/vars.css',
],
}],
'tailwindcss',
'autoprefixer',
...(process.env.NODE_ENV === 'production' ? [purgecss] : []),
'postcss-nested',
],
}
I manage to make it work using this example
link to tailwind doc
From the doc:
You have this module css
/*Button.module.css*/
.error {
#apply bg-red-800 text-white;
}
Component file
//Button.js
import styles from './Button.module.css'
export function Button() {
return (
<button
type="button"
// Note how the "error" class is accessed as a property on the imported
// `styles` object.
className={styles.error}
>
Destroy
</button>
)
}
From the example, please notice the use of className={styles.error} instead of className="error"

Resources