app-bar.html 2.57 KB
<dom-module id="app-bar">
  <!--<link rel="import" type="css" href="app-bar.css">-->
  <style>
    :host * {
      box-sizing: border-box;
    }

    :host(.search-on) {
      left: 0;
      background: inherit;
      z-index: 1001;
    }

    :host ::content iron-icon {
      margin-right: 15px;
      cursor: pointer;
    }

    #search {
      position: relative;
    }

    #search iron-icon {
      margin-right: 0;
    }

    #search[show] {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      padding: 0 16px;
      background: #fff;
    }

    #search input {
      display: none;
      font-family: var(--primary-font-family);
      font-size: 15px;
      width: 100%;
      padding: 10px;
      border: 0;
      border-radius: 2px;
      -webkit-appearance: none;
    }

    #search[show] input {
      display: block;
    }

    #search input:focus {
      outline: 0;
    }
  </style>
  <template>
    <content></content>
    <div id="search" class="horizontal layout center" show$="{{showingSearch}}" on-tap="toggleSearch">
      <iron-icon icon="search" hidden$="[[noSearch]]"></iron-icon>
      <form on-submit="performSearch" class="flex">
        <input type="search" id="query" value="{{query::keyup}}" autocomplete="off" placeholder="Search Elements" on-blur="clearSearch">
      </form>
    </div>
  </template>
</dom-module>

<script>
  Polymer({
    is: 'app-bar',
    properties: {
      query: {
        type: String,
        notify: true
      },
      showingSearch: {
        type: Boolean,
        value: false
      },
      noSearch: {
        type: Boolean,
        value: false
      }
    },
    observers: [
      'updateSearchDisplay(showingSearch)'
    ],
    listeners: {
      keyup: 'hotkeys'
    },
    toggleSearch: function(e) {
      if (e) {
        e.stopPropagation();
      }
      if (e.target === this.$.query) {
        return;
      }

      this.showingSearch = !this.showingSearch;
    },
    clearSearch: function() {
      this.showingSearch = false;
    },
    updateSearchDisplay: function(showingSearch) {
      if (showingSearch) {
        this.classList.add('search-on');
        this.async(function() {
          this.$.query.focus();
        });
      } else {
        this.classList.remove('search-on');
      }
    },
    hotkeys: function(e) {
      // ESC
      if (e.keyCode === 27 && Polymer.dom(e).rootTarget === this.$.query) {
        this.showingSearch = false;
      }
    },
    performSearch: function(e) {
      e.preventDefault();
      this.fire('nav', {url: '/browse?q=' + this.query});
    }
  });
</script>