javascript - 如何将 Shadow DOM delegatesFocus 选项与 Polymer 一起使用?

标签 javascript polymer focus web-component shadow-dom

Shadow DOM 的 Google 开发者入门指南 here解释了当单击不可聚焦区域时如何使用 delegatesFocus 来聚焦 Shadow DOM 内的第一个可聚焦元素。

可以在附加影子根并传递选项delegatesFocus: true时完成。

对于 Polymer,我找不到传递该选项的方法。我什至无法执行诸如 this.shadowRoot.delegatesFocus = true 之类的操作,因为它会抛出异常,表明该属性是只读的。

这是示例元素。

<link rel="import" href="https://rawgit.com/Polymer/polymer/master/polymer-element.html">
<dom-module id="pp-input">
  <template>

    <style>
      /* shadow DOM styles go here */
      :host {
        border: 2px solid;
        padding: 10px;
        display: block;
      }
      .content {
        font-size: 1em;
        border: 1px solid;
        padding: 0.2em 0.6em;
      }
    </style>
    <h2>Click Here!</h2>
    <!-- shadow DOM goes here -->
    <input type="text" on-input="onInput" value="[[value]]" />
  </template>

  <script>
    class PPInput extends Polymer.Element {
      static get is() {
        return "pp-input";
      }
      static get properties() {
        return {
          value: {
            type: String,
            notify: true,
            reflectToAttribute: true,
            value: true
          }
        }
      }
      onInput(e) {
        this.value = e.target.value;
      }
      constructor() {
        super();
        this.value = "";
      }

    }
    customElements.define(PPInput.is, PPInput);
  </script>
</dom-module>

<pp-input value="asdf"></pp-input>

最佳答案

更新 - 更简洁的方法

如本链接所述,可以创建自己的影子 DOM

https://www.polymer-project.org/2.0/docs/devguide/dom-template#create-your-own-shadow-root

<小时/>

承蒙 friend 的帮助,在 GitHub 上发现了这个问题。

https://github.com/Polymer/polymer/issues/3988

显然 attachShadow() 选项不是通过 Polymer 公开的,但可以执行以下操作来覆盖 Polymer 创建影子 DOM 的方式。

class PPInput extends Polymer.Element {

    // code...

    _attachDom(dom) {
        if (!this.shadowRoot) {
            this.attachShadow({
                mode: 'open',
                delegatesFocus: true
            });
            this.shadowRoot.appendChild(dom);
        }
        return this.shadowRoot;
    }
}

这样做可以使元素按预期工作。

<link rel="import" href="https://rawgit.com/Polymer/polymer/master/polymer-element.html">
<dom-module id="pp-input">
  <template>

    <style>
      /* shadow DOM styles go here */
      :host {
        border: 2px solid;
        padding: 10px;
        display: block;
      }
      .content {
        font-size: 1em;
        border: 1px solid;
        padding: 0.2em 0.6em;
      }
    </style>
    <h2>Click Here!</h2>
    <!-- shadow DOM goes here -->
    <input type="text" on-input="onInput" value="[[value]]" />
  </template>

  <script>
    class PPInput extends Polymer.Element {
      static get is() {
        return "pp-input";
      }
      static get properties() {
        return {
          value: {
            type: String,
            notify: true,
            reflectToAttribute: true,
            value: true
          }
        };
      }

      onInput(e) {
        console.log(e);
        this.value = e.target.value;
      }
      _attachDom(dom) {
        if (!this.shadowRoot) {
          this.attachShadow({
            mode: "open",
            delegatesFocus: true
          });
          this.shadowRoot.appendChild(dom);
        }
        return this.shadowRoot;
      }
      constructor() {
        super();
        this.value = "";
      }
    }
    customElements.define(PPInput.is, PPInput);
  </script>
</dom-module>

<pp-input></pp-input>

关于javascript - 如何将 Shadow DOM delegatesFocus 选项与 Polymer 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44816513/

相关文章:

wpf - 调用CheckBox.Focus()时如何使CheckBox焦点边框出现?

javascript - Kindle 实验性浏览器将网页中包含的所有 javascript 提前 7 小时错误时间 — 如何抵消?

javascript - polymer 将核心工具栏从高改为正常

polymer - 在 Polymer 的 <google-chart> Web 组件中使用 "google.visualization.*"对象

javascript - 当在同一页面上使用多个绘图时,Plotly.js 只有一个绘图可以工作

c# - WPF MVVM : Set focus to current selected item in Datagrid on button click

jquery focus() for Tab 在 Asp.Net 中跳转两次

javascript - "this"从本地函数对象调用函数时不同

javascript - 混合ajax和完整请求

javascript - 为什么单击关闭按钮后,可忽略的警报不再显示?