Next.js Discord

Discord Forum

Accordion not opening smoothly

Answered
Broad-snouted Caiman posted this in #help-forum
Open in Discord
Broad-snouted CaimanOP
I'm using the shadcn accordion: https://ui.shadcn.com/docs/components/accordion
The one at the website opens smoothly. If I use the same code, it will open instantly (not animated). It closes smoothly, like the example on the website.
What could be causing this?
Answered by Broad-snouted Caiman
Found out what the problem was, although I can't explain why this is actually a problem: important: true. If I remove this from tailwind.config.ts, then it works.
View full answer

36 Replies

Did you follow step 2, where you need to add the animations to the tailwind config?
@josh Click to see attachment
Broad-snouted CaimanOP
Yeah, it does that automatically. That's why the close part works. My guess is that --radix-accordion-content-height is only computed correctly when it's opened. That's why closing works smoothly, but opening doesn't.
I'm not sure then. Could you share the code that uses the accordion?
This is how I use it for a FAQ section

<div className="w-full md:w-2/3">
  <Accordion type="single" collapsible className="w-full">
    {
        faqs.map((faq, index) => (
            <AccordionItem key={index} value={`item-${index}`} className="border-theme-100">
                <AccordionTrigger className="text-left text-theme-900">{faq.question}</AccordionTrigger>
                <AccordionContent className="whitespace-pre-wrap text-theme-700">{faq.answer}</AccordionContent>
            </AccordionItem>
        ))
    }
  </Accordion>
</div>
@josh This is how I use it for a FAQ section tsx <div className="w-full md:w-2/3"> <Accordion type="single" collapsible className="w-full"> { faqs.map((faq, index) => ( <AccordionItem key={index} value={`item-${index}`} className="border-theme-100"> <AccordionTrigger className="text-left text-theme-900">{faq.question}</AccordionTrigger> <AccordionContent className="whitespace-pre-wrap text-theme-700">{faq.answer}</AccordionContent> </AccordionItem> )) } </Accordion> </div>
Broad-snouted CaimanOP
export default function GlossaryTerms({
  terms,
}: {
  terms: Readonly<GlossaryTerm>[]
}) {
  return (
    <Accordion type="single" collapsible className="w-full">
      {terms.map((term) => {
        return (
          <AccordionItem key={term.name} value={term.name}>
            <AccordionTrigger>{term.name}</AccordionTrigger>
            <AccordionContent>{term.meaning}</AccordionContent>
          </AccordionItem>
        )
      })}
    </Accordion>
  )
}
@josh Thanks, but it's not a problem when the items don't change.
Broad-snouted CaimanOP
True 👍
I'm not sure then. Your code looks the same as mine. Have you confirmed your theory of the content height variable not being set?
@josh I'm not sure then. Your code looks the same as mine. Have you confirmed your theory of the content height variable not being set?
Broad-snouted CaimanOP
Hm, I think so. What I did was replace to: { height: 'var(--radix-accordion-content-height)' } by to: { height: 50 }. With that, the animation works, although each element has its own height, so it isn't a solution.
@josh I'm not sure then. Your code looks the same as mine. Have you confirmed your theory of the content height variable not being set?
Broad-snouted CaimanOP
I've inspected my element:
<div data-state="open" id="radix-:R43auuekq:" role="region" aria-labelledby="radix-:R3auuekq:" data-orientation="vertical" class="overflow-hidden text-sm transition-all data-[state=closed]:animate-accordion-up data-[state=open]:animate-accordion-down" style="--radix-accordion-content-height: var(--radix-collapsible-content-height); --radix-accordion-content-width: var(--radix-collapsible-content-width); --radix-collapsible-content-width: 783.71875px;"><div class="pb-4 pt-0">Some content</div></div>
Then, I've inspected ShadCN website:
<div data-state="open" id="radix-:r6t:" role="region" aria-labelledby="radix-:r6s:" data-orientation="vertical" class="overflow-hidden text-sm transition-all data-[state=closed]:animate-accordion-up data-[state=open]:animate-accordion-down" style="--radix-accordion-content-height: var(--radix-collapsible-content-height); --radix-accordion-content-width: var(--radix-collapsible-content-width); --radix-collapsible-content-height: 36px; --radix-collapsible-content-width: 443.796875px;"><div class="pb-4 pt-0"></div></div>
Mine doesn't have --radix-collapsible-content-height: 36px;
Have you checked the console for any hydration errors?
@josh Have you checked the console for any hydration errors?
Broad-snouted CaimanOP
No hydration errors.
Reckon you could share the live site?
@josh Reckon you could share the live site?
Broad-snouted CaimanOP
It's still in development, but I've pushed it so you could see it: https://atlasdoconhecimento.vercel.app/glossario
thanks. I notice that --radix-collapsible-content-height gets set for a moment while it's closing, then gets deleted
Really odd, I'm not sure why. My next step would be to put the accordion on an empty page with nothing else in it, to rule out something else on the page influencing it
Probably a red herring, but I'm looking for differences between your code and mine.

Worth changing
<AccordionItem key={term.name} value={term.name}>

to

<AccordionItem key={term.name} value={`item-${term.name}`}>
@josh thanks. I notice that `--radix-collapsible-content-height` gets set for a moment while it's closing, then gets deleted
Broad-snouted CaimanOP
Wow, I didn't notice that, good sight, you have!
@Broad-snouted Caiman Ok, I'll create a blank project to test this out.
You could do this in your existing project but temporarily delete everything else from the layout
@josh You could do this in your existing project but temporarily delete everything else from the layout
Broad-snouted CaimanOP
Sorry for being late to answer you. I got really sick. So, actually I've tested from a blank project the exact same code from the ShadCN website. It works. Now I need to understand what in my project is affecting this component (I have no clue).
@josh You could do this in your existing project but temporarily delete everything else from the layout
Broad-snouted CaimanOP
Found out what the problem was, although I can't explain why this is actually a problem: important: true. If I remove this from tailwind.config.ts, then it works.
Answer
and hope you're feeling better now buddy
don't forget to mark your message as a solution so it can be easily found 🙂
@!=tgt this is likely since this might be overriding other styles
Broad-snouted CaimanOP
Yeah, but I didn't know this would have effects upon variables too.
@josh and hope you're feeling better now buddy
Broad-snouted CaimanOP
Yeah, I didn't expect it either.
@josh and hope you're feeling better now buddy
Broad-snouted CaimanOP
Yes, I am, thank you!
Thanks a lot, you were very helpful!!
No worries!