So I have an array of colors
let colors=["red-500","blue-500","green-500","yellow-500","cyan-500","white-500","orange-500"]
and i wanna use a specific color depending on a number
<h1 className={`bg-${colors[index]}`}></h1>
the colors are not always applied as intended for example sometimes it always be red or white
has anyone encountered similar issues with tailwind css + react ?
Tailwind will only build styles for classes that it detects in your code—but it does not actually run your source code and won’t detect dynamically constructed class names. Therefore, you must include the complete class name in your strings.
The styles that are working (like red and white) are probably included elsewhere in your code, and make it into the build, while the others are not.
Don't construct class names dynamically
<div class="text-{{ error ? 'red' : 'green' }}-600"></div>
Always use complete class names
<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
Source: Dynamic class names - Tailwind CSS
While the answer from #quartzic is a perfectly acceptable, I'd like to present an alternative.
The missing styles is most likely caused by Tailwind purging all styles that it doesn't detect being used anywhere in your code. This purge functionality can be configured by defining a safelist - a list of classes that shouldn't be purged under any circumstances.
In your case, I'd add the background color classes you want to use, to the safelist and you wont have to change anything in your React component. This is done in the tailwind.config.js file:
module.exports = {
// ...
safelist: [
'bg-red-500',
'bg-blue-500',
'bg-green-500',
'bg-yellow-500',
'bg-cyan-500',
]
// ...
}
The downside is that it might increase your style bundle size, if your safelist includes classes that turned out to not be used anyways. In you case, this doesn't seem to be an issue though.
This safelist can even use regular expressions (although, I'd be vary of using that, as it might increase bundle size unexpectedly).
You can read more in the Tailwind documentation
Related
I am using tailwind in a widget that in the end is attached to a shadow-dom node on different websites.
I am using TailwindCSSs class implementation of the dark mode ( darkMode: 'class') which unfortunately attaches a "dark" class to html-tag.
Is it possible to define different tag to attach the "dark" class to? I do not want to mess up the code of the websites I am adding my widget to.
Thanks :)
If you are using small amount of tailwind classes, try using a prefix. But you have to add prefix for every tailwind classes - such as tw-bg-black tw-text-white tw-font-bold etc.
I'm trying to build out a single static component in Astro.build that has previously been built in Svelte, but I'm not sure I have my environment properly setup. I have an imported TS file that returns an object with my tailwind classes. Essentially I get back an object that looks like this:
tailwind = {
wrapper: 'container mx-auto',
headline: 'text-4xl sm:text-6xl lg:text-7xl'
}
There is no weirdness in that function like 'text' + var + 'xl' it's just simply concatenating classes.
Then in my astro component I deconstruct the props { wrapper, headline } = tailwind(); so that I can assign them to elements like <div class={wrapper}>
This all works fine and my source HTML has all the classes I want. However my CSS file does not include the classes unless I first add them to the astro component directly.
for example if I first add and then change it to <div class={wrapper}> it all works fine, but if I were to add bg-blue-500 to what wrapper returns I do not get a blue background.
I should note this process works great in Svelte so I think it's something with my vite settings, but honestly I get a little lost when it comes to the roll up process. Can someone please help point me in the right direction?
Since Tailwind dynamically generates the CSS output, it needs to know the location of all files with Tailwind Classes.
From https://tailwindcss.com/docs/content-configuration
The content section of your tailwind.config.js file is where you configure the paths to all of your HTML templates, JavaScript components, and any other source files that contain Tailwind class names.
Add the .ts file to the content section of your Tailwind config file.
This question likely has no single direct answer, but hopefully will lead to some best practices or common patterns to use when adapting an existing styles framework to new web component development.
For my case, I have a component <custom-avatar>, and it's all set up properly with self-contained styles and functionality, everything is just peachy.
In certain use cases, the application display needs to stack avatars, just one slightly overtop one other at a diagonal, and the pattern I'm following is using a simple component <custom-composite-avatar>. All this does is wrap the slotted content in a <div> with the correct styling class, but key aspect is retaining the composability for flexible re-use, like so:
<custom-composite-avatar>
<custom-avatar title="first"></custom-avatar>
<custom-avatar title="second"></custom-avatar>
</custom-composite-avatar>
The tricky bit lies in the styles, which are imported from a monorepo that provides the same BEM-ish CSS and component CSS modules to other flavors of the component library like React, Vue, etc. I have the avatar and composite-avatar styles imported just fine, but forcing the intended overlap display is defined with the hierarchical selector .my-composite-avatar.my-composite-avatar--medium .my-avatar {}
So with .my-composite-avatar class applied to the div wrapper within <custom-composite-avatar> and the .my-avatar class applied to the wrapper within the <custom-avatar> and it's own Shadow DOM, that parent/child CSS selector is no good.
I doubt there is a silver bullet for this, but this seems like it will be a rather common scenario as more people migrate to Web Components while using existing styling systems. What approach makes the most sense to ensure that the composite component remains composable, and adaptation of existing selectors pain-free (or at least easy to communicate to other devs)? can this be solved with ::host or ::slotted, or will these cases require significant re-work?
Thanks for reading, your ideas are appreciated!
I would advice to become good friends with CSS properties
because they trickle down into shadowDOMs following CSS selectors.
CSS Custom Properties(variables)
and getPropertyValue
and setProperty if you want to be brutal and make Custom Elements change the outside world.
example
I have an <SVG-ICON> element taking configuration from attributes OR CSS properties
with my favorite lines of code:
let val = this.getAttribute(attr)
||
getComputedStyle(this)
.getPropertyValue("--svg-icon-" + attr)
.replace(/"/g, "")
.trim();
Allows for your standard attribute configuration:
<svg-icon name="configuration" fill="grey"></svg-icon>
But more powerful (simplified example):
<style>
body {
--svg-icon-fill: "grey";
}
svg-icon[selected] {
--svg-icon-fill: "green";
}
</style>
<svg-icon name="messages" selected></svg-icon>
<svg-icon name="configuration"></svg-icon>
CSS = Custom String Scripting
It doesn't often happen, but sometimes the simplest code makes me very happy.
There is no Styling restriction!
These 2 lines allow any String you want in CSS properties:
.replace(/"/g, "")
.trim();
Example
<style>
[name*="globe"] {
--svg-icon-tile: "rect:0,0,24,24,0,fill='blue'";
--svg-icon-stroke: white;
}
</style>
<svg-icon name="feather-icons-globe"></svg-icon>
The --svg-icon-tile has nothing to do with CSS, it is read (and parsed) by the <SVG-ICON> connectedCallback() code to generate a SVG background/tile for all icons named globe.
The double-quotes aren't required, but without them your IDE will complain about invalid CSS.
Have fun coding... you will pull some hairs when you start with calc() in your CSS properties...
But you can take 'CSS' to another level.
PS.
And monitor the future of ConstructAble StyleSheets aka ConstructIble StyleSheets aka Constructed Sheets aka AdoptedStyleSheets:
https://developers.google.com/web/updates/2019/02/constructable-stylesheets
https://chromestatus.com/feature/5394843094220800
iconmeister
within TailwindCSS you can specify such code:
<div class="bg-red-500 sm:bg-green-500 md:bg-blue-500></div>
So that the default color will be red, at the "sm" breakpoint it will be green and at the "md" breakpoint it will be blue.
You can assign any uitlity class to any breakpoint by prefixing it with the coresponding letters and a ":".
Does TailwindCSS simply generate every single utility class for/within every defined breakpoint?
Short answer
Yes, it generates everything as defined per the tailwind config file.
Longer answer
It generates all the classes as per the config. This is to make your development life easier, so you don't have to worry about which classes are defined and which aren't.
Now you probably don't want all of the classes tailwind provides in production environment, only the ones which you actually use. If you want to know more about how to control the file size and removing unused css, read the tailwind docs.
I am getting started with Clarity Design with Angular.
I am trying to cusomise styles thru 'style.css' which is the last entry
in styles array in angular.json
My questions/observations are..
Reduce the Height of the 'header' ?
'content-area' seems to be having some padding..
'sidenav' seems to be leaving too much margins, paddings to my liking...
How do I customise/override these in css ? Been trying few things but to no avail. Am I missing somthing ?
Thanks in advance.
First of all I'd suggest to read the official theming documentation.
In general each component has a file called _variables.<component_name>.scss that defines all the variables used by the specified component.
For instance this file defines the variable $clr-header-height that is the default header height.
This other file instead defines "global" variables.