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.

Is there a way to append the suggestions container to the body?

See original GitHub issue

Are you reporting a bug?

I am using the autosuggest within a component which uses overflow-x: hidden, this makes the overflow-y: auto (you can’t set it to visible), which leads to a problem where the suggestions box does not overflow out of the parent container even if I make it absolute.

It is similar to the discussion here: https://stackoverflow.com/questions/6421966/css-overflow-x-visible-and-overflow-y-hidden-causing-scrollbar-issue

It is similar to this reported issue: https://github.com/moroshko/react-autosuggest/issues/112

Codepen: https://codepen.io/gaurav5430/pen/YzPVoOe

Steps to reproduce:

  1. Focus on the input field
  2. Type c, and wait for suggestions to appear

Observed behaviour: Suggestions do not appear

Expected behaviour: Suggestions should appear

One solution to the problem is to append the suggestion box to body, since in my case, I can’t control the overflow property of the parent.

Is there currently a way to append the suggestions to the body? Also, would any accessibility functionality break if we append it to the body using something like a react portal ?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

5reactions
gaurav5430commented, Dec 24, 2019

I was able to resolve this issue in particular by using ReactDOM.createPortal and renderSuggestionsContainer. Something like this:

renderSuggestionsContainer = ({ containerProps, children }) => {
    let toRender = null;
    // we have to handle the show and hide of the suggestionsContainer, unlike suggestions for which display is handled automatically by Autosuggest
    if (this.state.showSuggestions) {
      // this.input is the input ref as received from Autosuggest
      const inputCoords = this.input.getBoundingClientRect();
      const style = {
        position: 'absolute',
        left: inputCoords.left + window.scrollX, // adding scrollX and scrollY to get the coords wrt document instead of viewport
        top: inputCoords.top + 52 + window.scrollY,
        maxHeight: '264px',
        overflow: 'auto',
        zIndex: 4,
        backgroundColor: '#ffffff',
        borderRadius: '4px',
        boxShadow: '0 4px 16px 0 rgba(0,0,0,.15), 0 1px 2px 0 rgba(0,0,0,.07), 0 0 1px 0 rgba(0,0,0,.2)',
        width: inputCoords.width,
      };

      toRender = (
        <div {... containerProps} style={style}>
         Some text:
        {children}
        </div>
      );
      return ReactDOM.createPortal(toRender, document.body);
    }

    return null;
  }

This does seem to retain all the accessibility stuff, keyboard listeners and everything.

Some pain points are:

  1. need to specify the whole css inline, as some part of it needs to be dynamic. This might just be dependent on my setup.
  2. getting the input ref was not as straight forward. I am rendering my own input component, and when i was trying to get the ref to input directly, Autosuggest was giving me errors in selectionOnFocus, as discussed in some other issues as I wasn’t passing the ref correctly.
1reaction
ivesdebruyckercommented, Sep 4, 2020

@gaurav5430 Great solution.

Some pointers:

  • by wrapping the container in <div className="react-autosuggest__container"> you don’t need to repeat the styling (except for the dynamic parts)
  • I’m checking for containerProps.className.indexOf('react-autosuggest__suggestions-container--open') >= 0 to know the state of the container. Probably not as robust but it works for me.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to append the suggestions container to the body?
I am using the autosuggest within a component which uses overflow-x: hidden, this makes the overflow-y: auto (you can't set it to visible),...
Read more >
How can jQuery load some html and append it to the <body ...
If there is a container,I can do this:$('#container').load(...).appendTo('body').Right?But I need to implement it without container. – omg.
Read more >
append() - jQuery Mobile Demos
Description: Insert content, specified by the parameter, to the end of each element in the set of matched elements. version added: 1.0.append( ...
Read more >
Class Body | Apps Script - Google Developers
Creates and appends a new Table . This method will also append an empty paragraph after the table, since Google Docs documents cannot...
Read more >
How to Combine multiple elements and append the result into ...
Approach 3: Appending elements using pure JavaScript. Use document.createElement() method with Tag name of element in Argument. Set values in ...
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