1<!--
2@license
3Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7Code distributed by Google as part of the polymer project is also
8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9-->
10
11<link rel="import" href="../polymer/polymer.html">
12
13<!--
14`iron-a11y-announcer` is a singleton element that is intended to add a11y
15to features that require on-demand announcement from screen readers. In
16order to make use of the announcer, it is best to request its availability
17in the announcing element.
18
19Example:
20
21    Polymer({
22
23      is: 'x-chatty',
24
25      attached: function() {
26        // This will create the singleton element if it has not
27        // been created yet:
28        Polymer.IronA11yAnnouncer.requestAvailability();
29      }
30    });
31
32After the `iron-a11y-announcer` has been made available, elements can
33make announces by firing bubbling `iron-announce` events.
34
35Example:
36
37    this.fire('iron-announce', {
38      text: 'This is an announcement!'
39    }, { bubbles: true });
40
41Note: announcements are only audible if you have a screen reader enabled.
42
43@group Iron Elements
44@demo demo/index.html
45-->
46
47<dom-module id="iron-a11y-announcer">
48  <template>
49    <style>
50      :host {
51        display: inline-block;
52        position: fixed;
53        clip: rect(0px,0px,0px,0px);
54      }
55    </style>
56    <div aria-live$="[[mode]]">[[_text]]</div>
57  </template>
58</dom-module>
59<script>
60'use strict';
61(function() {
62  'use strict';
63  Polymer.IronA11yAnnouncer = Polymer({
64    is: 'iron-a11y-announcer',
65
66    properties: {
67
68      /**
69        * The value of mode is used to set the `aria-live` attribute
70        * for the element that will be announced. Valid values are: `off`,
71        * `polite` and `assertive`.
72        */
73      mode: {
74        type: String,
75        value: 'polite'
76      },
77
78      _text: {
79        type: String,
80        value: ''
81      }
82    },
83
84    created: function() {
85      if (!Polymer.IronA11yAnnouncer.instance) {
86        Polymer.IronA11yAnnouncer.instance = this;
87      }
88
89      document.body.addEventListener('iron-announce', this._onIronAnnounce.bind(this));
90    },
91
92    /**
93      * Cause a text string to be announced by screen readers.
94      *
95      * @param {string} text The text that should be announced.
96      */
97    announce: function(text) {
98      this._text = '';
99      this.async(function() {
100        this._text = text;
101      }, 100);
102    },
103
104    _onIronAnnounce: function(event) {
105      if (event.detail && event.detail.text) {
106        this.announce(event.detail.text);
107      }
108    }
109  });
110
111  Polymer.IronA11yAnnouncer.instance = null;
112
113  Polymer.IronA11yAnnouncer.requestAvailability = function() {
114    if (!Polymer.IronA11yAnnouncer.instance) {
115      Polymer.IronA11yAnnouncer.instance = document.createElement('iron-a11y-announcer');
116    }
117
118    document.body.appendChild(Polymer.IronA11yAnnouncer.instance);
119  };
120})();
121
122</script>
123