I'm a primarily back-end developer, so I'm not that experienced with front end stuff. I'm using React with Tailwind CSS to create a page, and I have this segmented control on the page.
Currently, it works just fine, and the white background and shadow statically changes between Monthly and Yearly. However, I would like to make it animated / have a transition so that the white background / shadow slides from Monthly to Yearly and vice versa. I've never done front end transitions / animations before so not really sure how to go about this, especially with Tailwind CSS.
This is the code for the segmented control currently:
function makeRecurrenceIntervalButtons(
currentInterval: RecurringPriceInterval,
setInterval: (interval: RecurringPriceInterval) => void,
) {
return ['month', 'year'].map((interval: RecurringPriceInterval) => {
const className = currentInterval === interval
? `relative w-1/2 bg-white border-gray-200 rounded-md shadow-sm py-2 text-sm font-medium text-gray-700 whitespace-nowrap focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:z-10 sm:w-auto sm:px-8`
: `ml-0.5 relative w-1/2 border border-transparent rounded-md py-2 text-sm font-medium text-gray-700 whitespace-nowrap focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:z-10 sm:w-auto sm:px-8`;
const title = interval === 'month' ? 'Monthly' : 'Yearly';
return (
<button
key={interval}
onClick={() => setInterval(interval)}
type="button"
className={className}
>
{title}
</button>
);
});
}
I've looked at this page extensively, but not sure how to properly apply it at all. Any help would be appreciated, thanks!
First of all, you need to add transform class - othervise transitions wouldn't work.
Next there are a few classes to control you transition timing - duration and timing function
Finally, in first case you should add translate-x-0, when it's Yearly - translate-x-full - it should move button on it's own full width (not the parent element) in a positive x-axis direction.
So add this classes, for ex:
Monthly - transform duration-700 ease-in-out translate-x-0
Yearly - transform duration-700 ease-in-out translate-x-full
Related
I'm building a portfolio and i'm currently stuck in a section where i'm looping thru an object which contains the respective color that I want to use (eg:
export const skills = [
{
id: 1,
name: "front-end",
Icon: FaReact,
color: "#61DAFB",
},
In the component mapped receiving these props, I already logged the color variable and it's logging correctly. But when I try to use that variable to dynamic change the color of the component, it doesn't work at all.
const SkillCard = ({ name, Icon, tools, color }) => {
console.log(`[${color}]`);
return (
<article
className={`bg-black text-gray-300 w-full hover:shadow-lg hover:shadow-gray-800 flex flex-col gap-4 p-8 rounded-lg grayscale hover:grayscale-0 duration-200 border-b-[${color}] `}
>
<Icon style={style} size={50} />
<h1 className="font-bold text-xl capitalize">{name}</h1>
<p>{tools}</p>
</article>
);
};
Here you can see that I'm trying to use the border-bottom property to change depending on the color contained in the array of objects, but I just couldn't find the solution.
I already tried chanching the value of the property, to contain the square brackets, but didn't work as well.
Update January 23:
const SkillCard = ({ skill }) => {
const { name, Icon, tools, color } = skill;
const style = { color };
return (
<article
style={{
borderBottomStyle: "solid",
borderBottomColor: color,
borderBottomWidth: "8px",
}}
className="bg-black text-gray-400 hover:text-white w-full hover:shadow-lg hover:shadow-gray-800 flex flex-col gap-4 p-8 rounded-lg grayscale hover:grayscale-0 duration-200"
>
<Icon style={style} size={50} />
<h1 className="font-bold text-xl capitalize">{name}</h1>
<p>{tools}</p>
</article>
);
};
Just added that color property as an inline style and now it works as intended. Not shure if it's best practice, but it's what I achieved so far.
It seems that this is because Tailwind need the full class name (complete unbroken strings) to assign the correct style, according to Tailwind document.
Live demo of the example: stackblitz
For example, perhaps try something like:
export const skills = [
{
id: 1,
name: "front-end",
Icon: FaReact,
borderBotttomColor: "border-b-[#61DAFB]",
},
Then apply to the component, perhaps also add a bottom border width such as border-b-4:
const SkillCard = ({ name, Icon, tools, borderBotttomColor }) => {
return (
<article
className={`${borderBotttomColor} border-b-4 bg-black text-gray-300 w-full hover:shadow-lg hover:shadow-gray-800 flex flex-col gap-4 p-8 rounded-lg grayscale hover:grayscale-0 duration-200`}
>
<Icon style={style} size={50} />
<h1 className="font-bold text-xl capitalize">{name}</h1>
<p>{tools}</p>
</article>
);
};
Alternatively, dynamic class names could be defined as safelist in Tailwind configuration, although it might not be suitable for this use case.
I am using tailwind classes and below is my code. some people suggested to use classNames so used that as well, so similar code in both the newer and older format
const backgroundColor = disabled ? "bg-secondary-500" : "bg-green-700";
className={classNames(
"w-20 my-2 p-2 text-white rounded capitalize hover:ease-in hover:scale-110 hover:duration-200",
customClass,
backgroundColor,
{ "pointer-events-none": true }
)}
className={`w-20 my-2 p-2 text-white bg-green-700 rounded capitalize
hover:ease-in hover:scale-110 hover:duration-200
${disabled ? "pointer-events-none bg-secondary-500" : ""}
${customClass}`}
so in my customClass I have "w-60". but "w-20" is only getting applied. even it is happening for "bg-green-700" and I wanted it to be "bg-secondary-500" for disabled: true
so my disabled is coming as true and pointer-event-none is getting applied but secondary class is overridden by green class
I checked the DOM and both the bg class and both the width classes are available in the below order
<button class="w-20 my-2 p-2 text-white bg-green-700 rounded capitalize
hover:ease-in hover:scale-110 hover:duration-200 w-60
bg-secondary-500 pointer-events-none">View Docs
</button>
so if anyone has any idea this is happening, please help
For merging Tailwind classes use https://github.com/dcastil/tailwind-merge instead of classNames
import { twMerge } from 'tailwind-merge'
// ...
className={twMerge(
"w-20 my-2 p-2 text-white bg-green-700 rounded capitalize hover:ease-in hover:scale-110 hover:duration-200",
disabled && "pointer-events-none bg-secondary-500",
customClass
)}
Here if I understood correctly , you want bg-secondary-500 when button is disabled. This can be done by this
className={`w-20 my-2 p-2 text-white rounded capitalize
hover:ease-in hover:scale-110 hover:duration-200
${disabled ? "pointer-events-none bg-secondary-500" : "bg-green-500 pointer-events-auto"}
`}
However you need to add any condtion if you want to change from w-20 to w-60. Else simply use w-60.
I am learning tailwind these last days and I was wondering if there was any ways to "group" many properties after a media queries or a "hover:" for example.
<a
className="text-[#7E0000] no-underline text-xl mt-0 mb-3 pt-3 pb-3 relative z-0 cursor-pointer
after:left-0 after:bottom-0 after:absolute after:opacity-0 after:w-0 after:h-1 after:content-[''] after:bg-[#7E0000] after:transition-all after:duration-500"
onClick={() => scrollToView("nous")}
>
For example here, I have many properties that I want to add to "after:", is it possible to do a thing like : after:{opactiy-0 bottom-0 left-0} instead of after:opacity-0 after:bottom-0...
Thank you for your help
I have this rails reactjs app that if I put this line in the form
<input type='submit'/>
It will create for me a blue button. I have just installed Tailwind and if I input this line with tailwind class, it does not have any effect on the button design.
<input type='submit'
className='bg-red-500 text-white font-bold py-2 px-4 rounded-full'/>
Is there a way that I can override this global settings like putting !important inline in the className?
Any help would be greatly appreciated.
You can add a ! character before the class name and that should do the job
Example:
<input type='submit'
className='!bg-red-500 text-white font-bold py-2 px-4 rounded-full'/>
Docs: https://v2.tailwindcss.com/docs/just-in-time-mode#built-in-important-modifier
Yes, You can config tailwind.config.js if you add this
module.exports = {
important: true,
}
this will generate all tailwind classes as !important
read more here
I am using formvalidation.io and Tailwind. Whilst there are plugins for many of the major frameworks, there does not appear to be one for Tailwind.
My issue is trying to get the error messages to appear below the form input to which they relate. There are five inputs, all along the same lines :
<div class="relative w-full mb-3">
<label class="block uppercase text-thatblue text-xs font-bold mb-2" for="password">Password</label>
<input type="password" name="password" id="password" class="border-0 px-3 py-3 placeholder-thatblue-lightest text-thatblue bg-white rounded text-sm shadow focus:outline-none focus:ring w-full ease-linear transition-all duration-150" placeholder="Password">
</div>
If I put in an empty div with an ID of "messages" at the bottom of the form input I can get the error messages to display in that div using :
message: new FormValidation.plugins.Message({
clazz: 'text-xs text-red-500',
container: '#messages'
},
}),
...but that's not a great visual layout.
If, however, I put in an empty div with class of messages under each of the inputs
<div class="messages"></div>
And then change the container targeted by formvalidation.io to be
container: '.messages'
...then error messages for all five inputs appear against the first input, which is even less ideal.
The documentation suggests that I can target closest siblings using :
message: new FormValidation.plugins.Message({
clazz: 'text-xs text-red-500',
container: function (field, ele) {
return FormValidation.utils.closest(ele, '.messages').nextElementSibling;
},
}),
But this does nothing.
What am I missing, Obi Wan? You're my only hope.