When the user clicks on a specific element, ensure that it is the main focus and generate an overlay

One of my challenges is implementing a custom element that captures user input upon clicking, focusing on it and overlaying other elements. I want the overlay to disappear if the user clicks outside the div.

I attempted to achieve this using the iron-overlay-behavior, but unfortunately didn't get the desired outcome.

<custom-element 
    with-backdrop 
    scroll-action="lock"
    clicked="{{isClicked}}"
></decision-view>

Most examples available are for paper-dialog, leaving me wondering how to correctly implement iron-overlay-behavior for my own custom element.

Answer №1

It appears that the iron-overlay-behavior is primarily designed for modal dialogs, while your goal seems to be different (for example, modal dialogs typically only allow one to be shown at a time and require user input before returning to normal application or website behavior). In this case, it would make sense for that behavior to disable anything else to ensure focus!

When you mention:

creating an overlay on other elements

What exactly do you mean by that? Are you suggesting covering them with white paint to make them invisible?

Answer №2

Last week, I encountered a similar issue that was resolved by following the advice in this Stack Overflow post.

If you want to see a practical demonstration of how to address the problem, check out this example on CodePen created by Makha:

<dom-module id="parent-component">
  <template>
    <style>
      :host {
        display: block;
        margin: 10px auto auto auto;
        border: 2px solid gray;
        border-radius: 8px;
        background-color: white;
        padding: 5px;
        width: 100px;
      }
      [hidden] {
        display: none;
      }
      paper-button {
        background-color: lightblue;
      }
      #placeholder {
        width: 120px;
        height: 150px;
      }
    </style>
    <div>Give it a try.</div>
    <paper-button on-tap="_doTap">Click</paper-button>
    <div id="placeholder" hidden></div>
  </template>
  <script>
    class ParentComponent extends Polymer.Element {
      static get is() { return 'parent-component'; }
      static get properties() {
        return {
          mychild: {
            type: Object
          }
        }
      }
      _doTap(e) {
        let x = (e.detail.x - 50) + 'px';
        let y = (e.detail.y - 50) + 'px';
        this.mychild = new MyChild();
        this.mychild.addEventListener('return-event', e => this._closeChild(e));
        this.$.placeholder.style.position = 'absolute';
        this.$.placeholder.appendChild(this.mychild);
        this.$.placeholder.style.left = x;
        this.$.placeholder.style.top = y;
        this.$.placeholder.removeAttribute('hidden');
        this.mychild.open();
      }
      _closeChild(e) {
        console.log('The child says '+e.detail);
        this.mychild.remove();
        this.mychild = null;
        this.$.placeholder.setAttribute('hidden', '');
      }
    }
    customElements.define(ParentComponent.is, ParentComponent);
  </script>
</dom-module>

<parent-component></parent-component>

<dom-module id="my-child">
  <template>
    <style>
      :host {
        display: block;
        margin: 10px auto auto auto;
        border: 2px solid gray;
        border-radius: 8px;
        background-color: white;
        padding: 15px;
      }
      paper-button {
        background-color: lightgray;
      }
    </style>
    <div>I'm the child component.</div>
    <paper-button on-tap="_doTap">Close me</paper-button>
  </template>
  <script>
    class MyChild extends Polymer.mixinBehaviors([Polymer.IronOverlayBehavior], Polymer.Element) {
      static get is() { return 'my-child'; }
      static get properties() {
        return {
         withBackdrop: {
          type: Boolean,
          value: true
         }
        }
      }
      ready() {
        super.ready();
        console.log("Daddy?");
      }
      _doTap(e) {
        this.dispatchEvent(new CustomEvent('return-event', 
                                           { detail: 'Goodbye!', bubbles: true, composed: true }));
      }
    }
    customElements.define(MyChild.is, MyChild);
  </script>
</dom-module>

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

Is it possible to establish a specific many-to-many relationship using Prisma?

In the Prisma documentation, it states that the set function can be used to override the value of a relation. const user = await prisma.user.update({ where: { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="73121f ...

Troubleshooting Tips: Investigating a Service Call in AngularJS 2 using ES6 Syntax

MY DILEMMA: I have recently started learning Angular2 and found myself wanting to debug a service call. The service appears to have been properly called, as evidenced by the view display. However, when trying to log the content of the variable holding t ...

Seeking assistance with exporting a Vue single file component that relies on Swiper.js for functionality

I'm having trouble figuring out how to properly export a Vue SFC that includes the mySwiper object. I would appreciate seeing an example from someone who has experience with this. Below is the JavaScript portion of my SFC <script> import Swiper ...

Update the function's argument type signature if the current argument is a function with properties

Looking for input on a potential title change, but for now, here are the details of my specific use case: I'm currently developing a library that facilitates executing methods remotely and accessing properties across serialized boundaries like those ...

Bootstrap3 Remote Modal experiencing conflict due to Javascript

Utilizing a bootstrap modal to showcase various tasks with different content but the same format is my current goal. However, I am encountering an issue when attempting to make the textareas editable using JavaScript. The conflict arises when I open and cl ...

After calling sequelize.addModels, a Typescript simple application abruptly halts without providing any error messages

My experience with Typescript is relatively new, and I am completely unfamiliar with Sequelize. Currently, I have only made changes to the postgres config in the .config/config file to add the dev db configuration: export const config = { "dev" ...

Attempting to showcase a PDF document within a web browser

I am a newcomer to JavaScript and I'm encountering an issue with displaying a PDF file in the browser. Every time I try to do so, I keep receiving the same error message 'Cannot GET...' Despite trying various methods... router.get("/en ...

Crafting interactive image checkboxes

At present, the checkboxes in my application are quite basic with labels. However, our app designer has requested that we revamp this feature and make it more appealing by using clickable images that still function like checkboxes. Allow me to provide an ...

What could be causing the HTML5 canvas not to show up on the screen?

<!doctype html> <html> <head> <title>Canvas test</title> </head> <body> <script type="text/javascript"> c = getElementById('canvas'); ctx = c.getContext("2d")' ctx.fillRect(10,10,10,10); </s ...

Executing a command to modify the local storage (no need for an API request) using redux persist in a React Native environment

Currently, I am facing an issue where I am attempting to trigger an action in Redux sagas to assign an ID to a local store: import { call, takeEvery } from 'redux-saga/effects'; import { BENEFITS } from '../actions/types'; function* ...

Maintaining accurate type-hinting with Typescript's external modules

Before I ask my question, I want to mention that I am utilizing Intellij IDEA. In reference to this inquiry: How do you prevent naming conflicts when external typescript modules do not have a module name? Imagine I have two Rectangle classes in different ...

What are some effective ways to utilize asynchronous ORMs without getting bogged down in overly long callback

Currently, I am utilizing the Joose Javascript ORM plugin, which is still quite young (project page). This is being used to store objects in an Appcelerator Titanium mobile project (company page). Due to the storage being on the client side, the applicatio ...

What are the steps to create a project template in WebStorm by saving an existing project?

Currently, I am working on my Express.js project in WebStorm 8 and I would like to save it as a project template. Can you please guide me on how to do this using WebStorm? ...

Discovering and updating a DOM element with a rejuvenating touch: Angular JS

Although not very experienced with Angular JS, here's what I currently have and what I aim to achieve: <div ng-app="MyApp" ng-controller="appController"> <div class="input-group"> <input class="form-control enableEnter" type=" ...

How can I create a textbox in vue.js that only allows numeric input?

methods: { acceptNumber() { var x = this.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/); this.value = !x[2] ? x[1] : '(' + x[1] + ')' + x[2] + (x[3] ? '-' + x[3] : & ...

TypeScript PatchBaseline with AWS CDK

I am currently working with the AWS CDK and TypeScript, utilizing the @aws-cdk/aws-ssm library to create a PatchBaseline. While I have successfully created the Patch baseline, I'm encountering difficulties when attempting to define approvalRules. I ca ...

Refreshing and reloading within the same jQuery function

My PHP application involves the use of 2 PHP files. chart.php - This page includes a Google chart. <div id="chart_div" style="height:100%;width:100%"> </div> <script type="text/javascript"> google.charts.load('current', ...

Can anyone suggest a method for adding comments and improving the organization of a bower.json file?

Managing a large project with numerous bower dependencies can be challenging. It's often unclear whether these dependencies are still being used or if the specified versions are necessary for a reason. It would be ideal to have the ability to add comm ...

Having trouble rendering an object in my ThreeJS project. The error message says: "THREE.OBJLoader: Unexpected line: 'usemap glass'"

I encountered an error while running threejs in an angular 8 application. The objective is to load an object, for which the object and material files were obtained from Kenney assets. Despite referencing examples on the official threejs site and other onli ...

I require fixed headers that no longer stick once reaching a specific point

I have a setup where the names and occupations of participants are displayed next to their artworks, and I've made it sticky so that as we scroll through the images, the names stay on the left. Now, I want the names to scroll out once the images for e ...