Vaadin-combo-box: Allow specifying overlay width

Created on 23 Nov 2018  ·  6Comments  ·  Source: vaadin/vaadin-combo-box

It seems that the overlay element's width is fixed to be the same as the input field's. In some cases (short input, long item captions) it would be useful to allow customizing the overlay width to be larger. If this is doable with styles, such a method should be documented.

enhancement documentation flow

Most helpful comment

There is now a new feature in the web component to make this easier (closed issue https://github.com/vaadin/vaadin-combo-box/issues/529, see merged PR https://github.com/vaadin/vaadin-combo-box/pull/876)

This issue should now be about providing a helper method e.g. setOverlayWidth(String cssLength) for setting this new CSS custom prop --vaadin-combo-box-overlay-width.

All 6 comments

We have the exact issue described;
For a location input, a typical code is "SVG", with caption "Stavanger Airport Sola"
This is an intranet app, so our users know most of the codes by heart, and want to write "SVG" instead of "Stavanger..."
Also, we have several of these next to each other in an editable grid. Space is tight, so we want the columns and comboboxes to be sized for the code, and not for the caption.

Vaadin suggested a workaround, which solves our immediate problem:

@CssImport
    ( id        = "vaadin-combo-box-overlay-override"
    , value     = "./styles/vaadin-combo-box-overlay.css"
    , themeFor  = "vaadin-combo-box-overlay"
    )

./styles/vaadin-combo-box-overlay.css:

:host {
     width: 500px !important;
}

Or, if you only want it on some comboboxes:

:host([theme~="large"]) {
     width: 500px !important;
}

combobox.getElement().setAttribute("theme", "large");

Duplicate of vaadin/vaadin-combo-box/issues/529.

See the suggested demo improvement at https://github.com/vaadin/vaadin-combo-box/issues/529#issuecomment-374611447

To dynamically update the ComboBox overlay width:

:host([theme="my-combo-box"]) {
  width: var(--my-combo-box-overlay-width, 200px) !important;
}
UI.getCurrent().getPage().executeJs("ShadyCSS.styleDocument({'--my-combo-box-overlay-width': $0 + 'px'})", newWidthAsInteger);

There is now a new feature in the web component to make this easier (closed issue https://github.com/vaadin/vaadin-combo-box/issues/529, see merged PR https://github.com/vaadin/vaadin-combo-box/pull/876)

This issue should now be about providing a helper method e.g. setOverlayWidth(String cssLength) for setting this new CSS custom prop --vaadin-combo-box-overlay-width.

I can confirm that the following code works nicely as suggested by @Haprog

comboBox.getElement().getStyle().set("--vaadin-combo-box-overlay-width", "400px")

The new docs site has examples of using a feature mentioned above:
https://vaadin.com/docs-beta/latest/ds/components/combo-box/#popup-width

Was this page helpful?
0 / 5 - 0 ratings

Related issues

silentHoo picture silentHoo  ·  3Comments

sohrabtaee picture sohrabtaee  ·  4Comments

florent1933 picture florent1933  ·  14Comments

steffen-harbich-cognitum picture steffen-harbich-cognitum  ·  8Comments

davidmaxwaterman picture davidmaxwaterman  ·  6Comments