Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwitchTransition with "out-in" and possible null item? #877

Open
simonhrogers opened this issue Jan 5, 2023 · 1 comment
Open

SwitchTransition with "out-in" and possible null item? #877

simonhrogers opened this issue Jan 5, 2023 · 1 comment

Comments

@simonhrogers
Copy link

I’d like to transition a group of elements with mode="out-in", but there will often be no element to display.

What is the correct way to accommodate for the possibility of no element? I’ve tried doing it like so below, which was working well but of course from the "empty" element you actually need to switch the transition mode to "in-out", or there will be a 500ms delay while the empty element is transitioned out. This looks weird!

So I thought I could change the transition mode below, by setting previousActiveProject in state, but changing the transition mode occasionally seems to yield no transition being run at all – the element will just disappear immediately with no fade.

Thanks so much for any help on this!

<div className={`index-section-item-image index-section-item-project-image`}>
  <SwitchTransition mode={previousActiveProject ? "out-in" : "in-out"}>
    <CSSTransition
      key={!activeProject || isScrolling ? 'empty' : activeProject._id}
      addEndListener={(node, done) => node.addEventListener("transitionend", done, false)}
      classNames='fade'
    >
      {!activeProject || isScrolling ? (
        <div className="empty"></div>
      ) : (
        <SanityImage
          key={`${activeProject._id}-image`}
          image={activeProject.image}
          sizes={['25vw','25vw','25vw','25vw']}
        />
      )}
    </CSSTransition>
  </SwitchTransition>
</div>
@SeedyROM
Copy link

SeedyROM commented Feb 5, 2024

This was how I handled my case which is slightly different than yours, I know which thing is being hovered and if it's being hovered or not.

<SwitchTransition mode="out-in">
  {/* This is required to not leave an always showing card... stupid... */}
  {isProductHovered ? (
    <CSSTransition key={product.id} timeout={300} classNames="fade">
      <div className={`product-info`}>
        <div className="product-info__content">
          <div className="product-info__image">
            <img src={product.featuredImage} alt={product.title} />
          </div>
          <div className="product-info__name">{product.title}</div>
          <div className="product-info__description">
            {product.description}
          </div>
        </div>
      </div>
    </CSSTransition>
  ) : (
    <CSSTransition key="empty" timeout={300} classNames="fade">
      <div> </div>
    </CSSTransition>
  )}
</SwitchTransition>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants