Adjust the width of the dropdown list to be distinct from the width of the dropdown button

My goal is to create a dropdown where the width of the button is different from the width of the list. I am aware that we can adjust the widths using inline styling like style={{ minWidth: '200px' }}. Now, I want to set the width of the dropdown list to 500px.

I attempted adding .fui-Listbox { width: 500px } in the stylesheet, but it didn't have the desired effect.

Is there a way to achieve this?

Check out the StackBlitz here: https://stackblitz.com/edit/react-ts-cejuzs?file=App.tsx,style.css,index.html

import {
  FluentProvider,
  webLightTheme,
  makeStyles,
  shorthands,
} from '@fluentui/react-components';
import {
  Dropdown,
  Option,
  DropdownProps,
} from '@fluentui/react-components/unstable';
import { TextField } from '@fluentui/react';
import * as React from 'react';
import './style.css';

export default class Grouped extends React.Component<{}, {}> {
  land = ['Cat', 'Dog', 'Ferret', 'Hamster'];

  render() {
    return (
      <FluentProvider
        className="fluent-provider"
        style={{ display: 'flex' }}
        theme={webLightTheme}
      >
        <Dropdown
          className="drop-down"
          style={{ minWidth: '200px' }}
          placeholder="Select an animal"
        >
          {this.land.map((option) => (
            <Option key={option}>{option}</Option>
          ))}
        </Dropdown>
      </FluentProvider>
    );
  }
}

Edit 1: In response to pier farrugia's suggestion, I tried using

.fui-Listbox { width: 500px !important; }
. This did adjust the width of the dropdown list, but it affected all dropdowns project-wide, which is not what I intended. I experimented with different classes and selectors like
.drop-down .fui-Listbox { width: '500px' !important; }
and
.drop-down-2 .fui-Listbox { width: '100%' !important; }
, but they didn't work as expected. It seems that under the dropdown mode, .fui-Listbox is not considered a child class of drop-down-2 or .fluent-provider-2. Can anyone provide guidance on correctly targeting specific dropdowns without affecting others?

Visit the updated StackBlitz here: https://stackblitz.com/edit/react-ts-51hiro?file=App.tsx,style.css,index.html

Answer №1

If you're looking to adjust the width of a dropdown list, there are a couple of ways to do it. You can either utilize the style or className attribute for the specific dropdown slot (listbox):

    <Dropdown
      className="drop-down"
      style={{ minWidth: '200px' }}
      placeholder="Select an animal"
      listbox={{style: {width: "500px"}}}
    >
      {this.land.map((option) => (
        <Option key={option}>{option}</Option>
      ))}
    </Dropdown>

Alternatively, if you prefer to stick with your existing stylesheet:

    <Dropdown
      className="drop-down"
      style={{ minWidth: '200px' }}
      placeholder="Select an animal"
      listbox={{className: "custom-dropdown-width"}}
    >
      {this.land.map((option) => (
        <Option key={option}>{option}</Option>
      ))}
    </Dropdown>

In your stylesheet, simply add the following rule:

.custom-dropdown-width {
  width: '500px' !important;
}

Answer №2

Here's the code snippet:

<Dropdown
          className="drop-down"
          style={{ width: '100px' }}
          placeholder="Select an animal"
        >

The width of 100px in your style is dynamically generated.

Even using !important, changing the style is not effective.

To make it work, update the line

style={{ width: '100px' }}

to

style={{ width: '500px' }}

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

You cannot make a hook call within the body of a function component, unless you are using useNavigate and useEffect in conjunction with axios interceptors

Currently, I am delving into React and attempting to include a Bearer token for private API calls. My approach involves writing a private axios function to intercept requests and responses. Subsequently, I invoke it in my API.js file to fetch data from the ...

Next.js Error: Unable to access the 'collection' property, as it is undefined

I have recently started using next.js and I am interested in creating a Facebook clone by following YouTube tutorials. However, I keep encountering the error message "Cannot read properties of undefined (reading 'collection')". To troubleshoot, I ...

I am currently struggling with a Typescript issue that I have consulted with several individuals about. While many have found a solution by upgrading their version, unfortunately, it

Error message located in D:/.../../node_modules/@reduxjs/toolkit/dist/configureStore.d.ts TypeScript error in D:/.../.../node_modules/@reduxjs/toolkit/dist/configureStore.d.ts(1,13): Expecting '=', TS1005 1 | import type { Reducer, ReducersMapO ...

Just encountered an error right after setting up node; Module 'yallist' not found in stack trace... If anyone has a solution, please share in

When checking the version with node -v, the version is displayed properly, but when attempting to do the same with npm -v, an error is shown instead. Here is the error message displayed with npm -v: internal/module/cjs/loader:933 const err = new Error(mes ...

Error: Unable to locate module: The requested module '@material-ui/data-grid' could not be found

As a newcomer to React, I encountered an error when trying to integrate Material UI data table into my salesTable.js file. The error message displayed is as follows: ERROR in ./src/SalesTable/SalesTable.js 5:0-50 Module not found: Error: Can't resolve ...

Is it possible for Google to crawl links that are generated using JavaScript?

I have developed four websites using HTML, CSS, and JavaScript without utilizing any server-side languages like PHP. My main goal is to acquire backlinks with various anchor text. To achieve this, I have decided to use a single JavaScript file on all my w ...

I want to show the age of users on their buttons rather than on the page using ReactJS. How can I achieve this?

I currently have an array containing five Users. The following code successfully displays each user's information from the array when the pop-up button is clicked. Everything works as expected. Now, I have implemented a form to update the age of eac ...

What are the implications of utilizing props value with a React hook?

Recently diving into the world of React hooks, I've come across a new pattern that isn't covered in the official documentation: const MyComponent = ({myProp}) => { const [myPropHook, setPropHook] = useState(myProp) ... } Is this approach co ...

Tips for changing the color of an underline in an <a> tag when it is hovered over

Is it possible to change the color of the underline in an <a> tag when hovering over it? After some investigation, it seems that direct color changes are not feasible. Instead, you can use the border-bottom option. However, my attempt did not yield ...

What is the rationale behind using :: before display: inline-block in Angular Material MDC's mat-mdc-form-field-error-wrapper, causing substantial padding?

I recently made the switch from Angular Material to Angular Material MDC in preparation for upgrading to Angular 17 from version 15. However, I've noticed that some styles are now broken. One issue I'm facing is a significant padding between the ...

vertical alignment, almost there

Is there a way to align a block of text so that the top of the first line is positioned 50% down from the top of the td element? Imagine this scenario, where the top of the first row is exactly at the 50% mark. -------------- | | | ...

Why does Popover in Material-UI hide the backdrop of the Modal?

It's puzzling why Popovers are set to have an invisible backdrop by default, with no apparent way to change it. Is there a key concept in Material Design that I'm overlooking? Should I raise this concern as an issue? <Modal container ...

What is the best way to utilize data obtained from a POST request in order to carry out a subsequent

I'm currently working on a project that involves integrating the Spotify API into a web application. The goal is to take a user-submitted search keyword, send it to the server for processing, and then receive and display the search results on the fron ...

CSS selector that targets an element exclusively when it contains solely another element, devoid of any additional text

Is there a CSS selector that can target an element, like <p>, with only a specified child element, such as <a>, within it and nothing else? For instance: <p><a>Some text</a></p>. I want to avoid selecting elements like & ...

The textfields within the .map are not aligned next to each other

Adding the .map caused the name and quantity text fields to no longer display side by side. Before adding the .map, these two fields were in the same row but now they are not. <Grid container rowSpacing={1} columnSpacing={{ xs: 1, sm: 2, md: 3 }}> ...

Element widths are displaying uneven alignment

I can't seem to wrap my head around what's happening here. I've got an HTML layout with a header, sidebar, and central content page. Both the sidebar and central content are within the same div acting as a clearfix. I floated the sidebar le ...

After deploying on Google Cloud, the Google Places API is showing no results

I've created an app that utilizes the Google Places API for its functionality. The issue I'm facing is that the places load fine when testing the app locally, but once it's deployed on Google Cloud, the results don't show up. Even thoug ...

Animate the CSS when the content is within the viewport using pagepiling.js integration

I'm currently working on animating content as it enters the viewport. However, I've encountered an issue where jQuery (used to check if the content is in the viewport) isn't functioning properly alongside pagepiling.js (). I suspect this mig ...

Tips for including a JWT token as a query parameter in NEXT js?

const UserJwt = (props) => { const { jwt } = props; const [jwtToken, setJwtToken] = useState(null); useEffect(() => { if (jwt) { setAuthToken(jwt); setJwtToken(jwt); } }, [jwt]); return <MobilePurchaseScreen {...pro ...

Using Redux to populate initial input values in React

Here's the situation - I have an input field where users can enter their email address. When they click 'save,' this email is stored in the database. The challenge I'm facing is that when the page is refreshed, I want the input field t ...