Issue with React-select: The background color is not extending to the full width when using wordWrap:"scroll"

I am currently implementing react-select in one of my projects. I need the wordWrap property to be set to "scroll". However, when the length of the options exceeds the menu width and I scroll to the right, the hover color does not fill the entire width.

To see a visual representation, you can check out the link: https://i.sstatic.net/gchJG.png

Below is the code snippet for reference. It has been sourced from https://codesandbox.io/s/modest-curie-etoj3 with some modifications made.


import React from "react";
import ReactDOM from "react-dom";
import Select from "react-select";

import "./styles.css";

function App() {
  // Custom styles for react-select
  const customStyles = {
    control: (base, state) => ({
      ...base,
      width: 300,
      background: "#023950",
      borderRadius: state.isFocused ? "3px 3px 0 0" : 3,
      borderColor: state.isFocused ? "yellow" : "green",
      boxShadow: state.isFocused ? null : null,
      "&:hover": {
        borderColor: state.isFocused ? "red" : "blue"
      }
    }),
    menu: base => ({
      ...base,
      width: 300,
      borderRadius: 0,
      hyphens: "auto",
      marginTop: 0,
      textAlign: "left",
      wordWrap: "normal"
    }),
    menuList: base => ({
      ...base,
      padding: 0
    })
  };

  // Options for react-select dropdown
  const options = [
    { label: "option 1 akjbalskej", value: 1 },
    { label: "option 2 akjbalskej", value: 2 },
    { label: "option 3 akjbalskej", value: 3 },
    { label: "option 4 akjbalskej", value: 4 },
    { label: "option 5 akjbalskej", value: 5 },
    { label: "supercalifragilisticexpialidocioussupercalifragilisticexpialidocioussupercalifragilisticexpialidocious", value: 6 }
  ];

  return (
    <div className="App">
      <Select styles={customStyles} options={options} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

I'm still new to react and frontend development. Any assistance on this matter would be greatly appreciated. Thank you!

Answer №1

To achieve the desired effect, it is recommended to utilize text-overflow, white-space and overflow properties instead of word-wrap. Modify your menu object as follows:

menu: base => ({
  ...
  // wordWrap: "normal",
  whiteSpace: "nowrap",
  overflow: "hidden",
  textOverflow: "ellipsis"
}),

Answer №2

This topic was discussed in a recent thread by another user, which you can find here.

To resolve the issue, modify your CSS from wordWrap: 'normal' to wordWrap: 'break-word'. You can refer to this example for clarification: https://codesandbox.io/s/jj3r81l3.

Here are the complete style properties:

const customStyles = {
    control: (base, state) => ({
      ...base,
      background: "#023950",
      borderRadius: state.isFocused ? "3px 3px 0 0" : 3,
      borderColor: state.isFocused ? "yellow" : "green",
      boxShadow: state.isFocused ? null : null,
      "&:hover": {
        borderColor: state.isFocused ? "red" : "blue"
      }
    }),
    menu: base => ({
      ...base,
      borderRadius: 0,
      hyphens: "auto",
      marginTop: 0,
      textAlign: "left",
      wordWrap: "break-word"
    }),
    menuList: base => ({
      ...base,
      padding: 0
    })
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Aligning images with absolute CSS to text that is wrapping positions the images perfectly alongside the text content

My webpage has a long content section that resizes based on the browser width. Here's an example: <h1 id="loc1">Title</h1> <p>bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bod ...

Is there a way to optimize downloading multiple identical images using html, css, jquery, etc.?

My chess board features 64 squares, each with a picture of a board piece on either a light or dark square. To ensure proper formatting, a Clear knight is placed on the board. The design utilizes a div element with a background image (representing light or ...

What is the best way to format text in React without using spaces?

Here's my take on the problem: https://codesandbox.io/s/fast-smoke-bhudq. The text does not wrap as expected, instead overflowing to the right beyond "HELLO". It appears that setting a maxWidth is not working effectively. Could someone please modify ...

The function L.featureGroup.subGroup() is a powerful tool provided by Leaflet and Cart

I'm struggling with getting the subgroups to function correctly. The clusters display, but when I deactivate layers, the clusters do not update accordingly. Moreover, activating a layer turns on all underlying points. How can I ensure that the subgrou ...

What is the best way to format the JQGrid table for better

Is there a way to indent the table in this example and create white space on the side of the rows and header? I want a white margin on the left of everything below "Stackoverflow example". I'm hoping to achieve this using only CSS. Below is the code ...

Verify the functionality of a specific method invoked within another method through unit testing

I currently have a method in my Angular application that is triggered upon clicking. Inside this method, I pass a value to another private method. .ts file public onViewItem(item: Results): void { const ids = [item.data['id']]; this.anot ...

Ways to bypass mongoose schema validation while making an update request in the API

In my model, one of the fields is specified as providerID: { type: Number, required: true, unique: true }. The providerID is a unique number that is assigned when inserting provider details for the first time. There are situations where I need to update ...

Encountering an import line error specifically in the Firefox browser

My React project utilizes the OverlayLoader library with no issues in Chrome, but encounters a "TypeError: can't convert null to object" error in Firefox. After investigating the issue, I discovered that it originates from the import line for the Over ...

Transmitting FormData via 2 API endpoints in NextJS

As I'm sending formdata, which includes images, to my backend, I've discovered a way to further secure it by utilizing the NextJS API. This involves making calls from the client to the NextJS server/api, with NextJS acting as an intermediary to s ...

Invoke the LazyQuery Hook within a Function

My GraphQL query implementation is as follows: const [loadUsers, { loading, data }] = useLazyQuery(LoadUsersQuery); When I utilize this code snippet in my project, the lazy query loadUsers functions properly and displays the results: return ( <d ...

Utilizing the Double Mapping Feature in React with Typescript

It seems I might be overlooking something, can someone guide me on how to properly double map in this scenario? I'm encountering an error on the second map: Property 'map' does not exist on type '{ departure: { code: string; name: strin ...

Incorporating ErrorText into a material-ui Textfield causes displacement of surrounding elements

In my React app, I have implemented a signup form using material-ui TextField which utilizes the errorText property to display error messages when necessary. errorText={this.state.messages.emailMessage} Initially, the state.messages.emailMessage is set t ...

The animation feature on the slideshow is dysfunctional

For this Vue component, I attempted to create a slideshow. The process is as follows: 1) Creating an array of all image sources to be included (array: pictures) 2) Initializing a variable(Count) to 0, starting from the beginning. 3) Adding v-bind:src=" ...

Unforeseen Extra Information is being transmitted along with Socket.io and Jquery

Issue : There seems to be a problem with the chat message printing an extra character in msg. Upon console logging, an additional object value is appearing in the state. What could be causing this issue? Console log output : 2{"message":"e"} 1{"mess ...

Ways to recycle a section of Javascript for multiple uses on a single page

As a newcomer to website building, I have a query regarding the usage of a JavaScript function designed for a slideshow. My one-page website comprises multiple slideshow units with their own divs, holders, and counters (e.g., 1/4). A JavaScript code contro ...

"An error occurred stating that _co.JSON is not defined in

During my attempt to convert an object into a string using the JSON method, I encountered an error upon loading my page: Error: _co.JSON is undefined The stacktrace for this error message is extensive and seems unnecessary to include at this point. Th ...

Discover the URLs that are connected to my iframe/widget

I've crafted a widget.html page that includes a "Powered by Example.com" box or widget. Additionally, I've implemented an HTML iframe that points to this specific page (widget.html) on my website. <iframe src="http://example.com/widget.html"& ...

Prevent button interaction until completion of modal animation using Bootstrap 4

I have incorporated a Bootstrap 4 modal in my website to display a large navigation menu. The navigation is triggered by a toggle button with a CSS animation that transforms a hamburger icon into an X. However, I encountered an issue where if the button i ...

Is there an issue with updating state in React using context?

I am looking to utilize context and usehook within the UploadButton component so that I can access the state in the UserButton component. What I am attempting to achieve? When the button in UploadButton is clicked, the state isDialogOpen is set to true. I ...

In order to effectively manage the output of these loaders, it may be necessary to incorporate an extra loader. This can be achieved by using the

I'm currently working with react typescript and trying to implement a time zone picker using a select component. I attempted to utilize the npm package react-timezone-select, but encountered some console errors: index.js:1 ./node_modules/react-timezo ...