question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default selected items don't appear when using transitions if select is hidden

See original GitHub issue

Steps to reproduce:

  1. Create a component that initially hides its children using display: hidden
  2. Put a React Select in this component, and make it a multi-select
  3. Enable animations
  4. Add some default selected items

Expected behaviour

The default selected items should appear inside the select box

Actual behaviour

The default selected items do not appear. In particular, they have a width of 0:

screen shot 2018-09-12 at 2 59 43 pm

This is because the React transition has not correctly expanded their widths to their ordinary widths.

This behaviour is replicated in this sandbox. Click the button to expand the Expander.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
desbrownecommented, Aug 18, 2020

If anyone needs a solution, this worked for our project. Basically, when Exit is called for a MultiValue, width is set directly on the ref div. The next state change sets the width to 0 and the CSS width transition does its magic. A small side benefit is that there is no measuring required at mount.

// Collapse.tsx

import * as React from 'react';
import { Transition } from 'react-transition-group';

const transition = `width 260ms ease-out`;
interface Props {
	children: any;
	in: boolean;
	onExited: () => void;
}
const Collapse: React.FC<Props> = ({ children, in: inProp, onExited }) => {
	const ref = React.useRef<HTMLDivElement>(null);
	const handleExit = React.useCallback(() => {
		if (ref.current) {
			const { width } = ref.current.getBoundingClientRect();
			ref.current.style.width = `${width}px`;
			ref.current.style.transition = transition;
		}
	}, []);
	return (
		<Transition
			enter={false}
			mountOnEnter
			unmountOnExit
			in={inProp}
			timeout={300}
			onExited={onExited}
			onExit={handleExit}
		>
			{state => (
				<div
					ref={ref}
					style={
						state === 'exiting' || state === 'exited'
							? {
									overflow: 'hidden',
									whiteSpace: 'nowrap',
									width: 0,
									transition,
							  }
							: undefined
					}
				>
					{children}
				</div>
			)}
		</Transition>
	);
};

export default Collapse;

// AnimatedMultiValue.tsx

import * as React from 'react';
import { MultiValueProps, OptionTypeBase, components } from 'react-select';

import Collapse from './Collapse';

interface Props extends MultiValueProps<OptionTypeBase> {
	in: boolean;
	onExited: () => void;
}
const AnimatedMultiValue: React.FC<Props> = ({ in: inProp, onExited, ...props }) => (
	<Collapse in={inProp} onExited={onExited}>
		<components.MultiValue {...props} cropWithEllipsis={inProp} />
	</Collapse>
);

export default AnimatedMultiValue;
// usage
const components = makeAnimated();
components.MultiValue = AnimatedMultiValue as React.FC<MultiValueProps<OptionTypeBase>>;
return (
	<Select 
		components={components} 
		value={value}
		options={options}
		onChange={handleChange}
	/>
);
1reaction
huwcommented, Sep 26, 2018

That does seem to work on the sandbox! I’ll have a look at whether that refactor will work with our code and get back 😃

EDIT: I’ve checked with our code, and visibility: hidden isn’t ideal, because it leaves that huge space around the element. Any ideas?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage objects with the Selection pane - Microsoft Support
Use the Selection pane to move objects backwards and forwards, hide or show objects, and to group or ungroup objects.
Read more >
How to hide a <option> in a <select> menu with CSS?
5. Instead of hiding and showing on a search. Try and populate the select depending on the search · 1. I agree with...
Read more >
visibility - CSS: Cascading Style Sheets - MDN Web Docs
The visibility CSS property shows or hides an element without changing the layout of a document. The property can also hide rows or...
Read more >
Select layers and objects – Figma Help Center
If you hide a layer we won't show in the Select layer menu. You will need to toggle ... The default behavior for...
Read more >
.hide() | jQuery API Documentation
When a custom queue name is used the animation does not automatically start; ... If an element has a display value of inline...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found