Code Monkey home page Code Monkey logo

Comments (24)

dparker2 avatar dparker2 commented on May 11, 2024 7

For future readers, this is exactly what I added to get this to work (with v2 of popper):

  &[data-popper-reference-hidden='true'] {
    opacity: 0;
    pointer-events: none;
  }

I found that opacity worked better than visibility, which was causing a weird lag for some reason. The docs also don't include the ='true' part in the selector even though that's needed here, since the attribute changes to false instead of actually being removed when the reference is in view.

from floating-ui.

atomiks avatar atomiks commented on May 11, 2024 6

@FezVrasta hide is enabled by default isn't it? 😆

https://popper.js.org/popper-documentation.html#modifiers..hide.enabled

@ericsakmar you need to use something like

.popper[x-out-of-boundaries] {
  display: none;
}

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024 1

oh, right, thanks

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

Yes probably we should add a "hide when reference is not visible" modifier (maybe with a shorter name ahah).

If you create one please send a PR, it would be a nice addition!

from floating-ui.

andresgutgon avatar andresgutgon commented on May 11, 2024

What is the best why to hide popper? I mean. With a modifier you just past data object modified. What should I pass to hide the popper?

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

I think the applyStyle modifier should be edited a bit to allow custom CSS properties.

from floating-ui.

andresgutgon avatar andresgutgon commented on May 11, 2024

Hi, I'm not sure I have time to do the PR now. But this is what I did:

I defined a modifier called hideOnReferenceNotVisibleOnBoundaries and if boundaries is defined check their visibility. Be aware that this code use jQuery dependent and ES6 syntax :)

      /**
       * Check if reference is visible inside boundaries
       *
       * @param {Object} data
       * @param {Object} boundariesElement
       * @return {Boolean}
       */
      function isVisible(data, boundariesElement) {
        const { boundaries, offsets } = data;
        const { top, bottom } = offsets.reference;
        const { height: boundariesHeight} = boundaries;
        const boundariesOffsetTop = $(boundariesElement).offset().top;
        const boundariesBottom = boundariesOffsetTop + boundariesHeight;

        return ((top <= boundariesBottom) && (bottom >= boundariesOffsetTop));
      }

      /**
       * Modifier to hide popper if is outside boundaries
       * If boundaries is not defined we skip the check
       *
       * @param {Object} data
       * @return {Object}
       */
      const hideOnReferenceNotVisibleOnBoundaries = (data) => {
        const boundariesElement = data.instance._options.boundariesElement;

        if (boundariesElement === 'viewport') return data;

        const visible = isVisible(data, boundariesElement);

        // Hide popper
        $(data.instance._popper).toggleClass('js-hide', !visible);

        return data;
      };

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

That's a bit too hacky to be included in Popper.js... Thanks anyway for your attempt. If someone else is interested please let me know. I can help with it.

from floating-ui.

andresgutgon avatar andresgutgon commented on May 11, 2024

What's the hacky part? Just to improve my version :)

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

We don't use jQuery with Popper.js, and modifiers should never access the DOM to write to it in any way.

Your modifier should edit only the data object, adding maybe a data.hidden property, then, applyStyle modifier should read the value and then hide or not the popper.

from floating-ui.

andresgutgon avatar andresgutgon commented on May 11, 2024

Ok, thanks @FezVrasta

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

@andresgutgon check out #53, it should help with this modifier.

from floating-ui.

andresgutgon avatar andresgutgon commented on May 11, 2024

I think we can close this one

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

Why? The enhancement is not implemented yet

from floating-ui.

andresgutgon avatar andresgutgon commented on May 11, 2024

I thought is a user decision to implement that. That you only provide the styles in data to change visibility.

Of you want this modifier/whatever be in the core?

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

I think it's a feature that would be nice to have in the core.

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

I've implemented this feature in the v1-dev branch, if someone wants to backport it to the v0 version please use the modifier below and include it before the applyStyles modifier:

function hide(data) {
    var refRect = data.offsets.reference;
    var bound = data.boundaries;

    if (
        refRect.bottom < bound.top ||
        refRect.left > bound.right ||
        refRect.top > bound.bottom ||
        refRect.right < bound.left
    ) {
        data.styles.display = 'none';
    } else {
        data.styles.display = '';
    }

    return data;
}

from floating-ui.

ericsakmar avatar ericsakmar commented on May 11, 2024

I'm still seeing the popover after it scrolls outside of the boundaries. Is there anything I need to do to enable this feature?

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024

You have to enable it, it's disabled by default

from floating-ui.

ericsakmar avatar ericsakmar commented on May 11, 2024

Cool. How would I enable it? I'm not quite sure I understand. Thanks!

from floating-ui.

FezVrasta avatar FezVrasta commented on May 11, 2024
new Popper(ref, pop, {
  modifiers: {
    hide: { enabled: true }
  }
})

from floating-ui.

larsbo avatar larsbo commented on May 11, 2024

is this really working?
I use Popper.js 1.15.0 and my popper containers never gets the x-out-of-boundaries attribute when they are not inside the viewport.

nvm, some kind of npm issue... works fine after refreshing node_modules 😄

from floating-ui.

nmanikumar5 avatar nmanikumar5 commented on May 11, 2024

Its not working properly @FezVrasta

from floating-ui.

JLiu1272 avatar JLiu1272 commented on May 11, 2024

Hi, @FezVrasta, I tried using the modifier you suggested, and like @nmanikumar5, I am not able to get it to work either.

from floating-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.