Positioning of a paper-dialog in Polymer

I'm currently utilizing polymer's paper-dialog element in my application. While the dialog is always centered by default, I am looking to establish a minimum distance between the dialog and the right side of the window. This way, as the window resizes, the paper-dialog will maintain a set distance from the right side without getting too close. Is there a CSS solution for achieving this requirement, or should I consider alternative techniques?

Answer №1

To define a specific distance between the dialog and the right side, you can modify the 'right' property of your paper-dialog element with a fixed position as shown below:

html /deep/ .dialog-right-position {
  position: fixed;
  top: 10px;
  right: 10px;
}

Make sure to apply the same CSS class name to your element.

<paper-dialog heading="Custom Dialog Positioning" class="dialog-right-position">
  <p>This is a right positioned paper dialog!</p>
</paper-dialog>

If you require a demonstration, see the example provided below:

<!doctype html>
<html>
<head>
  <title>paper-dialog-alignment</title>
  <script src="webcomponentsjs/webcomponents.js"></script>
  <link href="paper-button/paper-button.html" rel="import">
  <link href="paper-dialog/paper-dialog.html" rel="import">

  <style shim-shadowdom>
    html /deep/ .dialog-right-position {
      position: fixed;
      top: 10px;
      right: 10px;
    }

    html /deep/ .dialog-right-position::shadow #scroller {
      width: 500px;
      height: 350px;
    }
  </style>
</head>
<body unresolved>
    <template is="auto-binding">
      <section on-tap="{{toggleRightDialog}}">
        <button>
          Display dialog    
        </button>

        <paper-dialog heading="Custom Dialog Positioning" class="dialog-right-position">
          <p>This is a right positioned paper dialog!</p>
        </paper-dialog>
      </section>
    </template>

    <script>
      var scope = document.querySelector('template[is=auto-binding]');
      scope.toggleRightDialog = function(e) {
        if (e.target.localName == 'button') {
            var d = e.target.nextElementSibling;
            if (d) {
                d.toggle();
            }
        }
      };
    </script>
</body>
</html>

Answer №2

After some troubleshooting, I successfully adjusted the position of the dialog to prevent it from getting too close to the right side of the window. This involved calculating the size of the window, the width of the dialog, and ensuring a minimum amount of space is left between the dialog and any element on the right side of the window to avoid overlapping. If there is sufficient space available, the dialog is centered; otherwise, it is positioned to the right. Feel free to review the code below, which was inspired by jérémy Darchy:

<!doctype html>
<html>
<head>
  <title>paper-dialog-alignment </title>
  <script src="webcomponentsjs/webcomponents.js"></script>
  <link href="paper-button/paper-button.html" rel="import">
  <link href="paper-dialog/paper-dialog.html" rel="import">

  <style shim-shadowdom>

    html /deep/ .dialog-right-position::shadow #scroller {
      width: 500px;
      height: 350px;
    }

    html /deep/ #blueDiv {
      float: right;
      background: blue;
      width: 200px;
      height: 100vh;
    }
  </style>
</head>
<body unresolved>
    <template is="auto-binding">
      <section on-tap="{{toggleDialog}}">
        <button>
          Toggle dialog    
        </button>

        <paper-dialog id="dialog" heading="Custom Dialog Positioning" class="dialog-right-position">
          <p>not overlaping the blue element, centered when possible </p>
        </paper-dialog>

        <div id="blueDiv"></div>

      </section>
    </template>

    <script>

      var scope = document.querySelector('template[is=auto-binding]');

      window.onresize = function(event) {
        var dialog = document.querySelector("html /deep/ #dialog");
        scope.repositionPaperDialog(dialog);
      };

      scope.toggleDialog = function(e) {
        this.$.dialog.toggle();
      };

      // fired event from the core-overlay element, when the dialog opens
      this.addEventListener("core-overlay-position", function(e) {
        scope.repositionPaperDialog(e.detail.target);
      });

       scope.repositionPaperDialog = function(dialog) {

          if ((document.body.clientWidth/2) < (dialog.offsetWidth/2 + this.$.blueDiv.offsetWidth)) {
            // dialog overlaps the blue element, set the right position of the dialog
            dialog.dimensions.position.h = 1;
            dialog.style.right = "200px";
            dialog.style.left = "";
          } else {
            // center dialog
            dialog.style.left = "";
            dialog.style.right = "";
            dialog.dimensions.position.h = 0;
          }
        };
    </script>
</body>
</html>

Answer №3

A simpler approach to achieve the same result is by defining all sides (top, bottom, left, right) in the CSS for paper-dialogs. I set them all to 25% in my example, resulting in a dialog that is evenly positioned on the screen. This method proved to be effective in solving the issue at hand.

paper-dialog {
  position: fixed;
  top: 25%;
  left: 25%;
  bottom: 25%;
  right: 25%;
  margin: 0px;
}

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

What techniques can I use to achieve a seamless transition using Javascript and CSS?

I'm striving for a seamless CSS transition. The concept of transition had me puzzled. Even though I utilized the setTimeout method in JavaScript to make my CSS functional, it lacks that SMOOTH feel! Check out my code below. function slideChange( ...

The functionality of an AJAX poll is not functioning properly due to issues with the PHP

I am embarking on my first website and project, hoping to see it through to completion. In my room, I have a modest site set up on a Raspberry Pi 2. Users visit this site to cast their votes of 'yes' or 'no'. However, there seems to be ...

What is the process for eliminating the hover effect on a Bootstrap button?

I have customized a Bootstrap button in my stylesheet to make it dark, but it still has a hover effect from Bootstrap that I want to remove. How can I get rid of this hover effect? Take a look at the code snippet below for reference: .btn-dark { min-w ...

Triggering an automatic pop-up window using JavaScript based on a PHP condition

I have developed a hidden pop-up box that becomes visible when targeted, with its opacity increasing to one. I am aiming for the pop-up to appear if the user is identified as a "firstTimeUser," a status saved in a PHP $_SESSION variable. However, I am stru ...

How come user CSS in Stylish takes precedence over the author's CSS?

Here's a sample page: <!DOCTYPE html> <html> <head> <style type="text/css"> body { background: black; } </style> </head> <body> </bod ...

Steps for creating a herringbone design on an HTML canvas

Seeking Help to Create Herringbone Pattern Filled with Images on Canvas I am a beginner in canvas 2d drawing and need assistance with drawing mixed tiles in a cross pattern (Herringbone). var canvas = this.__canvas = new fabric.Canvas('canvas' ...

Alter the dimensions and material displayed on Vue

In my Vue.js project, I have a topbar with two divs whose size and content need to be adjusted based on whether the user is logged in. If they are not logged in, it should look like this: <div id='search' width="400px"></div><div ...

Dynamically setting the IMG SRC attribute with the base64 result of a FileReader for file input

Looking for a little guidance on something new, I'll keep it brief because I'm sure it's just some small detail I'm overlooking... Starting with an image like this, where currentImage is the initial existing image path; <img src="{ ...

Disappearance of the second level in a CSS dropdown menu

I am currently working on creating a dropdown menu using only CSS. Check out this example I'm using: http://jsfiddle.net/DEK8q/8/ My next step is to center the menu, so I added position-relative like this: #nav-container { position: rela ...

Creating a Multilevel Dropdown Menu: A Step-by-Step Guide

I am curious about how to create a multilevel dropdown menu using Bootstrap 5 and vanilla JavaScript. I created an example based on the Bootstrap 5 dropdowns component documentation, but it did not display when I clicked on it. The issue seems to be relat ...

The input field will be in a read-only state if it contains a value from the database. However, it will be editable if the value

Hello everyone, I am a newcomer to this community and would greatly appreciate your help. I am encountering an issue with the following lines of code: <input type="text" class="form-control" id="fines" name="fines&quo ...

Tips for creating a cohesive group of HTML elements within an editable area

Imagine having a contenteditable area with some existing content: <div contenteditable="true"> <p>first paragraph</p> <p> <img width='63' src='https://developer.cdn.mozilla.net/media/img/mdn-logo-s ...

Refresh WebPage automatically after a Servlet successfully uploads and processes an image

I have a webpage that includes an image and a button. When the button is clicked, it uploads the image by submitting a form to a file upload servlet. However, after successfully uploading the image, the servlet does not display it in the img tag. Here is ...

Guide on eliminating flex in CSS

Having an issue with aligning two grids created in HTML using Bootstrap. Below is the code: <!DOCTYPE html> <html lang="en"> <head> <title>IRIS Flower Classification</title> <link rel="stylesheet" href="{{ url_for( ...

Bootstrap grid button with maximum flexibility

I am currently exploring the intricacies of the bootstrap grid system. My goal is to create a set of buttons that look like this: https://i.sstatic.net/V5meG.png Each button should expand to fill the width and height of its container. However, when a b ...

Mastering the art of column stacking with CSS on your WordPress website

Looking for a CSS solution to adjust the layout when the screen resolution is lowered. Currently, I have two rows with three columns in each row, containing blurbs. My goal is to make these two rows convert into three rows with two columns each at a cert ...

Change the value of the material slide toggle according to the user's response to the JavaScript 'confirm' dialogue

I am currently working on implementing an Angular Material Slide Toggle feature. I want to display a confirmation message if the user tries to switch the toggle from true to false, ensuring they really intend to do this. If the user chooses to cancel, I&ap ...

What is the best way to personalize the icon for this navigation button?

I'm interested in making some changes, especially to the border. I've already figured out how to adjust the color of the 3 bars like so: .navbar-default .navbar-toggle .icon-bar { background-color: #fff; } Modification RESOLUTION .navbar- ...

Javascript continues to conceal my web background without my permission

After loading my webpage, I click on a link and the expected javascript runs as planned. However, once it completes and presents the results, the page goes blank to a white screen displaying only the outcomes. My goal is to have these results showcased o ...

allowing absolutely positioned region to expand

For further information, please visit this page: test page In the process of designing a website, I have implemented a sidebar featuring an accordion style vertical navigation bar. The sidebar is set to be absolutely positioned in relation to its containi ...